Ad
Change "Total" Text When Specific Products In Cart On WooCommerce Cart And Checkout Pages
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!
Ad
Answer
To change the total text on the cart and checkout page, you could edit the template file. As can be found in templates/cart/cart-totals.php
This template can be overridden by copying it to yourtheme/woocommerce/cart/cart-totals.php.
So replace (Line 97 - 100 - @version 2.3.6)
<tr class="order-total">
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
</tr>
With
<tr class="order-total">
<?php
// The targeted product ids, multiple product IDs can be entered, separated by a comma
$targeted_ids = array( 30, 815 );
// Flag, false by default
$flag = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$flag = true;
break;
}
}
// True
if ( $flag ) {
?>
<th><?php esc_html_e( 'Authorize', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Authorize', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
<?php
} else {
?>
<th><?php esc_html_e( 'Total', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total', 'woocommerce' ); ?>"><?php wc_cart_totals_order_total_html(); ?></td>
<?php
}
?>
</tr>
OR instead of overwriting the template file, use gettext() filter.
Code goes in functions.php
file of the active child theme (or active theme)
function filter_gettext( $translated, $original_text, $domain ) {
// Is admin
if ( is_admin() ) return $translated;
// No match
if ( $original_text != 'Total' ) return $translated;
// The targeted product ids, multiple product IDs can be entered, separated by a comma
$targeted_ids = array( 30, 815 );
// Flag, false by default
$flag = false;
// WC Cart
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$flag = true;
break;
}
}
}
// True
if ( $flag ) {
$translated = __( 'Authorize', 'woocommerce' );
}
return $translated;
}
add_filter( 'gettext', 'filter_gettext', 10, 3 );
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad