dongqiang5541 2016-10-03 21:59
浏览 223
已采纳

WooCommerce向国际客户发货通知

I need to add a shipping notice at checkout and i have the below code:

// 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' );

I just need to know if there is a way to restrict this notice to my international customers? I don't mind having to list each country and Canada would be a great start.

I have seen the $woocommerce->customer->get_shipping_country() but not entirely sure how to implement it in my functions.phpand also what codes does Woocommerce use for each country?

Thanks for helping me out!

  • 写回答

2条回答 默认 最新

  • douyuai8994 2016-10-04 01:18
    关注

    Here is your revisited code with some jQuery inside, because customer is not always registered, and you don't know in all cases what country is going to be chosen as shipping country.

    Here is that code:

    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' );
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and fully functional.

    You can see here a raw demo (temporary). Here the chosen country that will not display the message on checkout is France.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题