Subtotal cart variable php WooCommerce Subtotal cart variable php WooCommerce wordpress wordpress

Subtotal cart variable php WooCommerce


There is multiple ways to get cart subtotal:

  1. With global $woocommerce; variable (the old way-:

    global $woocommerce;
    $woocommerce->cart->subtotal;// or$woocommerce->cart->get_cart_subtotal()

  2. Without global $woocommerce; variable using simply WC() (nowadays):

    WC()->cart->subtotal// OrWC()->cart->get_cart_subtotal()

References:


I kept getting an incorrect subtotal (a little high, but could not determine why) using Loic's code snippets. I tried every similar variant and eventually found the following code:

WC()->cart->get_subtotal();

Using this snippet above, I get precisely the amount expected which matches the actual subtotal displayed in the cart.

Now my 'free shipping' upsell calculation is working perfectly.