将复选框添加到WooCommerce中的产品库存选项卡,并在默认情况下选中该复选框 [英] Add checkbox to product inventory tab in WooCommerce and have the checkbox checked by default

查看:25
本文介绍了将复选框添加到WooCommerce中的产品库存选项卡,并在默认情况下选中该复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在here中获得此代码段以添加自动设置的复选框自定义域,它工作正常。

// 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 class="options_group">';

  $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');
}

我的问题:如何将此复选框移动到"清单"选项卡上并在默认情况下选中该复选框?

我已尝试更改:

add_action( 'woocommerce_product_options_pricing', 'add_custom_field_product_options_pricing' );

收件人:

add_action( 'woocommerce_product_options_inventory_product_data', 'add_custom_field_product_options_pricing' );

add_action( 'woocommerce_admin_process_product_object', 'save_custom_field_product_options_pricing' );

收件人:

add_action( 'woocommerce_process_product_meta', 'save_custom_field_product_options_pricing' );

但无济于事。有什么建议吗?

推荐答案

woocommerce_admin_process_product_object替换了过时的woocommerce_process_product_meta挂接,因此您绝对不应该用它替换它

要在默认情况下选中该复选框,您可以将value添加到woocommerce_wp_checkbox()中的参数

因此您得到:

// 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 );

这篇关于将复选框添加到WooCommerce中的产品库存选项卡,并在默认情况下选中该复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