Home Woocommerce cart modifications being overwritten by checkout.js
Reply: 2

Woocommerce cart modifications being overwritten by checkout.js

visevo Published in 2016-08-05 21:23:20Z

I'm hooking into Woocommerce's woocommerce_calculate_totals to set a minimum order amount for age verification. Basically the customer needs to have an order minimum of $1. This works on the view cart page, but the subtotal is getting overwritten by checkout.js - I can see it being modified after page load.

Here's my code in functions.php:

function hv_set_min_total() {
    if( is_checkout() ) {
        global $woocommerce;
        // set minimum cart total
        $min_cart_total = 1;

        // Total used before taxes and shipping
        $total = $woocommerce->cart->subtotal;

        // compare values
        if( $total < $min_cart_total ) {
            $diff = $min_cart_total - $total;
            $woocommerce->cart->subtotal += $diff;
            $woocommerce->cart->cart_contents_total += $diff;
            $woocommerce->cart->subtotal_ex_tax += $diff;
add_action( 'woocommerce_calculate_totals', 'hv_set_min_total' );

I've also tried hooking into various other hooks that all change the subtotal, but still get overwritten by the checkout.js file. Any ideas on how I can stop this from happening?

Other things I've tried, but still get removed:

  • Add Fee
  • Negative discount
Mauro Reply to 2016-08-06 04:34:30Z

I'm not getting well but let's see if this works for you, you are using the woocommerce_calculate_totals, but you are not returning anything.

What I would do is:

function totales ($totals){
    //echo "<pre>";
    //echo "</pre>";

    //All your code ...

    $totals->subtotal_ex_tax = ($totals->subtotal_ex_tax - 100000);
    $totals->cart_contents_total = ($totals->cart_contents_total - 15000);

    return $totals;


add_action('woocommerce_calculate_totals', 'totales', 10 , 1);

This way you can modify the subtotal and total but not the price of the product.

If you whant to know about the method that contains that hook go here

visevo Reply to 2016-08-08 23:39:39Z

As it turns out, apparently the is_checkout() method is returning false or not available when checkout.js runs, which resulted in the subtotal reverting back to its original state.

To anyone that's come across this issue: is_checkout() method is not available to checkout.js

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311248 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO