On my WooCommerce checkout page, I am showing a notification before the order review section based on the cart total. I have included the following code in my functions.php
file of my child theme:
function action_woocommerce_checkout_before_order_review () {
// Get cart total
$cart_total = WC()->cart->get_cart_contents_total();
// Compare
if ( $cart_total == 0.01 ) {
echo '<p>' . __( 'My message', 'woocommerce' ) . '</p>';
}
}
add_action( 'woocommerce_checkout_before_order_review', 'action_woocommerce_checkout_before_order_review', 10, 0 );
The problem with this code is that the notice does not appear if a coupon is applied to the order, such that the cart total equals 0.01 euro, but instead only appears after the page is refreshed.
How can I alter this code such that the notice is added/removed through AJAX?
3
Answers
The following code solved my issue
If you need to add a custom message based on the cart total you can simply work with Javascript (or JQuery).
Using the hook
woocommerce_checkout_before_order_review
the message will be shown only if, when the page is loaded, the total of the cart is equal to0.01
.Here you can find the list of WooCommerce Javascript events:
You could then add something like this into your active theme’s functions.php file (it’s just an idea):
The following will show or hide your custom message based on checkout coupon events via Ajax and on cart contents total amount:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.