[英]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.