mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-05-01 11:52:25 +08:00
https://stackoverflow.com/questions/70708930/add-checkbox-to-product-inventory-tab-in-woocommerce-and-have-the-checkbox-check/70709915
24 lines
1.2 KiB
Text
24 lines
1.2 KiB
Text
// 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 );
|