Teyki Teyki
Teyki Teyki

Reputation: 21

Add custom field woocommerce (variable product)

I search how can i add a custom field for a viriable product in woocommerce. I already do this, but is only work for simple product.

add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
    // Print a custom text field
    woocommerce_wp_text_input( array(
        'id' => '_custom_text_field',
        'label' => 'Custom Text Field',
        'description' => 'This is a custom field, you can write here anything you want.',
        'desc_tip' => 'true',
        'placeholder' => 'Custom text'
    ) );
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields( $post_id ) {
    if ( ! empty( $_POST['_custom_text_field'] ) ) {
        update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
    }
}

How can I edit this for have it for variable product?

Upvotes: 2

Views: 1933

Answers (2)

Alvilog
Alvilog

Reputation: 11

Need use that action

add_action( 'woocommerce_product_after_variable_attributes', 'wc_custom_add_custom_fields', 10, 3 );

Upvotes: 1

mattkrupnik
mattkrupnik

Reputation: 537

Just change

add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );

to

add_action( 'woocommerce_product_options_inventory_product_data', 'wc_custom_add_custom_fields' );

and your custom field will be in inventory tab

Upvotes: 2

Related Questions