Home > other >  Add checkbox to product inventory tab in WooCommerce and have the checkbox checked by default
Add checkbox to product inventory tab in WooCommerce and have the checkbox checked by default

Time:01-15

I got this snippet in here to add checkbox custom field which is auto set and it’s working fine.

// Displaying quantity setting fields on admin product pages
add_action( 'woocommerce_product_options_pricing', 'add_custom_field_product_options_pricing' );
function add_custom_field_product_options_pricing() {
  global $product_object;

  echo '</div><div >';

  $values = $product_object->get_meta('_cutom_meta_key');

   woocommerce_wp_checkbox( array( // Checkbox.
    'id'            => '_cutom_meta_key',
    'label'         => __( 'Custom label', 'woocommerce' ),
    'value'         => empty($values) ? 'yes' : $values,
    'description'   => __( 'Enable this to make something.', 'woocommerce' ),
   ) );
}

// Save quantity setting fields values
add_action( 'woocommerce_admin_process_product_object', 'save_custom_field_product_options_pricing' );
function save_custom_field_product_options_pricing( $product ) {
$product->update_meta_data( '_cutom_meta_key', isset($_POST['_cutom_meta_key']) ? 'yes' : 'no');
}

My question: how to move this checkbox to be on the inventory tab and have the checkbox checked by default?

I’ve tried changing :

add_action( 'woocommerce_product_options_pricing', 'add_custom_field_product_options_pricing' );

to:

add_action( 'woocommerce_product_options_inventory_product_data', 'add_custom_field_product_options_pricing' );

AND

add_action( 'woocommerce_admin_process_product_object', 'save_custom_field_product_options_pricing' );

to:

add_action( 'woocommerce_process_product_meta', 'save_custom_field_product_options_pricing' );

But to no avail. Any advice?

CodePudding user response:

woocommerce_admin_process_product_object replaces the outdated woocommerce_process_product_meta hook so you should definitely not replace it with it

To have the checkbox checked by default you can add value to the args from woocommerce_wp_checkbox()

So you get:

// Add checkbox
function action_woocommerce_product_options_inventory_product_data() {
    global $product_object;
    
    // Get meta
    $value = $product_object->get_meta( '_cutom_meta_key' );
    
    // Checkbox
    woocommerce_wp_checkbox( array( 
        'id'            => '_cutom_meta_key', // Required, it's the meta_key for storing the value (is checked or not)
        'label'         => __( 'Custom label', 'woocommerce' ), // Text in the editor label
        'desc_tip'      => false, // true or false, show description directly or as tooltip
        'description'   => __( 'Enable this to make something', 'woocommerce' ), // Provide something useful here
        'value'         => empty( $value ) ? 'yes' : $value // Checked by default
    ) );
}
add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_product_options_inventory_product_data', 10, 0 );
        
// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
    // Update meta
    $product->update_meta_data( '_cutom_meta_key', isset( $_POST['_cutom_meta_key'] ) ? 'yes' : 'no' );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
  •  Tags:  
  • Related