【问题标题】:Add checkbox to WooCommerce product advanced options tab将复选框添加到 WooCommerce 产品高级选项选项卡
【发布时间】:2021-02-19 10:12:13
【问题描述】:

我正在尝试在 WooCommerce 的产品页面中添加一个复选框。它被用来判断产品是否需要在结帐时签署发布表格。

这是在子主题中完成的。该复选框显示在我想要的产品的高级选项卡中,但它没有被保存。

这是来自我的functions.php 的代码,用于创建复选框并保存它。

function add_release_checkbox() {
  $args = array(
    'label' => __('Release Form Required','woocommerce'), // Text in the editor label
    'value' => get_post_meta(get_the_id(), 'release_req', true), // meta_value where the id serves as meta_key
    'id' => 'release_req', // required, it's the meta_key for storing the value (is checked or not)
    'name' => __('Release Required','woocommerce'),
    'cbvalue' => true, // "value" attribute for the checkbox
    'desc_tip' => true, // true or false, show description directly or as tooltip
    'description' => __('Tells if a release form is required for this product.','woocommerce') // provide something useful here
  );
  woocommerce_wp_checkbox( $args );
}

add_action( 'woocommerce_product_options_advanced', 'add_release_checkbox' );

function save_release_meta($post_id) {
    write_log($_POST); // for debugging purposes write the post to the debug.log file
    $release = isset($_POST['release_req']) ? $_POST['release_req'] : '0';
    $product = wc_get_product($post_id);
    $product->update_meta_data('release_req', $release);
    $product->save();
}
add_action('woocommerce_process_product_meta', 'save_release_meta');

我把它做成一个文本框,它可以工作,所以问题是我做错了什么?即使我选中或取消选中它也不会保存。

【问题讨论】:

    标签: php wordpress woocommerce backend product


    【解决方案1】:

    这不起作用的原因是您在代码中使用了以下内容

    'name' => __('Release Required','woocommerce'),
    

    名称不必是可翻译的,并且以错误的方式应用


    低于有效答案。我使用woocommerce_admin_process_product_object 保存而不是过时的woocommerce_process_product_meta 挂钩

    function add_release_checkbox() {
        // Add checkbox
        $args = array(
            'label' => __( 'Release Form Required','woocommerce' ), // Text in the editor label
            'id' => 'release_req', // required, it's the meta_key for storing the value (is checked or not)
            'desc_tip' => true, // true or false, show description directly or as tooltip
            'description' => __('Tells if a release form is required for this product.','woocommerce') // provide something useful here
        );
        woocommerce_wp_checkbox( $args );
    }
    
    add_action( 'woocommerce_product_options_advanced', 'add_release_checkbox' );
    
    // Save Field
    function action_woocommerce_admin_process_product_object( $product ) {
        // Isset, yes or no
        $checkbox = isset( $_POST['release_req'] ) ? 'yes' : 'no';
    
        // Update meta
        $product->update_meta_data( 'release_req', $checkbox );
    }
    add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 ); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      相关资源
      最近更新 更多