$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Adding price to woocommerce cart item with get_price gets multiplied with 3?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far