• The price is added twice in the total. if you add a product in cart and selected the price, the total price is correct in the checkout, but if the customer create his account in same process than his checkout then the price is added twice after proced payment. This is my code

    function calculate_gift_wrap_fee( $cart_object ) {
    /* Gift wrap price */
    $additionalPrice = 100;
    foreach ( $cart_object->cart_contents as $key => $value ) {
    if( isset( $value[“gift_wrap_fee”] ) ) {
    $orgPrice = floatval( $value[‘data’]->price );
    $value[‘data’]->price = ( $orgPrice + $additionalPrice );
    }
    }
    }
    add_action( ‘woocommerce_before_calculate_totals’, ‘calculate_gift_wrap_fee’, 99 );

    https://www.ads-software.com/plugins/woocommerce/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    If calculate totals happens twice, your changes will stack since you’re changing the product.

    use the fees api https://docs.woothemes.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/

    Thread Starter tst2016

    (@tst2016)

    Hello Mike,

    Thank you

    Basically my code is working fine if the customer is not creating his account in same process but additional percentage add twice if the customer is creating his account in same process. That is the issue.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    For the reason above ^. It’s not suitable – the changes the products can occur multiple times.

    Thread Starter tst2016

    (@tst2016)

    Thanks Mike,

    So i need to use woocommerce_cart_calculate_fees instead of woocommerce_before_calculate_totals.

    function calculate_gift_wrap_fee( $cart_object ) {
    /* Gift wrap price */
    $additionalPrice = 100;
    foreach ( $cart_object->cart_contents as $key => $value ) {
    if( isset( $value[“gift_wrap_fee”] ) ) {
    $orgPrice = floatval( $value[‘data’]->price );
    $value[‘data’]->price = ( $orgPrice + $additionalPrice );
    }
    }
    }
    add_action( ‘woocommerce_cart_calculate_fees’, ‘calculate_gift_wrap_fee’, 99 );

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Well the idea would be to add a fee (via the api – doc I linked to has an example) to add extra costs, rather than change the product prices.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘woocommerce_before_calculate_totals not working properly’ is closed to new replies.