Woocommerce后端产品编辑页面中的“将复选框添加到产品类型选项" [英] Add checkbox to product type option in Woocommerce backend product edit pages
问题描述
我在Woocommerce管理产品数据设置中添加了一个自定义选项复选框.如果我启用该复选框并保存更改,则该值会正确保存在产品元数据中,但该复选框永远不会保持选中状态.
I have added a custom option checkbox in Woocommerce admin product data settings. 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?
我的代码:
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' );
推荐答案
答案很简单……您只是忘了在第一个函数中为数组添加键ID,例如:
The answer is very simple… you just forgot to add a key id for your array in the first function, like:
$product_type_options['evisa'] = array( // … …
所以在您的代码中:
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 );
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于Woocommerce后端产品编辑页面中的“将复选框添加到产品类型选项"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!