skip to Main Content

On Display a custom message based on customer shipping zone in Woocommerce answer, I found a solution to show a message in cart based on the shipping zone.

I want to show a message based on shipping zones and only if the cart amount is smaller than a specific amount, like:

  • Shipping Zone 1 and Cart amount is smaller than €29: it should show Text 1
    and
  • Shipping Zone 2 and Cart amount is smaller than €49: it should show Text 2

This is what I tried to edit the PHP code by myself:

add_action( 'woocommerce_cart_totals_after_shipping', 'shipping_notice_displayed', 20 );
add_action( 'woocommerce_review_order_after_shipping', 'shipping_notice_displayed', 20 );
function shipping_notice_displayed() {
    if ( did_action( 'woocommerce_cart_totals_after_shipping' ) >= 2 ||
        did_action( 'woocommerce_review_order_after_shipping' ) >= 2 ) {
        return;
    }

    // Chosen Shipping Method
    $chosen_shipping_zone_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_zone    = explode(':', $chosen_shipping_method_id)[0];
    $cart_subtotal             = WC()->cart->subtotal; 

    // Settings
    $cart_maximum1             = 29;
    $cart_maximum2             = 49;

    // HERE define the cart maximum 
    if ( $cart_subtotal < $cart_maximum2 && $chosen_shipping_zone != 'Shipping Zone I' ) {
        $cart_maximum = $cart_maximum1;
    } elseif ( $cart_subtotal < $cart_maximum2 && $chosen_shipping_method == 'Shipping Zone I' ) {
        $cart_maximum = $cart_maximum2;
    }

    // Display a message
    if( isset($cart_maximum) ){
        // The message
        $message =  sprintf( 'Above 29,00 € cart total save 5 €!' ,
            is_cart() ? 'Maximum' : 'Max',
            strip_tags( wc_price( $cart_maximum, array('decimals' => 2 ) ) )
        );

        // Display
        echo '</tr><tr class="shipping info"><th>&nbsp;</th><td data-title="Delivery info">'.$message.'</td>';
    }
}

But get some errors and my code doesn’t work. How I can display different Cart Messages based on shipping zones and a max cart subtotal?

2

Answers


  1. Chosen as BEST ANSWER

    I added your code to my page an changed the Names of Zone 1 and 2 to my Zone names. As you can see with "...Rheinbach..." and "...Irrel...".

    add_action( 'woocommerce_cart_totals_after_shipping' , 'shipping_zone_message_based_on_cart_amount' );
    add_action( 'woocommerce_review_order_after_shipping' , 'shipping_zone_message_based_on_cart_amount' );
    function shipping_zone_message_based_on_cart_amount() {
    
        // HERE in the array, define the shipping zone name with threshold max amount (pairs)
    
        $zone_name_threshold = array(
            'Rheinbach Lieferzone I' => 29,
            'Rheinbach Lieferzone II' => 29,
            'Irrel 20km Zone' => 49,
        'Irrel bis 30km Zone' => 49,
        'Irrel bis 40km Zone' => 49,
        'Irrel bis 60km Zone' => 49,
        'Irrel bis 80km Zone' => 49,
        );
    
        $chosen_method = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
        $instance_id   = end( explode(':', reset($chosen_method) ) );
        $shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id  );
        $zone_name     = $shipping_zone->get_zone_name();
    
    
        if( isset($zone_name_threshold[$zone_name]) && WC()->cart->subtotal < $zone_name_threshold[$zone_name]){
            echo '</tr>
            <tr class="shipping-info">
                <td data-title="Delivery info" class="deliveryinfo">' . sprintf(
                    __( "Ab 29,00 € Warenkorbwert entfällt der Mindermengenzuschlag von 5,00 €!", "woocommerce"), wc_price($zone_name_threshold[$zone_name])
                ) . '</td>';
        }
    }
    

  2. There are some mistakes and missing things in your code, try the following instead, to display a dynamic Cart Message based on the shipping zone and a threshold cart amount:

    add_action( 'woocommerce_cart_totals_after_shipping' , 'shipping_zone_message_based_on_cart_amount' );
    add_action( 'woocommerce_review_order_after_shipping' , 'shipping_zone_message_based_on_cart_amount' );
    function shipping_zone_message_based_on_cart_amount() {
        // HERE in the array, define the shipping zone name with threshold max amount (pairs)
        $zone_name_threshold = array(
            'Shipping Zone 1' => 29,
            'Shipping Zone 2' => 49,
        );
    
        // Get the customer shipping zone name
        $chosen_method = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
        $instance_id   = end( explode(':', reset($chosen_method) ) );
        $shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $instance_id  );
        $zone_name     = $shipping_zone->get_zone_name();
    
    
        if( isset($zone_name_threshold[$zone_name]) && WC()->cart->subtotal < $zone_name_threshold[$zone_name]){
            echo '</tr>
            <tr class="shipping-info">
                <th>&nbsp;</th>
                <td data-title="Delivery info">' . sprintf(
                    __( "Above %s cart amount, save 5 €!", "woocommerce"), wc_price($zone_name_threshold[$zone_name])
                ) . '</td>';
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search