php - 根据运送国家/地区在 WooCommerce 结帐中显示自定义消息

标签 php wordpress woocommerce checkout notice

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

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

现在它将处理运输国家/地区。

关于php - 根据运送国家/地区在 WooCommerce 结帐中显示自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65809171/

相关文章:

php - 在 Visual Studio Code 中为 PHP 重命名重构

wordpress - 如何在woocommerce中获得用户的订单总和?

php - 在 Woocommerce 管理产品页面中编辑自定义产品选项卡内容

PHPUnit - 安装了版本 3.7.21 而不是版本 6

PHP 5.3.8、JSON 和 CentOS 6

html - 插入更多文本时 DIV 不会变宽

wordpress - 如何取消响应式网站页脚菜单中的 CSS 效果?

wordpress - Paypal /Woocommerce - "Error Detected Your shopping cart is empty."

php - Laravel 中的 mariaDB JSON 支持

javascript - 如何使用 wp_localize_script 将数组传递给 jQuery?