![](/img/trans.png)
[英]Add class to WooCommerce pages via custom checkbox in WooCommerce product settings
[英]Add checkbox to product type option in Woocommerce backend product edit pages
我在Woocommerce管理產品數據設置中添加了自定義選項復選框。 如果我啟用該復選框並保存更改,則該值會正確保存在產品元數據中, 但復選框永遠不會保持選中狀態 。
我做錯了什么? 如何使這個工作作為其他選項復選框?
我的代碼:
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,如:
$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文件中。 經過測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.