簡體   English   中英

WooCommerce向國際客戶發貨的通知

[英]WooCommerce shipping notice to International customers

我需要在結帳時添加發貨通知,並且我有以下代碼:

// 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->customer->get_shipping_country()但不完全確定如何在我的functions.php中實現它,以及Woocommerce在每個國家/地區使用什么代碼?

謝謝你的協助!

這是您重新訪問的代碼,其中包含一些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文件中,也可能會出現在任何插件文件中。

此代碼已經過測試,功能齊全。

您可以在此處看到原始演示 (臨時) 在這里,不會在結帳時顯示消息的所選國家是法國。

我認為您不能保證在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' );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM