使用优惠券简码表格检查优惠券是否在 WooCommerce 中应用
Check if coupon is applied in WooCommerce using coupon shortcode form
我有一个应用优惠券的短代码(工作正常),它使用自定义消息来实现。一切正常,除了一件事:如果应用优惠券并且用户犯了“错误”尝试再次应用它 - 它会给出优惠券不存在的错误。
我正在尝试修改此行为,但在我的最新更新中甚至没有呈现输出(优惠券输入和提交按钮)。
我有错误和成功消息,它们都工作正常。我需要的是当用户/客户尝试应用已应用的优惠券时的第三条消息。
示例:
客户/用户申请“冬季”并获得 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="  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="  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 文件。已测试并有效。
我有一个应用优惠券的短代码(工作正常),它使用自定义消息来实现。一切正常,除了一件事:如果应用优惠券并且用户犯了“错误”尝试再次应用它 - 它会给出优惠券不存在的错误。
我正在尝试修改此行为,但在我的最新更新中甚至没有呈现输出(优惠券输入和提交按钮)。
我有错误和成功消息,它们都工作正常。我需要的是当用户/客户尝试应用已应用的优惠券时的第三条消息。
示例:
客户/用户申请“冬季”并获得 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="  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="  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 文件。已测试并有效。