我已经对 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/