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

我已经对 functions.php 中的这个简单函数有了足够的了解,让我向优惠券添加一个复选框。但是,一旦我保存/更新优惠券,我的复选框值(选中/未选中)不会被提交(因此复选框始终未选中)。换句话说,当我更新/保存时,我无法让它在 postmetas 的 meta_value 列中将值更新为 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/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php

最佳答案

您的代码存在的问题是您试图将复选框的值保存在为其生成 html 的同一函数中。这行不通。您需要将当前功能分成两部分,分别在两个不同的 WooCommerce Hook 上运行。

首先是显示实际的复选框:

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');

https://stackoverflow.com/questions/42459770/

相关文章:

html - 将鼠标悬停在文本上时显示图像

spring-boot - 将所有 Zuul 代理请求动态重新路由到单个 URL

xml - “||” (OR) 在XML中怎么写?

c# - 在 NSubstitute 中使用 IEnumerable 设置参数匹配器

list - 为什么这个删除 Haskell 中的重复函数有两个参数而不是一个?

spring-mvc - 如何在应用程序中使用 "RestController"和 "Control

c# - 无法将类型为 'System.DateTime' 的对象转换为类型 'System.Str

reactjs - 在reactjs中使用具有相同路由路径的条件组件

bash - 在 bash 命令输出中添加前缀

oracle - Windows 的 ins-32025 oracle 11g 错误