Adding price to woocommerce cart item with get_price gets multiplied with 3?

admin2025-06-06  6

Similar question: WooCommerce Dynamic Cart Pricing

function odb_add_length_price( $cart_object ) {
    if( !(is_admin() && !defined('DOING_AJAX'))) {
        foreach ( $cart_object->get_cart() as $cart_item ) {
            $price = $cart_item['data']->get_price();

            $price = 1 + $price;

            $cart_item['data']->set_price( $price ); 
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'odb_add_length_price', 10, 1);

If the product price is set to 100, then the total should be 101. The price returned is set to 103, for some reason it gets multiplied by 3.

If i remove the $price and set a static value to 100, the returned price is 101.

If i only return the price, the prices returns as 100.

So each time i combine or calculate get_price plus x, it multiplies by 3.

Similar question: WooCommerce Dynamic Cart Pricing

function odb_add_length_price( $cart_object ) {
    if( !(is_admin() && !defined('DOING_AJAX'))) {
        foreach ( $cart_object->get_cart() as $cart_item ) {
            $price = $cart_item['data']->get_price();

            $price = 1 + $price;

            $cart_item['data']->set_price( $price ); 
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'odb_add_length_price', 10, 1);

If the product price is set to 100, then the total should be 101. The price returned is set to 103, for some reason it gets multiplied by 3.

If i remove the $price and set a static value to 100, the returned price is 101.

If i only return the price, the prices returns as 100.

So each time i combine or calculate get_price plus x, it multiplies by 3.

Share Improve this question edited Oct 31, 2018 at 14:22 Nicolai Byø Friis asked Oct 30, 2018 at 9:53 Nicolai Byø FriisNicolai Byø Friis 11 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The solution was to add remove_action( 'woocommerce_before_calculate_totals', 'odb_add_length_price', 10, 1); on the first line in the function.

This removes the function each time it run and works like expected.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749185782a317080.html

最新回复(0)