skip to Main Content

i want when the product has a discount, in the cart, in the subtotal instead of showing the price with the discount, it will show the price without the discount, see the picture below and you will understand :
enter image description here

i use the following hook, when 1 product is in the cart, it displays correctly, but when a product with a price of $20 is added to the cart, instead of the sum of 2 products, the price of the second product, which is $20, is displayed in subtotal to give

function filter_woocommerce_cart_subtotal( $subtotal, $compound, $cart ) {
    foreach ( WC()->cart->get_cart() as $key => $cart_item ) {
        $subtotal = wc_price( $cart_item['data']->get_regular_price() * $cart_item['quantity'] );
    }
    return $subtotal;
} add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );

thank you for your time and answering my question

2

Answers


  1. Chosen as BEST ANSWER
    function filter_woocommerce_cart_subtotal( $subtotal, $compound, $cart ) {
        $subtotal = 0;
        foreach ( WC()->cart->get_cart() as $key => $cart_item ) {
            $subtotal += $cart_item['data']->get_regular_price() * $cart_item['quantity'];
        }
        $subtotal = wc_price ( $subtotal ); 
        return $subtotal;
    } add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
    

  2. it looks like you’re always overwriting your $subtotal instead of adding to it

    function filter_woocommerce_cart_subtotal( $subtotal, $compound, $cart ) {
        $subtotal = 0;
            foreach ( WC()->cart->get_cart() as $key => $cart_item ) {
                $subtotal += intval(wc_price( $cart_item['data']->get_regular_price() * $cart_item['quantity'] ));
                     // ^^^^^^^^^ change is here
            }
        return $subtotal;
    } add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search