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 questionI 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 questionI 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
|
1 Answer
Reset to default 0If 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.
$value['data']->set_price(0);
? that is for product A – Reigel Gallarde Commented Dec 13, 2019 at 4:54