4

In woocommerce, I need to display custom message on cart or checkout page, based on shipping zone, like "you'll be charged 10% more for this zip code".

My workaround is about customizing that kind of default message :

add_filter( 'woocommerce_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
// For Checkout page
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
function wf_customize_default_message( $default_msg ) {
    $zip_array = array(
        '30031',
    );

    if ( in_array( WC()->customer->get_shipping_postcode() , $zip_array) ) {
        $custom_msg = "Call us for quotation - 1-800-XXX-XXXX";
        if( empty( $custom_msg ) ) {
          return $default_msg;
        }
        return $custom_msg;
    }

    return $default_msg;
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Camille S.
  • 61
  • 1
  • 6
  • Hi Loic, thanks for your quick answer. I had not test your code yet but is it possible to adapt your sample to shipping zones I have created for one shipping method I have created ? + I have a lot of zipcodes to add, not just one... Thanks a lot for your help ! – Camille S. Oct 10 '18 at 20:53
  • Hi again Loic, I don't understand, I can't see your first answer ? – Camille S. Oct 10 '18 at 21:04
  • I'm sorry but I think you have deleted your answer... Can you post it again please ? – Camille S. Oct 10 '18 at 21:09
  • I have made a different answer… more convenient… Using woocommerce notices will not work on checkout if user change of shipping zone. – LoicTheAztec Oct 10 '18 at 21:41
  • I didn't test its yet but it seems impressive ! Thanks a lot ! – Camille S. Oct 10 '18 at 22:05

2 Answers2

4

Updated

Try the following code based on a shipping Zones name (with postcodes restrictions) that will display your message on the shipping total lines (but that will not generate a woocommerce notice):

add_action( 'woocommerce_cart_totals_after_shipping' , 'shipping_zone_targeted_postcodes_custom_notice' );
add_action( 'woocommerce_review_order_after_shipping' , 'shipping_zone_targeted_postcodes_custom_notice' );
function shipping_zone_targeted_postcodes_custom_notice() {
    // HERE DEFINE YOUR SHIPPING ZONE NAME(S)
    $targeted_zones_names = array('France'); // <======  <======  <======  <======  <======  

    // Get the customer shipping zone name
    $chosen_methods    = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
    $chosen_method     = explode(':', reset($chosen_methods) );
    $shipping_zone     = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
    $current_zone_name = $shipping_zone->get_zone_name();

    if( in_array( $current_zone_name, $targeted_zones_names ) ){
        echo '<tr class="shipping">
            <td colspan="2" style="text-align:center">' . sprintf(
                __( "You'll be charged %s more for %s zip code", "woocommerce"),
                '<strong>10%</strong>',
                '<strong>' . WC()->customer->get_shipping_postcode() . '</strong>'
            ) . '</td>
        </tr>';
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
2

Using @LoicTheAztec's solution above, I modified it to give a message based on customer's country.

add_action( 'woocommerce_cart_totals_after_shipping' , 'out_of_zone_shipping_notice' );
add_action( 'woocommerce_review_order_after_shipping' , 'out_of_zone_shipping_notice' );
function out_of_zone_shipping_notice() {
    // HERE DEFINE YOUR SHIPPING COUNTRY NAMES
    $targeted_country_names = array("CA", "US"); // 

    // Get the customer shipping country
    $shipping_country    = WC()->customer->get_shipping_country();
    
    if( !in_array( $shipping_country, $targeted_country_names ) ){
        echo '<tr class="shipping"><td colspan="2" style="text-align:center">You are outside of our regular shipping zone. Please <a href="../contact/">contact us</a> with your address so we can get an accurate cost to ship.</td></tr>';
    }
}

In order to see what format the $shipping_country showed up in, I used

echo $shipping_country

just below $shipping_country = WC()->customer->get_country(); to show on the cart page what the actual country code syntax was so that I could match it with the if statement. However, this code segment was removed prior to deployment to avoid random characters on the screen for the customer.

ntk4
  • 1,247
  • 1
  • 13
  • 18