微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据发货国家/地区在 WooCommerce 结帐中显示自定义消息

如何解决根据发货国家/地区在 WooCommerce 结帐中显示自定义消息

我目前正在使用下面的代码显示基于国家/地区的自定义消息:

add_action( 'woocommerce_before_checkout_billing_form','display_shipping_notice' );
function display_shipping_notice() {
    echo '<div class="shipping-notice woocommerce-info"  style="display:none">Please allow 5-10 business days for delivery after order processing.</div>';
}
  
add_action( 'woocommerce_after_checkout_form','show_shipping_notice_js' );
function show_shipping_notice_js(){
    ?>
    <script>
        jQuery(document).ready(function($){
            // Set the country code (That will display the message)
            var countryCode = 'GB';
  
            $('select#billing_country').change(function(){
                selectedCountry = $('select#billing_country').val();
                  
                if( selectedCountry == countryCode ){
                    $('.shipping-notice').show();
                }
                else {
                    $('.shipping-notice').hide();
                }
            });
        });
    </script>
    <?PHP 
}

代码的问题在于,它只会在更改或选择国家/地区时显示消息。但是,大多数客户已经预先填写了他们的国家/地区,因此不会显示自定义消息。

我正在尝试找到一种方法来更改代码,以便在选择正确的国家/地区时始终显示消息。

解决方法

我已重新访问您的代码,并进行了更改:

  • 成功消息(蓝色)到通知消息(绿色),
  • 加载 DOM 时处理加载国家/地区的 JQuery 代码。

但请注意,您的 jQuery 代码正在处理帐单国家/地区(请参阅最后的送货国家/地区)

请尝试以下操作:

add_action( 'woocommerce_checkout_before_customer_details','display_shipping_notice' );
function display_shipping_notice() {
    echo '<div class="shipping-notice woocommerce-message" role="alert" style="display:none">Please allow 5-10 business days for delivery after order processing.</div>';
}

add_action( 'woocommerce_after_checkout_form','show_shipping_notice_js' );
function show_shipping_notice_js(){
    ?>
    <script>
        jQuery(function($){
            var countryCode  = 'GB',// Set the country code (That will display the message)
                countryField = 'select#billing_country'; // The Field selector to target
            
            function showHideShippingNotice( countryCode,countryField ){
                if( $(countryField).val() === countryCode ){
                    $('.shipping-notice').show();
                }
                else {
                    $('.shipping-notice').hide();
                }
            }

            // On Ready (after DOM is loaded)
            showHideShippingNotice( countryCode,countryField );

            // On billing country change (Live event)
            $('form.checkout').on('change',countryField,function() {
                showHideShippingNotice( countryCode,countryField );
            });
        });
    </script>
    <?php
}

代码位于活动子主题(或活动主题)的functions.php 文件中。 经测试有效。

enter image description here


注意:如果您要处理发货国家/地区而不是帐单国家/地区,请替换:

                countryField = 'select#billing_country'; // The Field selector to target

这样:

                countryField = 'select#shipping_country'; // The Field selector to target

现在它将处理发货国家/地区。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。