9

Does anyone know what $variable to use in php to get the subtotal of the cart?
Subtotal = total value of cart BEFORE discount or taxes.

It would help me a lot. I want to add an extra fee if the subtotal is above 250 euro.

Thanks in advance.

Aleksandar
  • 1,496
  • 1
  • 18
  • 35
Cedric De Clercq
  • 163
  • 2
  • 2
  • 4
  • you can check here http://woocommerce.wp-a2z.org/oik_api/wc_cartget_cart_subtotal/ –  Jun 06 '16 at 18:24

2 Answers2

24

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 // Or WC()->cart->get_cart_subtotal()

References:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks a lot! Any idea how I can get the shipping country? – Cedric De Clercq Jun 08 '16 at 20:33
  • Thanks but it doesn't work.. I would like the Woocommerce shipping country to use in an if-rule in functions.php to add an extra fee. So 'if shipping country is BE (Belgium), the fee is €20'. It's just the 'if shipping country is BE' that i'm struggling with. I can't find the right code – Cedric De Clercq Jun 09 '16 at 21:00
  • is there anyway to change the subtotal of cart using this? – mapmalith Aug 23 '17 at 09:35
10

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.

MarkPraschan
  • 560
  • 6
  • 8
  • **NB** **get_cart_subtotal()** `If the cart has compound tax, we want to show the subtotal as cart + shipping + non-compound taxes (after discount).` https://woocommerce.wp-a2z.org/oik_api/wc_cartget_cart_subtotal/ **get_subtotal()** is just the goods subtotal before tax. I also had issues! – TaoRich Feb 25 '22 at 12:20