I am trying to send the woocommerce cart items to third party shipping tool. I need the item name, quantity and individual price to be sent to the third party. How can this be achieved?
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo $_product->post_title;
}
How do I get item name and quantity and price?
Since WooCommerce 2.1 (2014) you should use the WC function instead of the global. You can also call more appropriate functions: foreach ( WC()->cart->get_cart() as $cart_item ) { $item_name = $cart_item['data']->get_title(); $quantity = $cart_item['quantity']; $price = $cart_item['data']->get_price(); ...
In WooCommerce, product details are stored in product objects that hold product data. If you want to get the name of a WooCommerce product then you will first need to create a product object then call the get_name() function on that object to retrieve the product name.
Cart URLs are easily created from the WooCommerce Settings page. Simply enable Cart URLs, add a new Cart URL with the products you've previously determined. Save and share the cart link. The option to give each Cart URL a unique name and pretty permalink, (visible to the customer) is available.
Try this :
<?php global $woocommerce; $items = $woocommerce->cart->get_cart(); foreach($items as $item => $values) { $_product = wc_get_product( $values['data']->get_id()); echo "<b>".$_product->get_title().'</b> <br> Quantity: '.$values['quantity'].'<br>'; $price = get_post_meta($values['product_id'] , '_price', true); echo " Price: ".$price."<br>"; } ?>
To get Product Image and Regular & Sale Price:
<?php global $woocommerce; $items = $woocommerce->cart->get_cart(); foreach($items as $item => $values) { $_product = wc_get_product( $values['data']->get_id() ); //product image $getProductDetail = wc_get_product( $values['product_id'] ); echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr ) echo "<b>".$_product->get_title() .'</b> <br> Quantity: '.$values['quantity'].'<br>'; $price = get_post_meta($values['product_id'] , '_price', true); echo " Price: ".$price."<br>"; /*Regular Price and Sale Price*/ echo "Regular Price: ".get_post_meta($values['product_id'] , '_regular_price', true)."<br>"; echo "Sale Price: ".get_post_meta($values['product_id'] , '_sale_price', true)."<br>"; } ?>
Since WooCommerce 2.1 (2014) you should use the WC function instead of the global. You can also call more appropriate functions:
foreach ( WC()->cart->get_cart() as $cart_item ) { $item_name = $cart_item['data']->get_title(); $quantity = $cart_item['quantity']; $price = $cart_item['data']->get_price(); ...
This will not only be clean code, but it will be better than accessing the post_meta directly because it will apply filters if necessary.
Note on product price
The price of the cart item may be different from that of the product (stored in the database as post meta).
Some plugins or custom functions (added to the functions.php of the active theme) can change the price of the cart item.
If you want to be sure you get the price of the product added to the cart you will have to get it like this:
foreach ( WC()->cart->get_cart() as $cart_item ) {
// gets the cart item quantity
$quantity = $cart_item['quantity'];
// gets the cart item subtotal
$line_subtotal = $cart_item['line_subtotal'];
$line_subtotal_tax = $cart_item['line_subtotal_tax'];
// gets the cart item total
$line_total = $cart_item['line_total'];
$line_tax = $cart_item['line_tax'];
// unit price of the product
$item_price = $line_subtotal / $quantity;
$item_tax = $line_subtotal_tax / $quantity;
}
Instead of:
foreach ( WC()->cart->get_cart() as $cart_item ) {
// gets the product object
$product = $cart_item['data'];
// gets the product prices
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$price = $product->get_price();
}
Other data you can get:
foreach ( WC()->cart->get_cart() as $cart_item ) {
// get the data of the cart item
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
// gets the cart item quantity
$quantity = $cart_item['quantity'];
// gets the cart item subtotal
$line_subtotal = $cart_item['line_subtotal'];
$line_subtotal_tax = $cart_item['line_subtotal_tax'];
// gets the cart item total
$line_total = $cart_item['line_total'];
$line_tax = $cart_item['line_tax'];
// unit price of the product
$item_price = $line_subtotal / $quantity;
$item_tax = $line_subtotal_tax / $quantity;
// gets the product object
$product = $cart_item['data'];
// get the data of the product
$sku = $product->get_sku();
$name = $product->get_name();
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$price = $product->get_price();
$stock_qty = $product->get_stock_quantity();
// attributes
$attributes = $product->get_attributes();
$attribute = $product->get_attribute( 'pa_attribute-name' ); // // specific attribute eg. "pa_color"
// custom meta
$custom_meta = $product->get_meta( '_custom_meta_key', true );
// product categories
$categories = wc_get_product_category_list( $product->get_id() ); // returns a string with all product categories separated by a comma
}
This will show only Cart Items Count.
global $woocommerce;
echo $woocommerce->cart->cart_contents_count;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With