While setting up WooCommerce, you might need to repeat a few coding operations. So, this task is pretty tedious & time-consuming. Such repetitive tasks are required for $cart $product and $order information. In this tutorial, we will learn about how to Get cart info total items etc from $cart object.
Further, we will address queries like “How to get the cart total?” or “How to get the cart items?”. Also, you will learn about how to get cart fees, applied promo coupons, cart contents total, and total weight, and more.
So, let’s get into the comprehensive tutorial:
You can learn various coding operations through this guide as per various cases.
-
You can access to $cart variable
Firstly, you can use hooks (do_action and apply_filters) which utilizes additional arguments. These hooks are passed on to the function. If you get access to “$cart” object, then your job is done!
Otherwise, you can implement things the other way. Also, you must make sure that you can access the “$cart” object. It is exactly the same as the “WC()->cart” object. You must be able to access this object globally on any WooCommerce frontend section.
In short,
$cart = WC()->cart;
So, it is the one way to get cart info total items etc from $cart object.
2. You cannot access $cart
If you cannot access the $cart object, you can get it globally on your WooCommerce website page. So, you will be able to load the cart object with WC()->cart.
// $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'];
$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
$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();
Also Read, A Guide to Add WooCommerce Order Status Dropdown Filter