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();
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();

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.

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

  1. 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!

  2. 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

  3. 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! ๐Ÿ™‚

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

  5. 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!

  6. 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();
        }
                                    
                                    
    }
    
    
    
  7. 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

  8. 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

  9. 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

  10. 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!

  11. 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!

  12. 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!

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

    1. Awesome!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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