Questionshow to add custom fields to any WooCommerce products
mmWebetutorial Staff asked 6 months ago

The hooks for add custom fields to any WooCommerce products:-
The first step is to hook an action to woocommerce_product_options_general_product_data. The function linked to this hook will be responsible of the new fields display.

A second hook will be taken into account to save fields values: woocommerce_process_product_meta.
Adding New Fields:
To create our fields use WooCommerce builtin function (All these functions are located in WooCommerce/Admin/WritePanels/writepanels-init.php.):

  • woocommerce_wp_text_input()
  • woocommerce_wp_textarea_input()
  • woocommerce_wp_select()
  • woocommerce_wp_checkbox()
  • woocommerce_wp_hidden_input()

Text Field Type:

// Text Field

woocommerce_wp_text_input(

array(

‘id’ => ‘_text_field’,

‘label’ => __( ‘My Text Field’, ‘woocommerce’ ),

‘placeholder’ => ”,

‘desc_tip’ => ‘true’,

‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )

)

);

Textarea Field Type:

// Textarea
woocommerce_wp_textarea_input(
array(
‘id’          => ‘_textarea’,
‘label’       => __( ‘My Textarea’, ‘woocommerce’ ),
‘placeholder’ => ”,
‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
)
);

Dropdown Select Field Type:

// Select
woocommerce_wp_select(
array(
‘id’      => ‘_select’,
‘label’   => __( ‘My Select Field’, ‘woocommerce’ ),
‘options’ => array(
‘one’   => __( ‘Option 1’, ‘woocommerce’ ),
‘two’   => __( ‘Option 2’, ‘woocommerce’ ),
‘three’ => __( ‘Option 3’, ‘woocommerce’ )
)
)
);

Checkbox Field Type:

// Checkbox
woocommerce_wp_checkbox(
array(
‘id’            => ‘_checkbox’,
‘wrapper_class’ => ‘show_if_simple’,
‘label’         => __(‘My Checkbox Field’, ‘woocommerce’ ),
‘description’   => __( ‘Check me!’, ‘woocommerce’ )
)
);

Hidden Field Type:

// Hidden field
woocommerce_wp_hidden_input(
array(
‘id’    => ‘_hidden_field’,
‘value’ => ‘hidden_value’
)
);

Products Select Field Type:

// Product Select
?>
<p class=”form-field product_field_type”>
<label for=”product_field_type”><?php _e( ‘Product Select’, ‘woocommerce’ ); ?></label>
<select id=”product_field_type” name=”product_field_type[]” class=”ajax_chosen_select_products” multiple=”multiple” data-placeholder=”<?php _e( ‘Search for a product&hellip;’, ‘woocommerce’ ); ?>”>
<?php
$product_field_type_ids = get_post_meta( $post->ID, ‘_product_field_type_ids’, true );
$product_ids = ! empty( $product_field_type_ids ) ? array_map( ‘absint’,  $product_field_type_ids ) : null;
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product      = get_product( $product_id );
$product_name = woocommerce_get_formatted_product_name( $product );
echo ‘<option value=”‘ . esc_attr( $product_id ) . ‘” selected=”selected”>’ . esc_html( $product_name ) . ‘</option>’;
}
}
?>
</select> <img class=”help_tip” data-tip='<?php _e( ‘Your description here’, ‘woocommerce’ ) ?>’ src=”<?php echo $woocommerce->plugin_url(); ?>/assets/help.jpg” height=”16″ width=”16″ />
</p>

Saving Fields Values:

function woo_add_custom_general_fields_save( $post_id ){

// Text Field
$woocommerce_text_field = $_POST[‘_text_field’];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, ‘_text_field’, esc_attr( $woocommerce_text_field ) );

// Textarea
$woocommerce_textarea = $_POST[‘_textarea’];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, ‘_textarea’, esc_html( $woocommerce_textarea ) );

// Select
$woocommerce_select = $_POST[‘_select’];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, ‘_select’, esc_attr( $woocommerce_select ) );

// Checkbox
$woocommerce_checkbox = isset( $_POST[‘_checkbox’] ) ? ‘yes’ : ‘no’;
update_post_meta( $post_id, ‘_checkbox’, $woocommerce_checkbox );

// Hidden Field
$woocommerce_hidden_field = $_POST[‘_hidden_field’];
if( !empty( $woocommerce_hidden_field ) )
update_post_meta( $post_id, ‘_hidden_field’, esc_attr( $woocommerce_hidden_field ) );

// Product Field Type
$product_field_type =  $_POST[‘product_field_type’];
update_post_meta( $post_id, ‘_product_field_type_ids’, $product_field_type );

}

Your Answer
Login
Enter email and password to comment or answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.