on this question /questions/52747221/display-a-custom-message-based-on-customer-shipping-zone-in-woocommerce/78433091#78433091
I found an solution from @LoicTheAztec how to show Cart messages based on shipping zones.
It is the code below.
can someone explain me, how I can use this code and combine it with an cart amount smaller than …€?
More detail: I want to show a message based on shipping zones and only if the cart amount is smaller than x €
I need sth like this:
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
Is this possible? Would be great, if someone can help me!
This is the code to show an message based on shipping zone:
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>';
}
}
I tried to edit the php code by myself, but only errors appear. My PHP knowledges are to small for this 🙁
I tried this:
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> </th><td data-title="Delivery info">'.$message.'</td>';
}
}```
Miri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.