1

I am trying to display a custom message based on customer postcode in WooCommerce using Display a custom message based on customer shipping zone in Woocommerce answer code:

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

What modifications to make to the code?

Can someone please assist me in what adjustments need to be made for this to happen please?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
3D Ag3nt
  • 11
  • 1

1 Answers1

2

For a set of defined Postcodes only, use instead the following (defining a set of targeted postcodes):

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 POSTCODES
    $targeted_postcodes = array('89000','89200','89300');

    // Get the customer postcode
    $customer_postcode = WC()->customer->get_shipping_postcode();
    $customer_postcode = ! empty($customer_postcode) ? WC()->customer->get_billing_postcode() : $customer_postcode;;

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

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you for your reply, does this matter if the postcodes I want to add are approximately 150 postcodes ? And do I separate by commas , also does this spawn a pop up to confirm why there is a price increase of £100? Thank you again for your reply – 3D Ag3nt Mar 27 '21 at 14:06
  • hello , sorry this project had been depreciated to another project , this is now a priority again. will this be product wide or with select products , do i need to define these products if i dont want it to be on every product? thank you – 3D Ag3nt May 04 '21 at 08:36