Adding Custom Fields to your WooCommerce products are a great way to show custom data on your product pages. This presentation will show you how to add Custom Fields to your Simple & Grouped products as well as adding them to Variable Products.
2. Custom Fields allow you to add custom information
to your WooCommerce products
3. Using the built-in WooCommere hooks, Custom
Fields can be added to any of the standard
WooCommerce product panels (incl. variable
products)
4. Adding New Fields to Simple Products
function mytheme_woo_add_custom_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Add your custom fields here…
echo '</div>';
}
5. Adding New Fields - Text Field
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'My Text Field', 'mytheme' ),
'placeholder' => 'http://',
'desc_tip' => true,
'description' => __( 'Some helpful tooltip.', 'mytheme' )
)
);
6. Adding New Fields - Number Field
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_number_field',
'label' => __( 'My Number Field', 'mytheme' ),
'placeholder' => '',
'desc_tip' => false,
'description' => __( 'Some helpful text', 'mytheme' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0',
),
)
);
12. Add Fields to Product Panel
function mytheme_woo_add_custom_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Your custom fields…
echo '</div>';
}
add_action( 'Specify WooCommerce Hook Here...', 'mytheme_woo_add_custom_fields' );
13. Add Fields to Product Panel - General Panel
// After pricing fields
add_action( 'woocommerce_product_options_pricing', 'mytheme_woo_add_custom_fields' );
// After downloadable file fields
// Only visible when it's a downloable product
add_action( 'woocommerce_product_options_downloads','mytheme_woo_add_custom_fields' );
// After tax fields
add_action( 'woocommerce_product_options_tax', 'mytheme_woo_add_custom_fields' );
// After all General default fields
add_action( 'woocommerce_product_options_general_product_data',
'mytheme_woo_add_custom_fields' );
14. Add Fields to Product Panel - Inventory Panel
// After SKU field
add_action( 'woocommerce_product_options_sku', 'mytheme_woo_add_custom_fields' );
// After Manage Stock field
add_action( 'woocommerce_product_options_stock', 'mytheme_woo_add_custom_fields' );
// After Manage Stock field but only visible if checked
add_action( 'woocommerce_product_options_stock_fields', 'mytheme_woo_add_custom_fields' );
// After Stock Status field
add_action( 'woocommerce_product_options_stock_status', 'mytheme_woo_add_custom_fields' );
// After Sold Individually field
add_action( 'woocommerce_product_options_sold_individually',
'mytheme_woo_add_custom_fields' );
// After all Inventory default fields
add_action( 'woocommerce_product_options_inventory_product_data',
'mytheme_woo_add_custom_fields' );
15. Add Fields to Product Panel - Shipping Panel
// After Dimensions field
add_action( 'woocommerce_product_options_dimensions', 'mytheme_woo_add_custom_fields' );
// After all Shipping default fields
add_action( 'woocommerce_product_options_shipping', 'mytheme_woo_add_custom_fields' );
16. Add Fields to Product Panel - Linked Products
// After all Linked Products default fields
add_action( 'woocommerce_product_options_related', 'mytheme_woo_add_custom_fields' );
17. Add Fields to Product Panel - Attributes Panel
// After all Attributes default fields
add_action( 'woocommerce_product_options_attributes', 'mytheme_woo_add_custom_fields' );
18. Add Fields to Product Panel - Advanced Panel
// After Enable Reviews field
add_action( 'woocommerce_product_options_reviews', 'mytheme_woo_add_custom_fields' );
// After all Advanced default fields
add_action( 'woocommerce_product_options_advanced', 'mytheme_woo_add_custom_fields' );
19. Saving our Fields - Simple Products
// Save Fields
function mytheme_woo_add_custom_fields_save( $post_id ){
// Save your custom fields here…
}
add_action( 'woocommerce_process_product_meta', 'mytheme_woo_add_custom_fields_save' );