visevo visevo - 7 months ago 84
PHP Question

Woocommerce cart modifications being overwritten by checkout.js

I'm hooking into Woocommerce's

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
- I can see it being modified after page load.

Here's my code in

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
file. Any ideas on how I can stop this from happening?

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

  • Add Fee

  • Negative discount


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