Hello,

I have made some changes in Woocommerce core files (plugins\woocommerce\includes\class-wc-cart-totals.php) and I copied those changes in my child theme directory at themes\child-theme\woocommerce\includes\class-wc-cart-totals.php
but it doesn't seem to work.
I tried using "require 'themes\child-theme\woocommerce\includes\class-wc-cart-totals.php' " in my functions.php, but it attempts to load both child and parent files, and I receive an error that the class is already declared.

How can I achieve this requirement?

Thank you!

    Are you trying to override a template file or the actual logic file? Anything in the Woocommerce plugin /templates directory can be overridden by a file in your theme as long as it's in a /woocommerce directory of its own and follows the plugin-in's directory structure (minus the templates parent). However, if you're trying to alter the functionality you'll have to use the filters and actions from your functions.php file. For instance, if you're trying to adjust the cart total calculations you can use the woocommerce_calculate_totals action. There are several filters available as well.

      Hello, thanks for your reply.

      Yes, I'm trying to override the actual logic file.

      The changes are in the functions calculate_item_totals() and calculate_discounts() in 'class-wc-cart-totals.php' but I couldn't find a way(filter/actions) to change what I want.

      Change in calculate_item_totals(): replace $item->total + $item->price with $item->price
      $total_taxes = apply_filters( 'woocommerce_calculate_item_totals_taxes', WC_Tax::calc_tax( $item->total + $item->price, $item->tax_rates, $item->price_includes_tax ), $item, $this );

      Change in calculate_discounts(): Comment-out the lines where it applies taxes to coupon.

      I tried to use woocommerce_calculate_totals hook but couldn't manage to change the tax totals as I want it. Do you have any tips on how to accomplish that?

      Thank you

        In calculate_item_totals the second parameter passed to your function in the woocommerce_calculate_item_total_taxes filter is the instance of $item - just return $item->cost.

        For the calculate_discounts() challenge, you may have to find the WC_Tax class and see what that has to offer.

        maxxd Ok I will try that, thanks. What about the second change on which taxes are applied to the coupon?

          I think maxxd said something about that:

          For the calculate_discounts() challenge, you may have to find the WC_Tax class and see what that has to offer.

            Hello,
            I've checked in the calculate_discounts() function but couldn't use anything to override these calculations:

            	protected function calculate_discounts() {
            		$this->get_coupons_from_cart();
            
            		$discounts = new WC_Discounts( $this->cart );
            
            		// Set items directly so the discounts class can see any tax adjustments made thus far using subtotals.
            		$discounts->set_items( $this->items );
            
            		foreach ( $this->coupons as $coupon ) {
            			$discounts->apply_coupon( $coupon );
            		}
            
            		$coupon_discount_amounts     = $discounts->get_discounts_by_coupon( true );
            		$coupon_discount_tax_amounts = array();
            
            		// See how much tax was 'discounted' per item and per coupon.
            		if ( $this->calculate_tax ) {
            			foreach ( $discounts->get_discounts( true ) as $coupon_code => $coupon_discounts ) {
            				$coupon_discount_tax_amounts[ $coupon_code ] = 0;
            
            				foreach ( $coupon_discounts as $item_key => $coupon_discount ) {
            					$item = $this->items[ $item_key ];
            
            					if ( $item->product->is_taxable() ) {
            						// Item subtotals were sent, so set 3rd param.
            						$item_tax = array_sum( WC_Tax::calc_tax( $coupon_discount, $item->tax_rates, $item->price_includes_tax ) );
            
            						// Sum total tax.
            						$coupon_discount_tax_amounts[ $coupon_code ] += $item_tax;
            
            						// Remove tax from discount total.
            						if ( $item->price_includes_tax ) {
            							$coupon_discount_amounts[ $coupon_code ] -= $item_tax;
            						}
            					}
            				}
            			}
            		}
            
            		$this->coupon_discount_totals     = (array) $discounts->get_discounts_by_item( true );
            		$this->coupon_discount_tax_totals = $coupon_discount_tax_amounts;
            
            		if ( wc_prices_include_tax() ) {
            			$this->set_total( 'discounts_total', array_sum( $this->coupon_discount_totals ) - array_sum( $this->coupon_discount_tax_totals ) );
            			$this->set_total( 'discounts_tax_total', array_sum( $this->coupon_discount_tax_totals ) );
            		} else {
            			$this->set_total( 'discounts_total', array_sum( $this->coupon_discount_totals ) );
            			$this->set_total( 'discounts_tax_total', array_sum( $this->coupon_discount_tax_totals ) );
            		}
            
            		$this->cart->set_coupon_discount_totals( wc_remove_number_precision_deep( $coupon_discount_amounts ) );
            		$this->cart->set_coupon_discount_tax_totals( wc_remove_number_precision_deep( $coupon_discount_tax_amounts ) );
            
            		// Add totals to cart object. Note: Discount total for cart is excl tax.
            		$this->cart->set_discount_total( $this->get_total( 'discounts_total' ) );
            		$this->cart->set_discount_tax( $this->get_total( 'discounts_tax_total' ) );
            	}

            Sorry if I'm missing something obvious here, I'm really new to Wordpress development 🙂

            Thank you

              If you follow the logic of line 790: if ( $item->product->is_taxable() ) it'll take you to abstract-wc-product::is_taxable(), which introduces the 'woocommerce_product_is_taxable' filter. It looks like the second parameter passed via this filter is the product itself - you should be able to check the product and decide whether or not the product is taxable.

                Write a Reply...