【发布时间】:2021-07-15 08:38:14
【问题描述】:
我在产品选项上创建了自定义复选框,但在保存数据时卡住了。 我尝试了所有可能的变体,但没有成功。 我哪里错了? 谢谢
这是代码:
add_action( 'woocommerce_product_options_sku', 'custom_checkbox_field' );
function custom_checkbox_field(){
global $post, $product_object;
if ( ! is_a( $product_object, 'WC_Product' ) ) {
$product_object = wc_get_product( $post->ID );
}
woocommerce_wp_checkbox(
array(
'id' => 'custom_checkbox_field',
'value' => empty($values) ? 'yes' : $values,
'label' => __( 'Label', 'woocommerce' ),
'description' => __( 'Description', 'woocommerce' ),
)
);
}
add_action( 'woocommerce_process_product_meta', 'save_custom_field' );
function save_custom_field( $post_id ) {
// grab the product
$product = wc_get_product( $post_id );
// save data
$product->update_meta_data( 'custom_checkbox_field', isset($_POST['custom_checkbox_field']) ? 'yes' : 'no' );
$product->save();
}
【问题讨论】:
标签: php wordpress checkbox woocommerce custom-fields