Home > Net >  display the price without discount in the subtotal cart
display the price without discount in the subtotal cart

Time:08-16

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

CodePudding user response:

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 );

CodePudding user response:

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 );
  • Related