与优惠券相关的错误消息位于优惠券字段下方。

huangapple go评论135阅读模式
英文:

Error messages related to coupon below coupon field

问题

根据https://stackoverflow.com/questions/65692133/move-coupon-form-before-payment-section-in-woocommerce-checkout/65696286#65696286中的答案代码,我正在自定义Woocommerce结账页面,将优惠券代码字段移动到不同的位置。

现在最大的问题是我找不到一种方法将与优惠券相关的错误移动到优惠券字段下方。如果出现错误(例如,优惠券无效时),我想在coupon-form div下面显示新的div来显示错误信息。

这是我的代码:

// 只隐藏默认的WooCommerce优惠券字段
add_action('woocommerce_before_checkout_form', 'hide_checkout_coupon_form', 5);
function hide_checkout_coupon_form() {
	echo '<style>.woocommerce-form-coupon-toggle {display:none;}</style>';
}

// 在结账付款部分之前添加自定义优惠券字段
add_action('woocommerce_review_order_before_payment', 'woocommerce_checkout_coupon_form_custom');
function woocommerce_checkout_coupon_form_custom() {
	echo '<div class="coupon-form" style="margin-bottom:20px;">';
	echo '<input type="text" name="coupon_code" class="input-text" placeholder="' . __("Coupon code") . '" id="coupon_code" value="">';
	echo '<button type="button" class="button coupon-btn" name="apply_coupon" value="' . __("Apply coupon") . '">' . __("Apply coupon") . '</button>';
	echo '<div class="clear"></div>';
	echo '</div>';
}

// jQuery代码
add_action('wp_footer', 'custom_checkout_jquery_script');
function custom_checkout_jquery_script() {
	if (is_checkout() && !is_wc_endpoint_url()) :
	?>
	<script type="text/javascript">
	jQuery(function($){

		// 将输入的优惠券代码复制到WooCommerce隐藏的默认优惠券字段
		$('.coupon-form input[name="coupon_code"]').on('input change', function(){
			$('form.checkout_coupon input[name="coupon_code"]').val($(this).val());
		});
		
		// 在按钮点击时,提交WooCommerce隐藏的默认优惠券表单
		$('.coupon-form button[name="apply_coupon"]').on('click', function(){
			$('form.checkout_coupon').submit();
		});
	});

	(function($){

		$('.woocommerce-form-coupon-toggle').remove();
		$(document).on("click", 'button[name="apply_coupon"]', function(event) {
			
			event.preventDefault();
	
			$form = $('form[name="checkout"]');
			$form.block({message:''});
	
			var data = {
				security:       wc_checkout_params.apply_coupon_nonce,
				coupon_code:    $('input[name="coupon_code"]').val()
			};
	
			$.ajax({
				type:       'POST',
				url:        wc_checkout_params.wc_ajax_url.toString().replace('%%endpoint%%', 'apply_coupon'),
				data:       data,
				success:    function( code ) {
					
					$('.woocommerce-error, .woocommerce-message').remove();
					$form.removeClass('processing').unblock();
	
					if ( code ) {
						$('button[name="apply_coupon"]').parent().after(code);
						$(document.body).trigger('update_checkout', { update_shipping_method: false });
					}
	
				},
				dataType: 'html'
			});
	
		});
	
	})(jQuery);
	</script>
	<?php
	endif;
}

在我的一款喜爱的主题中,我找到了这段代码并尝试用它替换我的代码,但无法使其工作。

