0

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.