对于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
发布于 2017-02-25 20:20:07
代码的问题在于,您试图将复选框的值保存在为其生成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');https://stackoverflow.com/questions/42459770
复制相似问题