apply_coupon: function (t) {
    this.$review.is(".--loading") || this.$review.parents(".--loading").length || this.$review.addClass("--loading");
    var i = this,
        o = t.siblings("input[name='coupon_code']"),
        n = o.val(),
        s = { security: wc_checkout_params.apply_coupon_nonce, coupon_code: n };
    e.ajax({
        type: "POST",
        url: wc_checkout_params.wc_ajax_url.toString().replace("%%endpoint%%", "apply_coupon"),
        data: s,
        dataType: "html",
        success: function (o) {
            e(".woocommerce-error, .woocommerce-message, .woocommerce-info", this.$page).remove(), e(".__notice", this.$page).remove();
            var n = e('<div class="__notice --clean-wc-notice">' + o + "</div>").insertAfter(t.parent());
            e(document.body).trigger("applied_coupon_in_checkout", [s.coupon_code]),
                e(document.body).trigger("update_checkout", { update_shipping_method: !1 }),
                e(".woocommerce-message", n).length && i.$review.addClass("--mob-active");
        },
        complete: function () {
            i.$review.removeClass("--loading"), o.val("");
        },
    });
},

如果您需要任何进一步的帮助,请告诉我。

英文:

Based on https://stackoverflow.com/questions/65692133/move-coupon-form-before-payment-section-in-woocommerce-checkout/65696286#65696286 answer code. I'm customizing the Woocommerce checkout page, moving the coupon code field on different location.

Now the biggest problem is that I can't find a way to move coupon related errors below coupon field. If there is an error (for example, when a coupon is not valid) I want to show new div below coupon-form div displaying the error.

Here is My code:

