Change how the Subtotal Price in cart being calculated

Question

I want to change the subtotal price in wooocommerce cart. If particular variations product containing more than 1 Quantity i want to change the subtotal price on cart. For example if product A have price 12 then users update the Quantity into 2 i want to show subtotal same 12. If Quantity is 3 then i want to show subtotal 24. Every time Quantity increasing i want to minus the regular price of product from subtotal. My current code is following its not working for me.

add_action( 'woocommerce_before_calculate_totals', 'misha_recalculate_price' );

function misha_recalculate_price( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
     $product_ids = array(2697);

    $quantity = 0;


    foreach ( $cart_object->get_cart() as $hash => $value ) {

        if( in_array( $value['variation_id'], $product_ids )  ) {


            $quantity += $value['quantity'];

        }


    }

    if( $quantity > 1 ) {
        foreach ( $cart_object->get_cart() as $hash => $value ) {



            if( in_array( $value['variation_id'], $product_ids ) ) {

                $newprice = echo $value['line_total'] - $value['data']->get_regular_price();

                 $cart_object->subtotal -= $value['data']->get_regular_price();

                $value['data']->set_price( $newprice );

            }

        }
    }

}
0
developerme 8 months 0 Answers 81 views 0

Leave an answer