SlideShare a Scribd company logo
anthonyhortin
@maddisondesigns #WPMELB
Working with Custom Fields
Custom Fields allow you to add custom information
to your WooCommerce products
Using the built-in WooCommere hooks, Custom
Fields can be added to any of the standard
WooCommerce product panels (incl. variable
products)
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>';
}
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' )
)
);
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',
),
)
);
Adding New Fields - Textarea
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'My Textarea', 'mytheme' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( 'Some helpful tooltip.', 'mytheme' ),
)
);
Adding New Fields - Select
// Select
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'My Select Field', 'mytheme' ),
'options' => array(
'one' => __( 'Option 1', 'mytheme' ),
'two' => __( 'Option 2', 'mytheme' ),
'three' => __( 'Option 3', 'mytheme' ),
),
)
);
Adding New Fields - Checkbox
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_checkbox',
'wrapper_class' => 'show_if_simple',
'label' => __('My Checkbox Field', 'mytheme' ),
'description' => __( 'Check me!', 'mytheme' )
)
);
Adding New Fields - Hidden
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_hidden_field',
'value' => 'hidden_value'
)
);
Adding New Fields - Custom Fields
// Custom field Type
?>
<p class="form-field custom_field_type">
<label for="custom_field_type">
<?php echo __( 'Custom Field Type', 'mytheme' ); ?>
</label>
<span class="wrap">
<?php $custom_field_type = get_post_meta( $post->ID, '_custom_field_type', true ); ?>
<input placeholder="<?php _e( 'Field One', 'mytheme' ); ?>" type="number" ...
<input placeholder="<?php _e( 'Field Two', 'mytheme' ); ?>" type="number" ...
</span>
<span class="description">
<?php _e( 'Your description here!', 'mytheme' ); ?>
</span>
</p>
<?php
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' );
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' );
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' );
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' );
Add Fields to Product Panel - Linked Products
// After all Linked Products default fields
add_action( 'woocommerce_product_options_related', 'mytheme_woo_add_custom_fields' );
Add Fields to Product Panel - Attributes Panel
// After all Attributes default fields
add_action( 'woocommerce_product_options_attributes', 'mytheme_woo_add_custom_fields' );
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' );
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' );
Saving our Fields
// Text Field
$woocommerce_text_field = $_POST['_text_field'];
update_post_meta( $post_id, '_text_field', esc_attr($woocommerce_text_field) );
// Number Field
$woocommerce_number_field = $_POST['_number_field'];
update_post_meta( $post_id, '_number_field', esc_attr($woocommerce_number_field) );
// Textarea
$woocommerce_textarea = $_POST['_textarea'];
update_post_meta( $post_id, '_textarea', esc_html($woocommerce_textarea) );
// Select
$woocommerce_select = $_POST['_select'];
update_post_meta( $post_id, '_select', esc_attr($woocommerce_select) );
Saving our Fields
// Checkbox
$woocommerce_checkbox = isset($_POST['_checkbox']) ? 'yes' : 'no';
update_post_meta( $post_id, '_checkbox', $woocommerce_checkbox );
// Hidden Field
$woocommerce_hidden_field = $_POST['_hidden_field'];
update_post_meta( $post_id, '_hidden_field', esc_attr($woocommerce_hidden_field) );
// Custom Field
$custom_field_type = array( esc_attr($_POST['_field_one']), esc_attr($_POST['_field_two']) );
update_post_meta( $post_id, '_custom_field_type', $custom_field_type );
Adding New Fields to Variable Products
function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_variable_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'mytheme' ),
'placeholder' => 'http://',
'desc_tip' => true,
'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ),
'value' => get_post_meta( $variation->ID, '_variable_text_field', true )
)
);
echo '</div>';
}
Adding New Fields to Variable Products
function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_variable_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'mytheme' ),
'placeholder' => 'http://',
'desc_tip' => true,
'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ),
'value' => get_post_meta( $variation->ID, '_variable_text_field', true )
)
);
echo '</div>';
}
Adding New Fields to Variable Products
function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_variable_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'mytheme' ),
'placeholder' => 'http://',
'desc_tip' => true,
'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ),
'value' => get_post_meta( $variation->ID, '_variable_text_field', true )
)
);
echo '</div>';
}
Adding New Fields to Variable Products
function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_variable_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'mytheme' ),
'placeholder' => 'http://',
'desc_tip' => true,
'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ),
'value' => get_post_meta( $variation->ID, '_variable_text_field', true )
)
);
echo '</div>';
}
Adding New Fields to Variable Products
function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_variable_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'mytheme' ),
'placeholder' => 'http://',
'desc_tip' => true,
'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ),
'value' => get_post_meta( $variation->ID, '_variable_text_field', true ),
)
);
echo '</div>';
}
Add Fields to Product Panel - Variations Panel
// After variation Enabled/Downloadable/Virtual/Manage Stock checkboxes
add_action( 'woocommerce_variation_options', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
// After Price fields
add_action( 'woocommerce_variation_options_pricing', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
// After Manage Stock fields
add_action( 'woocommerce_variation_options_inventory', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
// After Weight/Dimension fields
add_action( 'woocommerce_variation_options_dimensions', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
// After Shipping/Tax Class fields
add_action( 'woocommerce_variation_options_tax', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
// After Download fields
add_action( 'woocommerce_variation_options_download', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
// After all Variation fields
add_action( 'woocommerce_product_after_variable_attributes', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
Saving our Fields - Variable Products
// Save Fields
function mytheme_woo_add_custom_variation_fields_save( $post_id ){
// Text Field
$woocommerce_text_field = $_POST['_variable_text_field'][ $post_id ];
update_post_meta( $post_id, '_variable_text_field', esc_attr( $woocommerce_text_field ) );
}
add_action( 'woocommerce_save_product_variation', 'mytheme_woo_add_custom_variation_fields_save', 10, 2 );
Using our Fields
// Display our Product custom fields above the summary on the Single Product Page
function mytheme_display_woo_custom_fields() {
global $post;
$ourTextField = get_post_meta( $post->ID, '_text_field', true );
if ( !empty( $ourTextField ) ) {
echo '<div>Our Text Field: ' . $ourTextField . '</div>';
}
}
add_action( 'woocommerce_single_product_summary', 'mytheme_display_woo_custom_fields', 15 );
Resources
// Example Code
https://maddisondesigns.com/woocommerce-custom-fields-wpmelb
// WooCommerce Action and Filter Hook Reference
https://docs.woocommerce.com/wc-apidocs/hook-docs.html
// WordPress Hooks
https://developer.wordpress.org/plugins/hooks
I’m Anthony Hortin
You can find me here
@maddisondesigns
maddisondesigns.com
@easywpguide
easywpguide.com
Thanks!
Questions?

More Related Content

What's hot (6)

PPT
Les20
Vijay Kumar
 
PDF
날로 먹는 Django admin 활용
KyeongMook "Kay" Cha
 
PDF
50 Laravel Tricks in 50 Minutes
Azim Kurt
 
PPT
Drupal Form Api
Amit Kumar Singh
 
PPTX
Editing the Visual Editor (WordPress)
Jake Goldman
 
PDF
Instant Dynamic Forms with #states
Konstantin Käfer
 
날로 먹는 Django admin 활용
KyeongMook "Kay" Cha
 
50 Laravel Tricks in 50 Minutes
Azim Kurt
 
Drupal Form Api
Amit Kumar Singh
 
Editing the Visual Editor (WordPress)
Jake Goldman
 
Instant Dynamic Forms with #states
Konstantin Käfer
 

Similar to Working with WooCommerce Custom Fields (20)

PDF
WooCommerce: Custom Fields
Rodolfo Melogli
 
PPTX
Wordpress WooCommerce
Northern Initiatives
 
PDF
Stepping Into Custom Post Types
K.Adam White
 
PDF
Woo Variable Products
SO Marketing
 
PPTX
Styling WooCommerce
Becky Davis
 
PPTX
Woocommerce 101
Laura Hartwig
 
PDF
Developing For The WordPress Customizer
Anthony Hortin
 
PDF
Wordpress plugin development from Scratch
Ocaka Alfred
 
PDF
Getting Started with WooCommerce
Anthony Hortin
 
PDF
Developing for the WordPress Customizer
Anthony Hortin
 
PDF
Shopify Partner Social
The Working Party
 
PPTX
WooCommerce: custom fields
Rodolfo Melogli
 
PPTX
Custom Registration Fields for Magento 2
Webkul Software Pvt. Ltd.
 
PDF
Customizing WooCommerce
Brandon Yanofsky
 
PPTX
WP 201 Custom Post Types - Custom Fields - WordCamp Columbus 2015
Joe Querin
 
PDF
WordCamp Bristol 2019 - WordPress custom theme building
Jonny Allbut
 
PPTX
Wordpress workflow for an agency world
Chris Lowe
 
PPTX
WordCamp Kent 2019 - WP 101: Custom Post Type & Custom Fields
Joe Querin
 
PDF
E book
KinzaKhan71
 
PDF
E book
KinzaKhan71
 
WooCommerce: Custom Fields
Rodolfo Melogli
 
Wordpress WooCommerce
Northern Initiatives
 
Stepping Into Custom Post Types
K.Adam White
 
Woo Variable Products
SO Marketing
 
Styling WooCommerce
Becky Davis
 
Woocommerce 101
Laura Hartwig
 
Developing For The WordPress Customizer
Anthony Hortin
 
Wordpress plugin development from Scratch
Ocaka Alfred
 
Getting Started with WooCommerce
Anthony Hortin
 
Developing for the WordPress Customizer
Anthony Hortin
 
Shopify Partner Social
The Working Party
 
WooCommerce: custom fields
Rodolfo Melogli
 
Custom Registration Fields for Magento 2
Webkul Software Pvt. Ltd.
 
Customizing WooCommerce
Brandon Yanofsky
 
WP 201 Custom Post Types - Custom Fields - WordCamp Columbus 2015
Joe Querin
 
WordCamp Bristol 2019 - WordPress custom theme building
Jonny Allbut
 
Wordpress workflow for an agency world
Chris Lowe
 
WordCamp Kent 2019 - WP 101: Custom Post Type & Custom Fields
Joe Querin
 
E book
KinzaKhan71
 
E book
KinzaKhan71
 
Ad

More from Anthony Hortin (18)

PDF
Why you should be using WordPress child themes
Anthony Hortin
 
PDF
WordPress Gutenberg
Anthony Hortin
 
PDF
Introduction to Advanced Custom Fields
Anthony Hortin
 
PDF
The Why, When, How of WordPress Child Themes
Anthony Hortin
 
PDF
Essential plugins for your WordPress Website
Anthony Hortin
 
PDF
Building a Membership Site with WooCommerce Memberships
Anthony Hortin
 
PDF
Building a Membership Site with WooCommerce
Anthony Hortin
 
PDF
Getting to Grips with Firebug
Anthony Hortin
 
PDF
Getting to Know WordPress May 2015
Anthony Hortin
 
PDF
25 WordPress Plugins to Complement Your Site
Anthony Hortin
 
PDF
WordCamp San Francisco & WooCommerce Conference Recap
Anthony Hortin
 
PDF
Creating a multilingual site with WPML
Anthony Hortin
 
PDF
WordPress Visual Editor Mastery
Anthony Hortin
 
PDF
Getting to know WordPress
Anthony Hortin
 
PDF
Do's & Don'ts for WordPress Theme Development
Anthony Hortin
 
PDF
Submitting to the WordPress Theme Directory
Anthony Hortin
 
PDF
WordPress Queries - the right way
Anthony Hortin
 
PDF
Getting to grips with firebug
Anthony Hortin
 
Why you should be using WordPress child themes
Anthony Hortin
 
WordPress Gutenberg
Anthony Hortin
 
Introduction to Advanced Custom Fields
Anthony Hortin
 
The Why, When, How of WordPress Child Themes
Anthony Hortin
 
Essential plugins for your WordPress Website
Anthony Hortin
 
Building a Membership Site with WooCommerce Memberships
Anthony Hortin
 
Building a Membership Site with WooCommerce
Anthony Hortin
 
Getting to Grips with Firebug
Anthony Hortin
 
Getting to Know WordPress May 2015
Anthony Hortin
 
25 WordPress Plugins to Complement Your Site
Anthony Hortin
 
WordCamp San Francisco & WooCommerce Conference Recap
Anthony Hortin
 
Creating a multilingual site with WPML
Anthony Hortin
 
WordPress Visual Editor Mastery
Anthony Hortin
 
Getting to know WordPress
Anthony Hortin
 
Do's & Don'ts for WordPress Theme Development
Anthony Hortin
 
Submitting to the WordPress Theme Directory
Anthony Hortin
 
WordPress Queries - the right way
Anthony Hortin
 
Getting to grips with firebug
Anthony Hortin
 
Ad

Recently uploaded (20)

PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
PDF
DevOps Design for different deployment options
henrymails
 
PPTX
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
PPTX
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
PDF
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
PPT
Computer Securityyyyyyyy - Chapter 2.ppt
SolomonSB
 
PPTX
Research Design - Report on seminar in thesis writing. PPTX
arvielobos1
 
PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PDF
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
PDF
Web Hosting for Shopify WooCommerce etc.
Harry_Phoneix Harry_Phoneix
 
PPTX
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
PPTX
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
PPTX
本科硕士学历佛罗里达大学毕业证(UF毕业证书)24小时在线办理
Taqyea
 
PPT
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
PPTX
西班牙武康大学毕业证书{UCAMOfferUCAM成绩单水印}原版制作
Taqyea
 
PPTX
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
一比一原版(LaTech毕业证)路易斯安那理工大学毕业证如何办理
Taqyea
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
DevOps Design for different deployment options
henrymails
 
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
𝐁𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓
hokimamad0
 
Computer Securityyyyyyyy - Chapter 2.ppt
SolomonSB
 
Research Design - Report on seminar in thesis writing. PPTX
arvielobos1
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
Web Hosting for Shopify WooCommerce etc.
Harry_Phoneix Harry_Phoneix
 
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
本科硕士学历佛罗里达大学毕业证(UF毕业证书)24小时在线办理
Taqyea
 
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
西班牙武康大学毕业证书{UCAMOfferUCAM成绩单水印}原版制作
Taqyea
 
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
一比一原版(LaTech毕业证)路易斯安那理工大学毕业证如何办理
Taqyea
 

Working with WooCommerce Custom Fields

  • 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', ), ) );
  • 7. Adding New Fields - Textarea // Textarea woocommerce_wp_textarea_input( array( 'id' => '_textarea', 'label' => __( 'My Textarea', 'mytheme' ), 'placeholder' => '', 'desc_tip' => true, 'description' => __( 'Some helpful tooltip.', 'mytheme' ), ) );
  • 8. Adding New Fields - Select // Select woocommerce_wp_select( array( 'id' => '_select', 'label' => __( 'My Select Field', 'mytheme' ), 'options' => array( 'one' => __( 'Option 1', 'mytheme' ), 'two' => __( 'Option 2', 'mytheme' ), 'three' => __( 'Option 3', 'mytheme' ), ), ) );
  • 9. Adding New Fields - Checkbox // Checkbox woocommerce_wp_checkbox( array( 'id' => '_checkbox', 'wrapper_class' => 'show_if_simple', 'label' => __('My Checkbox Field', 'mytheme' ), 'description' => __( 'Check me!', 'mytheme' ) ) );
  • 10. Adding New Fields - Hidden // Hidden field woocommerce_wp_hidden_input( array( 'id' => '_hidden_field', 'value' => 'hidden_value' ) );
  • 11. Adding New Fields - Custom Fields // Custom field Type ?> <p class="form-field custom_field_type"> <label for="custom_field_type"> <?php echo __( 'Custom Field Type', 'mytheme' ); ?> </label> <span class="wrap"> <?php $custom_field_type = get_post_meta( $post->ID, '_custom_field_type', true ); ?> <input placeholder="<?php _e( 'Field One', 'mytheme' ); ?>" type="number" ... <input placeholder="<?php _e( 'Field Two', 'mytheme' ); ?>" type="number" ... </span> <span class="description"> <?php _e( 'Your description here!', 'mytheme' ); ?> </span> </p> <?php
  • 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' );
  • 20. Saving our Fields // Text Field $woocommerce_text_field = $_POST['_text_field']; update_post_meta( $post_id, '_text_field', esc_attr($woocommerce_text_field) ); // Number Field $woocommerce_number_field = $_POST['_number_field']; update_post_meta( $post_id, '_number_field', esc_attr($woocommerce_number_field) ); // Textarea $woocommerce_textarea = $_POST['_textarea']; update_post_meta( $post_id, '_textarea', esc_html($woocommerce_textarea) ); // Select $woocommerce_select = $_POST['_select']; update_post_meta( $post_id, '_select', esc_attr($woocommerce_select) );
  • 21. Saving our Fields // Checkbox $woocommerce_checkbox = isset($_POST['_checkbox']) ? 'yes' : 'no'; update_post_meta( $post_id, '_checkbox', $woocommerce_checkbox ); // Hidden Field $woocommerce_hidden_field = $_POST['_hidden_field']; update_post_meta( $post_id, '_hidden_field', esc_attr($woocommerce_hidden_field) ); // Custom Field $custom_field_type = array( esc_attr($_POST['_field_one']), esc_attr($_POST['_field_two']) ); update_post_meta( $post_id, '_custom_field_type', $custom_field_type );
  • 22. Adding New Fields to Variable Products function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) { echo '<div class="options_group form-row form-row-full">'; // Text Field woocommerce_wp_text_input( array( 'id' => '_variable_text_field[' . $variation->ID . ']', 'label' => __( 'My Text Field', 'mytheme' ), 'placeholder' => 'http://', 'desc_tip' => true, 'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ), 'value' => get_post_meta( $variation->ID, '_variable_text_field', true ) ) ); echo '</div>'; }
  • 23. Adding New Fields to Variable Products function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) { echo '<div class="options_group form-row form-row-full">'; // Text Field woocommerce_wp_text_input( array( 'id' => '_variable_text_field[' . $variation->ID . ']', 'label' => __( 'My Text Field', 'mytheme' ), 'placeholder' => 'http://', 'desc_tip' => true, 'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ), 'value' => get_post_meta( $variation->ID, '_variable_text_field', true ) ) ); echo '</div>'; }
  • 24. Adding New Fields to Variable Products function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) { echo '<div class="options_group form-row form-row-full">'; // Text Field woocommerce_wp_text_input( array( 'id' => '_variable_text_field[' . $variation->ID . ']', 'label' => __( 'My Text Field', 'mytheme' ), 'placeholder' => 'http://', 'desc_tip' => true, 'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ), 'value' => get_post_meta( $variation->ID, '_variable_text_field', true ) ) ); echo '</div>'; }
  • 25. Adding New Fields to Variable Products function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) { echo '<div class="options_group form-row form-row-full">'; // Text Field woocommerce_wp_text_input( array( 'id' => '_variable_text_field[' . $variation->ID . ']', 'label' => __( 'My Text Field', 'mytheme' ), 'placeholder' => 'http://', 'desc_tip' => true, 'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ), 'value' => get_post_meta( $variation->ID, '_variable_text_field', true ) ) ); echo '</div>'; }
  • 26. Adding New Fields to Variable Products function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) { echo '<div class="options_group form-row form-row-full">'; // Text Field woocommerce_wp_text_input( array( 'id' => '_variable_text_field[' . $variation->ID . ']', 'label' => __( 'My Text Field', 'mytheme' ), 'placeholder' => 'http://', 'desc_tip' => true, 'description' => __( 'Here's some helpful tooltip text.', 'mytheme' ), 'value' => get_post_meta( $variation->ID, '_variable_text_field', true ), ) ); echo '</div>'; }
  • 27. Add Fields to Product Panel - Variations Panel // After variation Enabled/Downloadable/Virtual/Manage Stock checkboxes add_action( 'woocommerce_variation_options', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Price fields add_action( 'woocommerce_variation_options_pricing', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Manage Stock fields add_action( 'woocommerce_variation_options_inventory', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Weight/Dimension fields add_action( 'woocommerce_variation_options_dimensions', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Shipping/Tax Class fields add_action( 'woocommerce_variation_options_tax', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Download fields add_action( 'woocommerce_variation_options_download', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After all Variation fields add_action( 'woocommerce_product_after_variable_attributes', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
  • 28. Saving our Fields - Variable Products // Save Fields function mytheme_woo_add_custom_variation_fields_save( $post_id ){ // Text Field $woocommerce_text_field = $_POST['_variable_text_field'][ $post_id ]; update_post_meta( $post_id, '_variable_text_field', esc_attr( $woocommerce_text_field ) ); } add_action( 'woocommerce_save_product_variation', 'mytheme_woo_add_custom_variation_fields_save', 10, 2 );
  • 29. Using our Fields // Display our Product custom fields above the summary on the Single Product Page function mytheme_display_woo_custom_fields() { global $post; $ourTextField = get_post_meta( $post->ID, '_text_field', true ); if ( !empty( $ourTextField ) ) { echo '<div>Our Text Field: ' . $ourTextField . '</div>'; } } add_action( 'woocommerce_single_product_summary', 'mytheme_display_woo_custom_fields', 15 );
  • 30. Resources // Example Code https://maddisondesigns.com/woocommerce-custom-fields-wpmelb // WooCommerce Action and Filter Hook Reference https://docs.woocommerce.com/wc-apidocs/hook-docs.html // WordPress Hooks https://developer.wordpress.org/plugins/hooks
  • 31. I’m Anthony Hortin You can find me here @maddisondesigns maddisondesigns.com @easywpguide easywpguide.com Thanks! Questions?