【问题标题】:WooCommerce shipping notice to International customersWooCommerce 向国际客户发货通知
【发布时间】:2017-02-11 22:56:59
【问题描述】:

我需要在结帐时添加发货通知,并且我有以下代码:

// adds order note at checkout page     
function notice_shipping() {
echo '<p id="allow">Please allow 5-10 business days for delivery after order processing.</p>';
}

add_action( 'woocommerce_before_order_notes', 'notice_shipping' );

我只需要知道是否有办法将此通知限制为我的国际客户?我不介意必须列出每个国家,加拿大将是一个很好的开始。

我已经看过$woocommerce-&gt;customer-&gt;get_shipping_country(),但不完全确定如何在我的functions.php中实现它以及Woocommerce在每个国家/地区使用什么代码?

感谢您帮助我!

【问题讨论】:

    标签: php jquery wordpress woocommerce shipping


    【解决方案1】:

    这是您重新访问的代码,其中包含一些 jQuery,因为客户并不总是已注册,而且您并不知道在所有情况下将选择哪个国家作为发货国家。

    代码如下:

    function notice_shipping(){
        ?>
    
        <script>
            jQuery(document).ready(function($){
    
                // Set the country code (That will NOT display the message)
                var countryCode = 'FR';
    
                var adressType =  'billing';
    
                // Detecting If shipping Country is going to be used
                $('#ship-to-different-address-checkbox').click(function(){
    
                    if($('.shipping_address').css( 'display' ) == 'none'){
                        adressType = 'billing';
                    } else {
                        adressType = 'shipping';
                    }
                    // console.log($adressType);
                });
    
                // Showing or hidding the message
                $('select#billing_country, select#shipping_country').change(function(){
    
                    if(adressType == 'billing') {
                        selectedCountry = $('select#billing_country').val();
                    }
                    else if(adressType == 'shipping') {
                        selectedCountry = $('select#shipping_country').val();
                    }
    
                    if( selectedCountry == countryCode ){
                        $('.shipping-notice').hide();
                        // console.log('hide');
                    }
                    else {
                        $('.shipping-notice').show();
                        // console.log('show');
                    }
                    // console.log(selectedCountry);
                });
            });
        </script>
    
        <?php
    
        echo '<p id="allow" class="shipping-notice" style="display:none">Please allow 5-10 business days for delivery after order processing.</p>';
    }
    add_action( 'woocommerce_before_order_notes', 'notice_shipping' );
    

    此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    此代码已经过测试并且功能齐全。

    您可以see here a raw demo(临时)这里选择的结帐时不会显示消息的国家是法国。

    【讨论】:

    • 不错!我在想 JS 可能是一个更好的解决方案,但没有时间充实它。
    • @helgatheviking 谢谢 :) … 我也有点忙于一个新的非常复杂的新 WooCommerce 项目。所以这就是为什么我的答案来晚了。你的回答太棒了!
    • $('#ship-to-different-address-checkbox').click(function(){ 的两个小改动来自 console.log 的 $:if($('.shipping_address').css( 'display' ) == 'none'){ adressType = 'shipping'; } else { adressType = 'billing'; } //console.log(adressType);
    • @Xenocide122 好的,我会根据您的更改更新我的答案
    【解决方案2】:

    我认为您不能保证知道woocommerce_before_order_notes 上的客户地址。您可以尝试以下方法,但我不确定在客户提交账单地址之前它是否会起作用。

    // adds order note at checkout page     
    function notice_shipping( $checkout ) {
       $country = WC()->customer->get_shipping_country();
       if( $country != 'US' ){
           echo '<p id="allow">Please allow 5-10 business days for delivery after order processing.</p>';
        }
    }
    
    add_action( 'woocommerce_before_order_notes', 'notice_shipping' );
    

    【讨论】:

    • 哦,是的,非常正确。我将不得不寻找一个更好的地方来展示它。有什么建议?我将不得不查看钩子并在早上考虑它。因为我不在我的比赛中,所以我要到早上才能进行测试。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多