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



我在functions.php中的这个简单函数上已经足够远,让我在优惠券中添加一个复选框。但是,一旦我保存/更新优惠券,我的复选框值(Check/Uneclected)就不会被犯下(因此,复选框始终不受限制)。换句话说,当我更新/保存时,我无法将其更新为YES的值。复选框在那里,我只是无法使用它...非常令人沮丧!关于我做错了什么,请:)

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/plugins/woocommerce/包括/admin/meta-boxes/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');

最新更新