How can I get WooCommerce Cart Total

WooCommerce or WordPress theme developers had to search on Google every day to find lots of code to integrate client design into WooCommerce themes.  some designs need some more extraordinary efforts and extra resources to complete them. For Advance WooCommerce theme development and sometimes to match with the design we have to show the WooCommerce Cart total in theme.
How can I get WooCommerce Cart Total

In WooCommerce there is most of them build-in solutions for developers. I will try to explain how to get the WooCommerce cart total or how to get the Woocommerce cart object.

How can I get WooCommerce Cart Total

you can get access to current user cart details by just using this object class WC(), here is the below example to get cart details

$getcart = WC()->cart;

Now by this object, you can get every cart details you want.
if you want to get the WooCommerce cart total, follow the example below

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

If you want to list all cart items you can copy the code below and modify as your need:

// Loop over $cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
   $itemproduct = $cart_item['data'];
   $itemproduct_id = $cart_item['product_id'];
   $itemquantity = $cart_item['quantity'];
   $itemprice = WC()->cart->get_product_price( $product );
   $itemsubtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] );
   $itemlink = $product->get_permalink( $cart_item );
   // Anything related to $product, check $product tutorial
   $itemattributes = $product->get_attributes();
   $itemwhatever_attribute_tax = $product->get_attribute( 'pa_whatever' );
   $itemany_attribute = $cart_item['variation']['attribute_whatever'];
   $itemwhatever_attribute = $product->get_attribute( 'whatever' );
   $itemmeta = wc_get_formatted_cart_item_data( $cart_item );
}

if you want to get billing details of the customer in cart object, for the pre-fill checkout form, you can access this just like this below

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

if your customer shipping address is different from the billing address you can just access this from the cart object:

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

if you want to get the cart to cross-sell, you can just access it like this:

WC()->cart->get_cross_sells();

I wrote more development tips about WooCommerce, you can check all in our WooCommerce Category. if you still face any issues please let us know in comment box, Please follow The Daily WP on Facebook and on Youtube