WooCommerce: Get Cart Info (total, items, etc) from $cart Object

As a WooCommerce development freelancer, every day I repeat many coding operations that I keep forgetting over and over again!

This means I have to search through the WooCommerce plugin files again and again and waste a lot of precious time.

We’ve already seen how to get $product and $order information from their respective objects , so this time we’ll take a look at the Cart page and answer to: “How to get ____ if I have the $cart variable/object available?“.

For example, “How can I get the cart total“? Or “How can I get the cart items“? Or maybe the cart fees, the applied coupons, the cart contents total, the total weight and so on…

Hopefully this article will help you save time as well! Your feedback via Twitter and the blog comments section is much appreciated. Enjoy!

1. If you have access to $cart variable

Hooks (do_action and apply_filters) use additional arguments which are passed on to the function. If they allow you to use the “$cart” object you’re in business.

But because this is quite rare, we’ll move on to step 2 straight away. Just keep in mind that should you have the “$cart” object at your disposal, this is the exact same as “WC()->cart” object, which you can call globally on any frontend section of your WooCommerce website.

In a nutshell:

$cart = WC()->cart;

2. If you don’t have access to $cart

If you don’t have direct access to the $cart object, you can invoke it globally on any page of your WooCommerce website. That’s the beauty of WC()->cart; the Cart page uses this method for example to load the cart object, and so can you, anywhere you like.

// $cart conditionals (if)
WC()->cart->is_empty()
WC()->cart->needs_payment()
WC()->cart->show_shipping()
WC()->cart->needs_shipping()
WC()->cart->needs_shipping_address()
WC()->cart->display_prices_including_tax()

// Get $cart totals
WC()->cart->get_cart_contents_count();
WC()->cart->get_cart_subtotal();
WC()->cart->subtotal_ex_tax;
WC()->cart->subtotal;
WC()->cart->get_displayed_subtotal();
WC()->cart->get_taxes_total();
WC()->cart->get_shipping_total();
WC()->cart->get_coupons();
WC()->cart->get_coupon_discount_amount( 'coupon_code' );
WC()->cart->get_fees();
WC()->cart->get_discount_total();
WC()->cart->get_total(); // formatted string $123.99 
WC()->cart->get_total( 'edit' ); // unformatted float 123.99
WC()->cart->total;
WC()->cart->get_tax_totals();
WC()->cart->get_cart_contents_tax();
WC()->cart->get_fee_tax();
WC()->cart->get_discount_tax();
WC()->cart->get_shipping_total();
WC()->cart->get_shipping_taxes();
 
// Loop over $cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
   $product = $cart_item['data'];
   $product_id = $cart_item['product_id'];
   $variation_id = $cart_item['variation_id'];
   $quantity = $cart_item['quantity'];
   $price = WC()->cart->get_product_price( $product );
   $subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] );
   $link = $product->get_permalink( $cart_item );
   // Anything related to $product, check $product tutorial
   $attributes = $product->get_attributes();
   $whatever_attribute = $product->get_attribute( 'whatever' );
   $whatever_attribute_tax = $product->get_attribute( 'pa_whatever' );
   $any_attribute = $cart_item['variation']['attribute_whatever'];
   $meta = wc_get_formatted_cart_item_data( $cart_item );
}

// Get $cart customer billing / shipping
WC()->cart->get_customer()->get_billing_first_name();
WC()->cart->get_customer()->get_billing_last_name();
WC()->cart->get_customer()->get_billing_company();
WC()->cart->get_customer()->get_billing_email();
WC()->cart->get_customer()->get_billing_phone();
WC()->cart->get_customer()->get_billing_country();
WC()->cart->get_customer()->get_billing_state();
WC()->cart->get_customer()->get_billing_postcode();
WC()->cart->get_customer()->get_billing_city();
WC()->cart->get_customer()->get_billing_address();
WC()->cart->get_customer()->get_billing_address_2();
WC()->cart->get_customer()->get_shipping_first_name();
WC()->cart->get_customer()->get_shipping_last_name();
WC()->cart->get_customer()->get_shipping_company();
WC()->cart->get_customer()->get_shipping_country();
WC()->cart->get_customer()->get_shipping_state();
WC()->cart->get_customer()->get_shipping_postcode();
WC()->cart->get_customer()->get_shipping_city();
WC()->cart->get_customer()->get_shipping_address();
WC()->cart->get_customer()->get_shipping_address_2();

// Other stuff
WC()->cart->get_cross_sells();
WC()->cart->get_cart_item_tax_classes_for_shipping();
WC()->cart->get_cart_hash();
WC()->cart->get_customer();

Related content

  • WooCommerce: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

