简体   繁体   English

在Woocommerce后端产品编辑页面中添加复选框到产品类型选项

[英]Add checkbox to product type option in Woocommerce backend product edit pages

I have added a custom option checkbox in Woocommerce admin product data settings. 我在Woocommerce管理产品数据设置中添加了自定义选项复选框。 If I enable that checkbox and save the changes, the value is correctly saved in product meta data, but the checkbox never stay checked . 如果我启用该复选框并保存更改,则该值会正确保存在产品元数据中, 但复选框永远不会保持选中状态

What I am doing wrong? 我做错了什么? How to get this working as other option checkboxes? 如何使这个工作作为其他选项复选框?

My code: 我的代码:

function add_e_visa_product_option( $product_type_options ) {
    $product_type_options[''] = array(
        'id'            => '_evisa',
        'wrapper_class' => 'show_if_simple show_if_variable',
        'label'         => __( 'eVisa', 'woocommerce' ),
        'description'   => __( '', 'woocommerce' ),
        'default'       => 'no'
    );
    return $product_type_options;
}
add_filter( 'product_type_options', 'add_e_visa_product_option' );

function save_evisa_option_fields( $post_id ) {
  $is_e_visa = isset( $_POST['_evisa'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_evisa', $is_e_visa );
}
add_action( 'woocommerce_process_product_meta_simple', 'save_evisa_option_fields'  );
add_action( 'woocommerce_process_product_meta_variable', 'save_evisa_option_fields'  );

The answer is very simple… you just forgot to add a key id for your array in the first function, like: 答案很简单......你只是忘了在第一个函数中为你的数组添加一个键ID,如:

$product_type_options['evisa'] = array( // … …

So in your code: 所以在你的代码中:

add_filter( 'product_type_options', 'add_e_visa_product_option' );
function add_e_visa_product_option( $product_type_options ) {
    $product_type_options['evisa'] = array(
        'id'            => '_evisa',
        'wrapper_class' => 'show_if_simple show_if_variable',
        'label'         => __( 'eVisa', 'woocommerce' ),
        'description'   => __( '', 'woocommerce' ),
        'default'       => 'no'
    );

    return $product_type_options;
}

add_action( 'woocommerce_process_product_meta_simple', 'save_evisa_option_fields'  );
add_action( 'woocommerce_process_product_meta_variable', 'save_evisa_option_fields'  );
function save_evisa_option_fields( $post_id ) {
    $is_e_visa = isset( $_POST['_evisa'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_evisa', $is_e_visa );
}

Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 WooCommerce 产品设置中的自定义复选框将 class 添加到 WooCommerce 页面 - Add class to WooCommerce pages via custom checkbox in WooCommerce product settings WooCommerce 产品循环中的复选框,在 Ajax 上添加选项添加到购物车 - Checkbox in WooCommerce product loop that add an option on Ajax add to cart 在单个产品页面上添加一个复选框,这会在Woocommerce中增加额外的费用 - Add a checkbox on single product pages that adds an additional cost in Woocommerce 添加一个复选框作为隐藏相关产品的 Woocommerce 管理产品选项 - Add a checkbox as Woocommerce admin product option that hides related products 如何在 WooCommerce 后端编辑产品数据选项卡 - How to edit product data tab in WooCommerce backend 在woocommerce中添加自定义产品类型 - Add custom product type in woocommerce 在 Woocommerce 中添加新产品类型 3 - Add a new product type in Woocommerce 3 当产品为空时添加产品尺寸-添加/编辑产品-Woocommerce - Add product dimension while they are empty - Add/edit product - Woocommerce 将复选框添加到 WooCommerce 产品高级选项选项卡 - Add checkbox to WooCommerce product advanced options tab 如何检查 WooCommerce 中是否有自定义产品类型选项? - How to check if is a custom product type option in WooCommerce?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM