使用优惠券简码表格检查是否在 WooCommerce 中应用了优惠券

问题描述

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

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

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

示例:

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

客户/用户应用“summer”并收到错误消息,因为该优惠券不存在。

客户/用户再次应用“winter”并收到“已应用”消息。为什么?因为那张优惠券已经用过了。

希望这对你们所有人都有意义。这是我正在使用的代码:

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>';
    }
}

解决方法

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

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 文件中。经测试有效。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...