WooCommerce:在申请时检查优惠券元

时间:2021-05-12 08:02:51

标签: php wordpress woocommerce woocommerce-theming

更新 复选框代码是:

// Add a custom checkbox to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_option_checkbox', 10 );
function add_coupon_option_checkbox() {
    woocommerce_wp_checkbox( array(
        'id'            => 'coupon_options',
        'label'         => __( 'Enable option 2', 'woocommerce' ),
        'description'   => __( 'Make this coupon to not be runned below amount of $200', 'woocommerce' ),
        'desc_tip'      => false,
    ) );
}
// Save the custom checkbox value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_option_checkbox', 10, 2 );
function save_coupon_option_checkbox( $post_id, $coupon ) {
    update_post_meta( $post_id, 'coupon_options', isset( $_POST['coupon_options'] ) ? 'yes' : 'no' );
}

我想创建一个自定义检查函数,该函数会将 falsetrue 返回到在优惠券后端页面上选中的自定义复选框。 如果选中复选框,我需要返回 true,否则返回 false。 这是我写的函数:

    function check_coupon_option_1_or_2() {
    $applied_coupons = WC()->cart->get_applied_coupons();

    if( sizeof($applied_coupons) > 0 ) {
        // Loop through applied coupons
        foreach( $applied_coupons as $coupon_code ) {
            $coupon = new WC_Coupon( $coupon_code );
            if( $coupon->get_meta('coupon_options') === 'yes' ) {
                return true;
                break;
            } 
            elseif ( $coupon->get_meta('coupon_options') === 'no' ) {
                return false;
                break;
            }
        }
    }
}

0 个答案:

没有答案