mercredi 21 avril 2021

WooCommerce admin product custom checkbox field not saving value

i created custom checkbox on product options, but stuck in saving data. I tried all possible variants, but without success. Where i am going wrong? thanks

This is code:

add_action( 'woocommerce_product_options_sku', 'custom_checkbox_field' );
function custom_checkbox_field(){
    global $post, $product_object;

    if ( ! is_a( $product_object, 'WC_Product' ) ) {
            $product_object = wc_get_product( $post->ID );
    }
      
            
    woocommerce_wp_checkbox(
    array(
        'id'            => 'custom_checkbox_field',
        'value'         => empty($values) ? 'yes' : $values,
        'label'         => __( 'Label', 'woocommerce' ),
        'description'   => __( 'Description', 'woocommerce' ),
        )
    );
    
}

add_action( 'woocommerce_process_product_meta', 'save_custom_field' );  
    function save_custom_field( $post_id ) {
    

     // grab the product
     $product = wc_get_product( $post_id );

      // save data
      $product->update_meta_data( 'custom_checkbox_field', isset($_POST['custom_checkbox_field']) ? 'yes' : 'no' ); 
      $product->save();
    }



Aucun commentaire:

Enregistrer un commentaire