I want the text "Total" on WooCommerce cart and checkout page (see attached image) to be changed if there is a specific product ID in cart.
I tried achieving this in javascript but it will just apply to all:
<script type="text/javascript">
(function($) {
$(document).ready(function() {
$('#your_my_order_element_id').html('Your New string');
//$('.your_my_order_element_class').html('Your New string');
});
})(jQuery);
</script>
Can someone push me in the right direction? Any help will be appreciated!
My Answer:
To achieve this in PHP, you can use the `woocommerce_cart_totals_total` filter hook to change the text "Total" based on the specific product ID in the cart. Here's an example code snippet to get you started:
php
add_filter( 'woocommerce_cart_totals_total', 'change_total_text_based_on_product_id', 10, 1 );
function change_total_text_based_on_product_id( $total ) {
// Check if specific product ID is in the cart
$specific_product_id = 123; // Change this to your specific product ID
$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item_key => $cart_item ) {
if ( $cart_item['product_id'] == $specific_product_id ) {
$total = 'Your New Total Text';
break;
}
}
return $total;
}
Replace `123` with the actual product ID you want to check for. This code will change the text "Total" to "Your New Total Text" if the specific product ID is in the cart. Just add this code to your theme's `functions.php` file or a custom plugin.
Remember to test this code on a staging site before implementing it on a live site. Let me know if you need further assistance!
Rate this post
5 of 5 based on 9024 votesComments