29 thoughts on “WooCommerce: Get Cart Info (total, items, etc) from $cart Object

  1. How to get product name which was included in cart

    1. Hi Nilesh, you need to loop through the cart items, and then get the product (products) name:

      foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
         $product = $cart_item['data'];
         $name = $product ? $product->get_name() : '';
      }
      
  2. Hello Rodolfo,

    Thank you for all tips.

    Is it possible to add in the cart and order page, a row “Total HT” after the subtotal and shipping?
    What would be the php code?

    Add a “Total HT” excluding VAT line with the order + the shipping cost.
    Subtotal: 200 €.
    Shipping: 100 €.
    Total HT : 300 €

    Thank you

    1. Hello Pierre, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Since weeks I try to find out the correct typing to send cart informations for checkout in WooCommerce to Pinterest.
    I added in the header after the base tag:

      <script>
    
    pintrk('track', 'Purchase', {'value':'{{cart_total}}', order_quantity: '{{item.quantity}}'
    'currency':'EUR'});
    
    </script> 

    Big but: These are not the correct variables? Can you help me?

    1. Hi Ursula thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Hi, thank you for the post, really really really useful.

    I put this in a woocommerce_add_cart_item_data snippet:

        // GET THE PRICE OF THE ADDED PRODUCT
        $added_product = wc_get_product( $product_id );
        $added_product_price = $added_product->get_price();
        
        // GETTING THE TOTAL OF THE CART
        $MCG_current_cart_total = WC()->cart->total;
        
    $MCG_cart_total = $MCG_current_cart_total + $added_product_price;
    
    

    but in some how the last line hang up the page foreever… , and I don’t know why… do you have an idea?

    thank you

    1. Can’t really tell from those few lines of code, sorry

  5. Thank you so much, Rodolfo!

    You are providing highly valuable information and helping us to understand the mechanics of Woocommerce.
    Just found out what is the difference WC()->cart->get_subtotal() and WC()->cart->get_displayed_subtotal().

    You saved me a lot of time and headache! 🙂

  6. Just wanted to say thanks for providing this information. I hope it helps drive lots of paid work to you – you deserve it!

  7. It would be a great help
    Thanks in advance.

    1. Hey Sam, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom troubleshooting work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  8. i can get access to cart details in “woocommerce_before_calculate_totals” hook .
    But i can not manage to ***** Update the line_subtotal of a specific product *****
    Is there any way to do it using “woocommerce_before_calculate_totals” hook ?
    this is my code :

    
    foreach( $cart as $cart_item_id=>$citem ) {
        if ( $citem['product_id'] == $gift ) {
            $item_id = $cart_item_id;
            $grp = $citem['data']->get_regular_price();
            //$woocommerce->cart->cart_contents[$cart_item_id]['line_subtotal'] = 1;
           
            $woocommerce->cart->cart_contents[$cart_item_id]['line_subtotal'] = 1;
            //WC()->cart->set_session();
            
            //$woocommerce->cart->set_session();
        }
                                    
                                    
    }
    
    
    
  9. What would be the difference in:

    $cart->get_cart() as $cart_item

    vs

    WC()->cart->get_cart() as $cart_item

    ?

    They seem to work the same way for as far I’ve tested, but I notice some people use one and some people use the other.

    1. No difference.

      $cart = WC()->cart

  10. Hi Rodolfo, thanks for your article, it’s really helpful! One thing I’m struggling to get my head round, is the nature of WC()->cart in relation to the session variables. Is WC()->cart->get_cart() a method for getting data from server side session variables? How does this compare to WC()->session->cart? If the user is not logged in does it use the wc_cart_hash cookie to determine between sessions? Thanks, Adam

    1. Not 100% sure. However, I’ve always used get_cart() in my snippets so I never worry about sessions

  11. i can’t access my cart when i type : global $woocommerce; var_dump($woocommerce->cart);

    i get null is there any solution thanks in advance

  12. Hey,

    Your snippets are amazing. I have one quick question.

    Is there a way to get the total number of vendors in the cart? Using the wc vendors plugin here.

    I want to be able to charge COD per vendor shipped.

    1. Hi Lenold, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  13. Hey Rodolfo, I am interested in your articles, thank you. I wish to get something like $fontcolour = $cart_item[‘pewc_group_4988_4992’]; whew the pewc value is the select field, however it does not display anything… can you help?

    1. Hi Andrew, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  14. hi i want a code to display my cart id in checkout page before checkout

    1. Hi Adarsh, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  15. Nice Source for us, it is really helpful for us and whole information about Get cart info will bookmark this information

    1. Awesome!

Leave a Reply

Your email address will not be published. Required fields are marked *