If you need to add a custom message based on the cart total you can simply work with Javascript (or JQuery).
Using the hookwoocommerce_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):
// show or hide a custom message in checkout based on cart total
add_action( 'wp_footer', 'show_hide_message_based_on_cart_total' );
function show_hide_message_based_on_cart_total() {
?>
<script type="text/javascript">
// the script is triggered every time the checkout updates
jQuery(document.body).on('updated_checkout', function(){
// gets the values of the cart total and the discount total by removing the currency symbol and converting the string to a number
var cartTotal = jQuery('table.shop_table tr.cart-subtotal bdi').text().replace("€","").replace(",",".").trim();
var coupon = jQuery('table.shop_table tr.cart-discount span.amount').text().replace("€","").replace(",",".").trim();
if ( cartTotal - coupon == 0.01 ) {
// show the message
jQuery('<span id="custom_message">Custom Message</span>').insertBefore('#order_review');
} else {
// removes the message
jQuery('#custom_message').remove();
}
});
</script>
<?php
}
The following will show or hide your custom message based on checkout coupon events via Ajax and on cart contents total amount:
add_action( 'woocommerce_checkout_before_order_review', 'action_woocommerce_checkout_before_order_review', 10, 0 );
function action_woocommerce_checkout_before_order_review () {
// Here below set your message
$message = __( 'My message', 'woocommerce' );
$threshold = 0.01; // Here your targeted subtotal amount
$cart_total = WC()->cart->get_cart_contents_total();
echo '<p class="custom-message" style="'. ( $cart_total > $threshold ? '' : 'display:none;' ) .'">' . $message . '</p>';
// jQuery Ajax
wc_enqueue_js( "jQuery( function($){
if (typeof wc_checkout_params === 'undefined') return false;
function checkoutCouponEvent() {
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'checkout_coupon',
'ccevent': 'Yes'
},
success: function (response) {
if ( parseFloat(response) == " . $threshold . " ) {
$('.custom-message').show();
} else {
$('.custom-message').hide();
}
}
});
}
$(document.body).on('applied_coupon_in_checkout removed_coupon_in_checkout', function( event ){
setTimeout(function(){
checkoutCouponEvent();
}, event.type === 'removed_coupon_in_checkout' ? 350 : 0 );
});
});" );
}
// Ajax receiver function
add_action( 'wp_ajax_checkout_coupon', 'checkout_coupon_ajax_receiver' );
add_action( 'wp_ajax_nopriv_checkout_coupon', 'checkout_coupon_ajax_receiver' );
function checkout_coupon_ajax_receiver() {
if ( isset($_POST['ccevent']) && $_POST['ccevent'] ) {
echo WC()->cart->get_cart_contents_total();
}
wp_die();
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
The following code solved my issue
function action_woocommerce_checkout_before_order_review () {
$cart_total = WC()->cart->get_cart_contents_total();
if ( $cart_total == 0.01 ) {
echo '<p class="notice">' . __( 'My message', 'woocommerce' ) . '</p>';
}
?>
<script type="text/javascript">
jQuery('.woocommerce-remove-coupon').click(function(){
jQuery('.notice').hide();
});
</script>
<?php
}
add_action( 'woocommerce_review_order_before_cart_contents', 'action_woocommerce_checkout_before_order_review', 10, 0 );
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.