Overriding Woocommerce’s core functionality (order calculations)

Question

I need to implement some custom logic during the Cart items calculation and the code is located in ‘class-wc-cart-totals.php’ .

protected function calculate_item_subtotals() {
        $merged_subtotal_taxes = array(); // Taxes indexed by tax rate ID for storage later.

        $adjust_non_base_location_prices = apply_filters( 'woocommerce_adjust_non_base_location_prices', true );
        $is_customer_vat_exempt          = $this->cart->get_customer()->get_is_vat_exempt();

        foreach ( $this->items as $item_key => $item ) {
            if ( $item->price_includes_tax ) {
                if ( $is_customer_vat_exempt ) {
                    $item = $this->remove_item_base_taxes( $item );
                } elseif ( $adjust_non_base_location_prices ) {
                    $item = $this->adjust_non_base_location_price( $item );
                }
            }

            $item->subtotal = $item->price;

            if ( $this->calculate_tax && $item->product->is_taxable() ) {
                $item->subtotal_taxes = WC_Tax::calc_tax( $item->subtotal, $item->tax_rates, $item->price_includes_tax );
                $item->subtotal_tax   = array_sum( array_map( array( $this, 'round_line_tax' ), $item->subtotal_taxes ) );

                if ( $item->price_includes_tax ) {
                    // Use unrounded taxes so we can re-calculate from the orders screen accurately later.
                    $item->subtotal = $item->subtotal - array_sum( $item->subtotal_taxes );
                }

                foreach ( $item->subtotal_taxes as $rate_id => $rate ) {
                    if ( ! isset( $merged_subtotal_taxes[ $rate_id ] ) ) {
                        $merged_subtotal_taxes[ $rate_id ] = 0;
                    }
                    $merged_subtotal_taxes[ $rate_id ] += $this->round_line_tax( $rate );
                }
            }

            if ( true) ) { 
                //
                //custom logic here
                //              
            }

        $items_subtotal = $this->get_rounded_items_total( $this->get_values_for_total( 'subtotal'));
    }

How can I implement the logic in child functions.php without changing the core php file?
Thanks

0
joepr 8 months 2021-12-23T19:58:44-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse