【发布时间】:2021-02-19 10:12:13
【问题描述】:
我正在尝试在 WooCommerce 的产品页面中添加一个复选框。它被用来判断产品是否需要在结帐时签署发布表格。
这是在子主题中完成的。该复选框显示在我想要的产品的高级选项卡中,但它没有被保存。
这是来自我的functions.php 的代码,用于创建复选框并保存它。
function add_release_checkbox() {
$args = array(
'label' => __('Release Form Required','woocommerce'), // Text in the editor label
'value' => get_post_meta(get_the_id(), 'release_req', true), // meta_value where the id serves as meta_key
'id' => 'release_req', // required, it's the meta_key for storing the value (is checked or not)
'name' => __('Release Required','woocommerce'),
'cbvalue' => true, // "value" attribute for the checkbox
'desc_tip' => true, // true or false, show description directly or as tooltip
'description' => __('Tells if a release form is required for this product.','woocommerce') // provide something useful here
);
woocommerce_wp_checkbox( $args );
}
add_action( 'woocommerce_product_options_advanced', 'add_release_checkbox' );
function save_release_meta($post_id) {
write_log($_POST); // for debugging purposes write the post to the debug.log file
$release = isset($_POST['release_req']) ? $_POST['release_req'] : '0';
$product = wc_get_product($post_id);
$product->update_meta_data('release_req', $release);
$product->save();
}
add_action('woocommerce_process_product_meta', 'save_release_meta');
我把它做成一个文本框,它可以工作,所以问题是我做错了什么?即使我选中或取消选中它也不会保存。
【问题讨论】:
标签: php wordpress woocommerce backend product