简体   繁体   中英

Woocommerce mini cart subtotal does not update correctly after currency change

Is there any JavaScript or PHP solution to recalculate and replace Subtotal of mini cart based on available price values on page reload?

Currently I have two different currencies, for example Euro & Lira, when I add some items to cart in Euro currency the subtotal is correct but when I reload page in Lira currency the Subtotal value for added items is obviously wrong, so I need to recalculate subtotal for available items on page load and replace it with the returning subtotal value by Woocommerce.

Can you please help me to do it?


Update : I tried a tweak but still does not work. By default mini-cart:php use widget hook to display total or subtotal:

do_action( 'woocommerce_widget_shopping_cart_total' );

I tried this code instead but still on page load with a different currency it does not display correct subtotal (it returns a value similar to subtoal of other currency):

  <p class="woocommerce-mini-cart__total total">
        <?php
            $subtotal_txt   = isset($options['sc-subtotal-text']) ? $options['sc-subtotal-text']: __("Subtotal:",'side-cart-woocommerce');
        ?>
    <span><?php esc_attr_e($subtotal_txt,'side-cart-woocommerce') ?></span> 
    <?php echo WC()->cart->get_cart_subtotal(); ?>
    </p>

Should I add a JS or Ajax fragments to have a correct subtotal on every page load?

Answer : The important point is calling calculate_totals() , It seems like a bug. Anyway you may use this code instead:

<p class="woocommerce-mini-cart__total total">
    <?php 
         WC()->cart->calculate_totals();
         do_action( 'woocommerce_widget_shopping_cart_total' );
    ?>
</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM