Title: Subtotal cart variable php
Last modified: August 31, 2016

---

# Subtotal cart variable php

 *  [cedricdc](https://wordpress.org/support/users/cedricdc/)
 * (@cedricdc)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/subtotal-cart-variable-php/)
 * Hi,
 * Does anyone 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.
    A want to add an extra fee if the subtotal is above 
   250 euro.
 * Thanks in advance.
 * Kind regards,
    Cedric
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [cedricdc](https://wordpress.org/support/users/cedricdc/)
 * (@cedricdc)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/subtotal-cart-variable-php/#post-7452302)
 * I found something like this, be it doesn’t seem to work..
 *     ```
       $session_cart = $woocommerce->session->cart;
       	if(count($session_cart) > 0) {
       		$total = 0;
       		foreach($session_cart as $cart_product)
       			$total = $total + $cart_product['line_subtotal'];
       	}
       ```
   
 *  Thread Starter [cedricdc](https://wordpress.org/support/users/cedricdc/)
 * (@cedricdc)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/subtotal-cart-variable-php/#post-7452304)
 * To be clear, I want to use following rules:
    When you chose to pick up to items:
   extra fee = 0 When you chose to deliver the items: – Amount (subtotal before 
   discount) is below €250: – between 1 – 6 items: extra fee = 8,7 – between 7 –
   12 items: extra fee = 10,6 – … – Amount (subtotal before discount) is above €
   250: extra fee = 0
 * I have following code:
 *     ```
       add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
       function add_custom_fees( WC_Cart $cart ){
       	$fees = 0;
       	$aantal_items = 0;
       	$min_order = 250.00;
   
       	$country_shipping = WC()->countries->countries[ $order->shipping_country ];
       	$country = $country_shipping[0];
   
       	$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
       	$chosen_shipping = $chosen_methods[0];
   
       	foreach( $cart->get_cart() as $item ){
       	$aantal_items = $aantal_items + $item['quantity'];
       	}
   
       	$session_cart = $woocommerce->session->cart;
       	if(count($session_cart) > 0) {
       		$total = 0;
       		foreach($session_cart as $cart_product)
       			$total = $total + $cart_product['line_subtotal'];
       	}
   
       //PICKUP
            if ($chosen_shipping == 'local_pickup') {$fees = 0;}
       //DELIVERY
            elseif ($chosen_shipping == 'woocommerce_flatrate_percountry') {
   
       if ($total < $min_order and $aantal_items >= 1 and $aantal_items <= 6) {$fees = 8.7;}
       elseif ($total < $min_order and $aantal_items >= 7 and $aantal_items <= 12) {$fees = 10.6;}
       elseif ($total < $min_order and $aantal_items >= 13 and $aantal_items <= 18) {$fees = 11.8;}
       elseif ($total < $min_order and $aantal_items >= 19 and $aantal_items <= 24) {$fees = 20.5;}
       elseif ($total < $min_order and $aantal_items >= 25 and $aantal_items <= 30) {$fees = 22.4;}
       elseif ($total < $min_order and $aantal_items >= 31 and $aantal_items <= 36) {$fees = 23.6;}
       elseif ($total < $min_order and $aantal_items >= 37 and $aantal_items <= 42) {$fees = 32.3;}
       elseif ($total < $min_order and $aantal_items >= 43 and $aantal_items <= 48) {$fees = 34.2;}
       elseif ($total < $min_order and $aantal_items >= 49 and $aantal_items <= 54) {$fees = 35.4;}
       elseif ($total >= $min_order) {$fees = 0;}
       else {$fees = 60;}
   
       	 }
   
               $cart->add_fee( 'Verpakkingskosten', $fees);
           }
       ```
   
 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/subtotal-cart-variable-php/#post-7452361)
 *     ```
       WC()->cart->subtotal_ex_tax;
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Subtotal cart variable php’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * Last activity: [9 years, 11 months ago](https://wordpress.org/support/topic/subtotal-cart-variable-php/#post-7452361)
 * Status: not resolved