【问题标题】:Check if coupon is applied in WooCommerce using coupon shortcode form检查是否使用优惠券短代码表单在 WooCommerce 中应用优惠券
【发布时间】:2021-04-09 08:08:14
【问题描述】:

我有一个应用优惠券的短代码(效果很好),它使用自定义消息来实现。一切正常,除了一件事:如果应用了优惠券并且用户犯了再次尝试应用它的“错误” - 它会给出优惠券不存在的错误。

我正在尝试修改此行为,但在我的最新更新中,甚至没有呈现输出(优惠券输入和提交按钮)。

我有错误和成功消息,它们都可以正常工作。当用户/客户尝试应用已应用的优惠券时,我需要第三条消息。

示例:

客户/用户申请“冬季”并获得 10% 的折扣。显示成功消息。

客户/用户应用“夏季”并获取错误消息,因为该优惠券不存在。

客户/用户再次申请“冬季”并收到“已申请”消息。为什么?因为该优惠券已被应用。

希望这对大家有意义。这是我正在使用的代码:

add_shortcode( 'coupon', 'redeem_coupon_form' );
function redeem_coupon_form() {

    if ( is_wc_endpoint_url( 'order-received' ) ) return;
    
    if ( isset( $_GET['coupon'] ) && isset( $_GET['redeem-coupon'] ) ) {

        if ( $coupon = esc_attr( $_GET['coupon'] ) ) {
        
            $applied = WC()->cart->apply_coupon($coupon);
            
            $applied_coupon = WC()->cart->get_applied_coupons();

            } else {

                $coupon = false;
            }

    $success = sprintf( __('<br><p class="coupon-code-does-exist">Thanks for applying the "%s" coupon<br />Please make sure everything checks out before paying.</p>'), $coupon);

    $error = sprintf( __('<br><p class="coupon-code-does-not-exist">Ops! What happened? The "%s" coupon does not exist.<br />Please check the coupon name, alternative - your spelling.</p>'), $coupon);

    $already_applied = sprintf( __('<br><p class="coupon-code-already-applied">You have already applied "%s" and can therefore not apply it again.</p>'), $coupon);

        if ( $applied == $applied_coupon ) {

            $message = $already_applied;

        } else {

            $message = isset( $applied ) && $applied ? $success : $error;

        wc_clear_notices();
    }

    $output  = '<div class="redeem-coupon"><form id="coupon-redeem">
                <input style="display:block; width: 65%; margin-right: 1%; height: 50px; border:1px solid #333; padding: 0px; float: left;" type="text" name="coupon" id="coupon" placeholder="&#32;&#32;Coupon Code Here" />
                <input style="display:block; margin: 0px;  width: 34%; height: 51px; padding: 0px;" type="submit" class="redeem-coupon-button" name="redeem-coupon" value="'.__('Redeem Coupon').'" />';

    $output .= isset( $coupon ) ? '<p class="result"> ' . $message . ' </p>' : '';
    
        return $output . '</form></div>';
    }
}

【问题讨论】:

    标签: php wordpress forms woocommerce coupon


    【解决方案1】:

    您的代码中存在一些错误,请尝试以下操作:

    add_shortcode( 'coupon', 'redeem_coupon_form' );
    function redeem_coupon_form() {
        if ( is_wc_endpoint_url( 'order-received' ) ||  is_wc_endpoint_url( 'order-pay' ) )
            return;
    
        if ( isset( $_GET['coupon'] ) && isset( $_GET['redeem-coupon'] ) ) {
            $coupon          = esc_attr( $_GET['coupon'] );
            $applied_coupons = WC()->cart->get_applied_coupons();
    
            if ( ! in_array( $coupon, $applied_coupons ) ) {
                $applied = WC()->cart->apply_coupon($coupon);
    
                if ( $applied ) {
                    $message = sprintf( __('<br><p class="coupon-code-does-exist">Thanks for applying the "%s" coupon<br />Please make sure everything checks out before paying.</p>'), $coupon);
                } else {
                    $message = sprintf( __('<br><p class="coupon-code-does-not-exist">Ops! What happened? The "%s" coupon does not exist.<br />Please check the coupon name, alternative - your spelling.</p>'), $coupon);
                }
            } else {
                 $message = sprintf( __('<br><p class="coupon-code-already-applied">You have already applied "%s" and can therefore not apply it again.</p>'), $coupon);
            }
            wc_clear_notices();
        }
    
        $output  = '<div class="redeem-coupon">
            <form id="coupon-redeem">
                <input style="display:block; width: 65%; margin-right: 1%; height: 50px; border:1px solid #333; padding: 0px; float: left;" type="text" name="coupon" id="coupon" placeholder="&#32;&#32;Coupon Code Here" />
                <input style="display:block; margin: 0px;  width: 34%; height: 51px; padding: 0px;" type="submit" class="redeem-coupon-button" name="redeem-coupon" value="'.__('Redeem Coupon').'" />';
    
        $output .= isset($coupon) && isset($message) ? '<p class="result"> ' . $message . ' </p>' : '';
    
        return $output . '</form></div>';
    }
    

    代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 嘿,再次感谢您的帮助。测试您的代码,我收到一条通知消息:Notice: Undefined variable: message
    • 是的,通知不见了。问题是;它不适用于现有的优惠券。尝试应用现有优惠券(以前未应用过)时,我收到错误消息。
    • 似乎工作正常:ish。我做了一个新的测试(没有饼干等)。现在的问题是,当尝试应用已应用的优惠券时,是这样的:Notice: Undefined variable: message 指的是:$message .= sprintf( __('&lt;br&gt;&lt;p class="coupon-code-already-applied"&gt;You have already applied "%s" and can therefore not apply it again.&lt;/p&gt;'), $coupon);
    • 所以我删除了 .在 = 符号之前,通知消失了。只是想确保这是正确的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2016-05-12
    • 2018-05-11
    • 2016-11-10
    相关资源
    最近更新 更多