2

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Bogdan
  • 342
  • 5
  • 13
  • Please provide screenshot, where you want to display it. – PPL May 17 '18 at 07:44
  • Is this on a single product? are you sure you are using the right hook? – Stender May 17 '18 at 07:50
  • please refer this link : https://stackoverflow.com/questions/47463330/custom-checkbox-in-product-settings-that-displays-a-custom-field-when-checked?answertab=active#tab-top – raju_odi May 17 '18 at 07:52

2 Answers2

5

Updated … Try this instead:

add_action( 'woocommerce_product_options_general_product_data', 'wc_custom_add_custom_fields' );
function wc_custom_add_custom_fields() {
    global $post;

   $input_checkbox = get_post_meta( $post->ID, 'is_gift', true );
   if( empty( $input_checkbox ) ) $input_checkbox = '';

    woocommerce_wp_checkbox(array(
        'id'            => 'is_gift',
        'label'         => __('Gift', 'woocommerce' ),
        'description'   => __( 'Add gift label', 'woocommerce' ),
        'value'         => $input_checkbox,
    ));
}

add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields($post_id) {
    $_custom_text_option = isset( $_POST['is_gift'] ) ? 'yes' : '';
    update_post_meta( $post_id, 'is_gift', $_custom_text_option );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0

try this

    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    function woo_add_custom_general_fields() {

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

    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

    function woo_add_custom_general_fields_save( $post_id ){

        // Checkbox
        $_checkbox = $_POST['is_gift'];
        if (isset( $_checkbox )){
        update_post_meta( $post_id, '_is_gift', $_checkbox );
        }
    }
Vaibhav Kumar
  • 768
  • 4
  • 12