Woocommerce优惠券添加自定义复选框

问题描述:

我在函数function.php中的这个简单函数上已经足够了,让我添加一个复选框给优惠券。但是,一旦我保存/更新优惠券,我的复选框值(选中/未选中)不会被提交(因此复选框始终未选中)。换句话说,当我更新/保存时,我无法在postmetas的meta_value列中将值更新为yes。复选框在那里,我只是不能使用它......非常令人沮丧!在我做错了任何sugestions,请:)Woocommerce优惠券添加自定义复选框

function add_coupon_revenue_dropdown_checkbox() { 
$post_id = $_GET['post']; 

woocommerce_wp_checkbox(array('id' => 'include_stats', 'label' => __('Coupon check list', 'woocommerce'), 'description' => sprintf(__('Includes the coupon in coupon check drop-down list', 'woocommerce')))); 

$include_stats = isset($_POST['include_stats']) ? 'yes' : 'no'; 

update_post_meta($post_id, 'include_stats', $include_stats); 

do_action('woocommerce_coupon_options_save', $post_id); 

}add_action('woocommerce_coupon_options', 'add_coupon_revenue_dropdown_checkbox', 10, 0); 

我想影响的部分是:

的wp-content /插件/ woocommerce /包括/管理/元盒/ class-wc-meta-box-coupon-data.php

你的代码存在的问题是,你试图将复选框的值保存在为其生成html的相同函数中。这不起作用。您需要将当前的函数分成两部分,这两部分运行在两个不同的WooCommerce挂钩上。

首先是显示实际复选框:

function add_coupon_revenue_dropdown_checkbox() { 
    woocommerce_wp_checkbox(array('id' => 'include_stats', 'label' => __('Coupon check list', 'woocommerce'), 'description' => sprintf(__('Includes the coupon in coupon check drop-down list', 'woocommerce')))); 
} 
add_action('woocommerce_coupon_options', 'add_coupon_revenue_dropdown_checkbox', 10, 0); 

第二是保存复选框的值正在处理所提交的形式时。

function save_coupon_revenue_dropdown_checkbox($post_id) { 
    $include_stats = isset($_POST['include_stats']) ? 'yes' : 'no'; 
    update_post_meta($post_id, 'include_stats', $include_stats); 
} 
add_action('woocommerce_coupon_options_save', 'save_coupon_revenue_dropdown_checkbox'); 
+0

哈哈,在我看到你的之前张贴了我的回答!是的,我现在了解这个过程。谢谢您的回答 :) – axelra82