jeudi 17 mai 2018

Checkbox custom fields for admin edit product in Woocommerce

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?




Aucun commentaire:

Enregistrer un commentaire