Home Checkbox custom fields for admin edit product in Woocommerce
Reply: 0

Checkbox custom fields for admin edit product in Woocommerce

user1015
1#
user1015 Published in May 27, 2018, 3:32 am

I'm trying to add checkbox to settings tab in WooCommerce (in admin panel) and use this code:

add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {    
    global $post;
    woocommerce_wp_checkbox(array( 
        'id'            => 'is_gift', 
        'label'         => __('Gift', 'woocommerce' ), 
        'description'   => __( 'Add gift label', 'woocommerce' ),
        'value'         => get_post_meta($post->ID, 'is_gift', true) 
    ));
}

add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields() {
    global $post;
    if (!empty($_POST['is_gift'])) {
        update_post_meta( $post->ID, 'is_gift', esc_attr( $_POST['is_gift'] ) );       
    }
}

This code showing checkbox, but not saving changes. It works only for one product. I guess something wrong with $post->ID?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.360693 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO