最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Change how the Subtotal Price in cart being calculated

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

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

            }

        }
    }

}
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

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

            }

        }
    }

}
Share Improve this question edited Dec 13, 2019 at 5:17 Reigel Gallarde 88911 silver badges23 bronze badges asked Dec 13, 2019 at 4:35 developermedeveloperme 1415 silver badges18 bronze badges 4
  • can you include the reason as to why you're doing this? – Reigel Gallarde Commented Dec 13, 2019 at 4:48
  • The reason is want to give product A as free gift when cart quantity more than 25 doller. So when Product B added into cart Then product A also added to cart automatically when cart total meet more than 25 doller. Also when user purchase Product A more than 2 or 3 qty at that time cart total meet 25 so i want to give 1 qty as free at that time. – developerme Commented Dec 13, 2019 at 4:51
  • if you want it as free then setting the price to 0 would be it right? like $value['data']->set_price(0);? that is for product A – Reigel Gallarde Commented Dec 13, 2019 at 4:54
  • At that time its not changing the subtotal value that is the issue – developerme Commented Dec 13, 2019 at 4:55
Add a comment  | 

1 Answer 1

Reset to default 0

If I understand your goal correctly, I see a few problems with your code..

First is the echo in $newprice = echo $value['line_total'] - $value['data']->get_regular_price(); Please remove the echo.

Second, I think you are better of using the "after", that is woocommerce_after_calculate_totals. You are using the "before", that is after your code runs, line_total will be changed.

发布评论

评论列表(0)

  1. 暂无评论