简体   繁体   中英

WooCommerce shipping notice to International customers

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!

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.

I don't think you are guaranteed to know the customer's address at the woocommerce_before_order_notes . You could try the following, but I am not sure it will work until after the customer submits her billing address.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM