• Resolved cbhurji

    (@cbhurji)


    Hi,
    I have looked on-line and did see some topics discussing on how one can override woocommerce plugin functions and in my case I am trying to override “wc_cart_totals_order_total_html” found in wc-cart-functions.php

    I thought I could simply copy this file to my own child theme under a new folder -> “woocommerce/include” and then edit the relevant function, but this does not work for me.

    I would like a simple solution as all I want to do is to change the text below Total that appears on Cart(Basket) and Checkout page.
    Currently the following is displayed as example:
    Total £3.92
    (includes £0.66 VAT)

    I want to change “(includes £0.66 VAT)” to simply “(incl. VAT & delivery)”

    Someone please help

    Cbhurji

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Mike M. a11n

    (@mikedmoore)

    Automattic Happiness Engineer

    Hi there,

    The wc_cart_totals_order_total_htm can be filtered using the wc_cart_totals_order_total_html filter. That’s the best way to change this text. You likely use a translation plugin like Loco Translate to change the text as well. Here is the line where the “includes x.xx VAT” is added. The text is translatable.

    Here is the filter – https://github.com/woocommerce/woocommerce/blob/d7f768f77919a3f7a059d915894d0e87b2cb3ab1/includes/wc-cart-functions.php#L324

    I hope this helps!

    Thread Starter cbhurji

    (@cbhurji)

    Hello Mike
    First of all thx for taking time to respond

    Sorry can you please elaborate a bit more. From the code you pointed out i.e. on L324 is what I worked out as to where this string is generated, however how do I change it without affecting the original file in the woocommerce structure (due to upgrades).

    You mentioned applying filter to this function… how do you do this and does this code sit in my child theme? Any example you can share please?

    Plugin Support Ryan Ray, a11n

    (@ryanr14)

    Hi @cbhurji,

    Sorry for any confusion caused by our initial answer. To help clarify you will need to create a custom function where you can then use this filter. That function is a PHP snippet, so you’d can add that to your site in a few ways.

    If your child theme is custom, I would then add your function to the child themes functions.php file. If that child theme is made by another developer and it might get updated and overwrite your changes I would then use a plugin like Code Snippets to store your custom code. The following snippet then just slightly modifies the text output for what you were after.

    function custom_cart_totals_order_total_html( $value ){
        $value = '<strong>' . WC()->cart->get_total() . '</strong> ';
    
    // If prices are tax inclusive, show taxes here.
    	if ( wc_tax_enabled() && WC()->cart->display_prices_including_tax() ) {
    		$tax_string_array = array();
    		$cart_tax_totals  = WC()->cart->get_tax_totals();
    		if ( get_option( 'woocommerce_tax_total_display' ) === 'itemized' ) {
    			foreach ( $cart_tax_totals as $code => $tax ) {
    				$tax_string_array[] = sprintf( '%s %s', $tax->formatted_amount, $tax->label );
    			}
    		} elseif ( ! empty( $cart_tax_totals ) ) {
    			$tax_string_array[] = sprintf( '%s %s', wc_price( WC()->cart->get_taxes_total( true, true ) ), WC()->countries->tax_or_vat() );
    		}
    
            if ( ! empty( $tax_string_array ) ) {
                $taxable_address = WC()->customer->get_taxable_address();
                $estimated_text  = '';
                $value .= '<small class="includes_tax">' . sprintf( __( '(incl. VAT & delivery)', 'woocommerce' ), implode( ', ', $tax_string_array ) . $estimated_text ) . '</small>';
            }
        }
        return $value;
    }
    
    add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_cart_totals_order_total_html', 20, 1 );

    The important part here being the text inside these parenthesis, which looks like this in Storefront.


    Link to image: https://cld.wthms.co/jV0kBT

    &


    Link to image: https://cld.wthms.co/lud7e6

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Override wc_cart_totals_order_total_html in the includes directory’ is closed to new replies.