// Just hide default woocommerce coupon field
add_action( &#39;woocommerce_before_checkout_form&#39;, &#39;hide_checkout_coupon_form&#39;, 5 );
function hide_checkout_coupon_form() {
echo &#39;&lt;style&gt;.woocommerce-form-coupon-toggle {display:none;}&lt;/style&gt;&#39;;
}
// Add a custom coupon field before checkout payment section
add_action( &#39;woocommerce_review_order_before_payment&#39;, 
&#39;woocommerce_checkout_coupon_form_custom&#39; );
function woocommerce_checkout_coupon_form_custom() {
echo &#39;&lt;div class=&quot;coupon-form&quot; style=&quot;margin-bottom:20px;&quot; style=&quot;display:none !important;&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;coupon_code&quot; class=&quot;input-text&quot; placeholder=&quot;&#39; . __(&quot;Coupon code&quot;) . &#39;&quot; id=&quot;coupon_code&quot; value=&quot;&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;button coupon-btn&quot; name=&quot;apply_coupon&quot; value=&quot;&#39; . __(&quot;Apply coupon&quot;) . &#39;&quot;&gt;&#39; . __(&quot;Apply coupon&quot;) . &#39;&lt;/button&gt;
&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
&lt;/div&gt;&#39;;
}
// jQuery code
add_action( &#39;wp_footer&#39;, &#39;custom_checkout_jquery_script&#39; );
function custom_checkout_jquery_script() {
if ( is_checkout() &amp;&amp; ! is_wc_endpoint_url() ) :
?&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
jQuery( function($){
// Copy the inputed coupon code to WooCommerce hidden default coupon field
$(&#39;.coupon-form input[name=&quot;coupon_code&quot;]&#39;).on( &#39;input change&#39;, function(){
$(&#39;form.checkout_coupon input[name=&quot;coupon_code&quot;]&#39;).val($(this).val());
// console.log($(this).val()); // Uncomment for testing
});
// On button click, submit WooCommerce hidden default coupon form
$(&#39;.coupon-form button[name=&quot;apply_coupon&quot;]&#39;).on( &#39;click&#39;, function(){
$(&#39;form.checkout_coupon&#39;).submit();
// console.log(&#39;click: submit form&#39;); // Uncomment for testing
});
});
(function($){
$(&#39;.woocommerce-form-coupon-toggle&#39;).remove();
$(document).on(&quot;click&quot;, &#39;button[name=&quot;apply_coupon&quot;]&#39;, function(event) {
event.preventDefault();
$form = $(&#39;form[name=&quot;checkout&quot;]&#39;);
$form.block({message:&#39;&#39;});
var data = {
security:       wc_checkout_params.apply_coupon_nonce,
coupon_code:    $( &#39;input[name=&quot;coupon_code&quot;]&#39; ).val()
};
$.ajax({
type:       &#39;POST&#39;,
url:        wc_checkout_params.wc_ajax_url.toString().replace( &#39;%%endpoint%%&#39;, &#39;apply_coupon&#39; ),
data:       data,
success:    function( code ) {
$( &#39;.woocommerce-error, .woocommerce-message&#39; ).remove();
$form.removeClass( &#39;processing&#39; ).unblock();
if ( code ) {
$( &#39;button[name=&quot;apply_coupon&quot;]&#39; ).parent().after( code );
$( document.body ).trigger( &#39;update_checkout&#39;, { update_shipping_method: false } );
}
},
dataType: &#39;html&#39;
});
});
})(jQuery);
&lt;/script&gt;
&lt;?php
endif;
}

In one of my favorite themes I found this piece of code and I tried to replace my code with it, but can not make it work

		apply_coupon: function (t) {
this.$review.is(&quot;.--loading&quot;) || this.$review.parents(&quot;.--loading&quot;).length || this.$review.addClass(&quot;--loading&quot;);
var i = this,
o = t.siblings(&quot;input[name=&#39;coupon_code&#39;]&quot;),
n = o.val(),
s = { security: wc_checkout_params.apply_coupon_nonce, coupon_code: n };
e.ajax({
type: &quot;POST&quot;,
url: wc_checkout_params.wc_ajax_url.toString().replace(&quot;%%endpoint%%&quot;, &quot;apply_coupon&quot;),
data: s,
dataType: &quot;html&quot;,
success: function (o) {
e(&quot;.woocommerce-error, .woocommerce-message, .woocommerce-info&quot;, this.$page).remove(), e(&quot;.__notice&quot;, this.$page).remove();
var n = e(&#39;&lt;div class=&quot;__notice --clean-wc-notice&quot;&gt;&#39; + o + &quot;&lt;/div&gt;&quot;).insertAfter(t.parent());
e(document.body).trigger(&quot;applied_coupon_in_checkout&quot;, [s.coupon_code]),
e(document.body).trigger(&quot;update_checkout&quot;, { update_shipping_method: !1 }),
e(&quot;.woocommerce-message&quot;, n).length &amp;&amp; i.$review.addClass(&quot;--mob-active&quot;);
},
complete: function () {
i.$review.removeClass(&quot;--loading&quot;), o.val(&quot;&quot;);
},
});
},

答案1

得分: 1

以下是您提供的代码的中文翻译:

要在WooCommerce结账页面的优惠券字段下方显示与优惠券相关的错误消息,您可以修改现有的代码。

// 仅隐藏默认的WooCommerce优惠券字段
add_action('woocommerce_before_checkout_form', 'hide_checkout_coupon_form', 5);
function hide_checkout_coupon_form() {
    echo '<style>.woocommerce-form-coupon-toggle {display:none;}</style>';
}

// 在结账付款部分之前添加自定义优惠券字段
add_action('woocommerce_review_order_before_payment', 'woocommerce_checkout_coupon_form_custom');
function woocommerce_checkout_coupon_form_custom() {
    echo '<div class="coupon-form" style="margin-bottom:20px;">
        <input type="text" name="coupon_code" class="input-text" placeholder="' . __('优惠券代码') . '" id="coupon_code" value="">
        <button type="button" class="button coupon-btn" name="apply_coupon" value="' . __('应用优惠券') . '">' . __('应用优惠券') . '</button>
        <div class="coupon-error"></div>
        <div class="clear"></div>
    </div>';
}

// jQuery代码
add_action('wp_footer', 'custom_checkout_jquery_script');
function custom_checkout_jquery_script() {
    if (is_checkout() && !is_wc_endpoint_url()) :
    ?>
    <script type="text/javascript">
        jQuery(function($){
            // 将输入的优惠券代码复制到WooCommerce隐藏的默认优惠券字段
            $('.coupon-form input[name="coupon_code"]').on('input change', function(){
                $('form.checkout_coupon input[name="coupon_code"]').val($(this).val());
            });
            // 单击按钮时,提交WooCommerce隐藏的默认优惠券表单
            $('.coupon-form button[name="apply_coupon"]').on('click', function(){
                $('form.checkout_coupon').submit();
            });
            // 处理优惠券申请响应
            $(document.body).on('applied_coupon_in_checkout', function(event, coupon_code){
                if (coupon_code) {
                    $('.coupon-error').html('');
                }
            });
            $(document.body).on('woocommerce-applied_coupon-in_checkout', function(event, response){
                if (response && response.result === 'failure' && response.messages) {
                    $('.coupon-error').html(response.messages);
                }
            });
        });
    </script>
    <?php
    endif;
}

请注意,这是您提供的代码的翻译版本,没有包括额外的解释或信息。

英文:

To display coupon-related errors below the coupon field on the WooCommerce checkout page, you can modify the existing cod

// Just hide default WooCommerce coupon field
add_action( &#39;woocommerce_before_checkout_form&#39;, &#39;hide_checkout_coupon_form&#39;, 5 );
function hide_checkout_coupon_form() {
echo &#39;&lt;style&gt;.woocommerce-form-coupon-toggle {display:none;}&lt;/style&gt;&#39;;
}
// Add a custom coupon field before checkout payment section
add_action( &#39;woocommerce_review_order_before_payment&#39;, &#39;woocommerce_checkout_coupon_form_custom&#39; );
function woocommerce_checkout_coupon_form_custom() {
echo &#39;&lt;div class=&quot;coupon-form&quot; style=&quot;margin-bottom:20px;&quot; style=&quot;display:none !important;&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;coupon_code&quot; class=&quot;input-text&quot; placeholder=&quot;&#39; . __(&quot;Coupon code&quot;) . &#39;&quot; id=&quot;coupon_code&quot; value=&quot;&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;button coupon-btn&quot; name=&quot;apply_coupon&quot; value=&quot;&#39; . __(&quot;Apply coupon&quot;) . &#39;&quot;&gt;&#39; . __(&quot;Apply coupon&quot;) . &#39;&lt;/button&gt;
&lt;div class=&quot;coupon-error&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
&lt;/div&gt;&#39;;
}
// jQuery code
add_action( &#39;wp_footer&#39;, &#39;custom_checkout_jquery_script&#39; );
function custom_checkout_jquery_script() {
if ( is_checkout() &amp;&amp; ! is_wc_endpoint_url() ) :
?&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
jQuery( function($){
// Copy the inputted coupon code to WooCommerce hidden default coupon field
$(&#39;.coupon-form input[name=&quot;coupon_code&quot;]&#39;).on( &#39;input change&#39;, function(){
$(&#39;form.checkout_coupon input[name=&quot;coupon_code&quot;]&#39;).val($(this).val());
});
// On button click, submit WooCommerce hidden default coupon form
$(&#39;.coupon-form button[name=&quot;apply_coupon&quot;]&#39;).on( &#39;click&#39;, function(){
$(&#39;form.checkout_coupon&#39;).submit();
});
// Handle coupon application response
$(document.body).on(&#39;applied_coupon_in_checkout&#39;, function(event, coupon_code){
if (coupon_code) {
$(&#39;.coupon-error&#39;).html(&#39;&#39;);
}
});
$(document.body).on(&#39;woocommerce-applied_coupon-in_checkout&#39;, function(event, response){
if (response &amp;&amp; response.result === &#39;failure&#39; &amp;&amp; response.messages) {
$(&#39;.coupon-error&#39;).html(response.messages);
}
});
});
&lt;/script&gt;
&lt;?php
endif;
}

huangapple
  • 本文由 发表于 2023年5月24日 20:37:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定