【问题标题】:Woocommerce coupons adding custom checkboxWoocommerce 优惠券添加自定义复选框
【发布时间】:2017-02-25 18:11:11
【问题描述】:

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

【问题讨论】:

    标签: php wordpress checkbox woocommerce hook-woocommerce


    【解决方案1】:

    您的代码的问题是您试图将复选框的值保存在为其生成 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');
    

    【讨论】:

    • 哈哈,在我看到你之前发布了我的答案!是的,我现在明白了这个过程。谢谢你的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多