Reputation: 539
I've tried working from this example (Dynamic information based on shipping method and cart total In Woocommerce), however my dynamic message shows twice. Any help is greatly appreciated.
Essentially what I am trying to do is display a message if the user's shipping is out of the delivery area (using WooCommerce's "Locations not covered by your other zones" Shipping zone). The message shows correctly, only when the user is not in a defined shipping zone - but it doubles up. One seems to be within the ajax refresh, and the other seems to be outside of it. Not exactly sure how or why it's doing that.
//add message to CHECKOUT if outside delivery area
add_action( 'woocommerce_review_order_after_shipping', 'outside_delivery_checkout', 20);
function outside_delivery_checkout() {
global $woocommerce;
if ( did_action( 'woocommerce_review_order_after_shipping' ) >= 2 ) {
return once;
}
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
$cart_subtotal = WC()->cart->subtotal;
if ($cart_subtotal && $chosen_shipping_method === 'free_shipping') {
echo '<div class="outside-delivery checkout"><b>PLEASE NOTE:</b><br /> Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge. <strong>Please refresh the page after updating your shipping settings.</strong></div>';
} elseif ($cart_subtotal && $chosen_shipping_method != 'free_shipping') {
// do nothing
}
}
Here is a link to the issue: https://redstarrolloffqc.com/dumpster-sizes-rates/ - add a product and go to the checkout page.
Upvotes: 1
Views: 1023
Reputation: 254182
The main problem here is that you are trying to insert a <div>
html tag with some content, inside an html <table>
just after a closing </tr>
html tag…
So the displayed Html breaks, even if it doesn't seem.
Instead you should use the following html structure:
<tr><td colspan="2"><div>Your formatted text (message)</div></td></tr>
Now did_action()
condition has no effect in your code, and global $woocommerce
is not required (and outdated since a while).
This way your content will be displayed in a table row, as it should.
This will solve definitively your issue…
So your code will be like:
//add message to CHECKOUT if outside delivery area
add_action( 'woocommerce_review_order_after_shipping', 'outside_delivery_checkout', 20);
function outside_delivery_checkout() {
$chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping_method = explode(':', $chosen_shipping_method_id)[0];
$cart_subtotal = WC()->cart->subtotal;
if ( $cart_subtotal && $chosen_shipping_method === 'free_shipping' ) {
$html = '<tr class="delivery-message"><td colspan="2"><div class="outside-delivery checkout">
<strong>' . __("PLEASE NOTE:") . '</strong><br />';
$html .= __("Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge.");
$html .= ' <strong>' . __("Please refresh the page after updating your shipping settings.") . '</strong>';
echo $html . '</div></td></tr>';
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Upvotes: 2