73

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?

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
Philomath
  • 1,145
  • 4
  • 18
  • 28

6 Answers6

144

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>";
        }
?>
Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32
  • 1
    hey @Rohil_PHPBeginner, how can i get all of the attributes of the product in the cart. With your script i got title, quantity and price but i want image and actual price also. Thanks in advance – Manik Arora Nov 14 '15 at 12:58
  • hello and what if i want to send it within my contact form 7 how can i do so? – Meily Chhon Jun 22 '16 at 14:55
  • How i can get product total price per quantity? Am using get_item_total to get the unit price. So is there a way of getting total price @Rohil_PHPBeginner? Thanks – Ana DEV Aug 11 '16 at 07:11
  • 1
    How do you get the products variations data? – Amjad Feb 08 '17 at 00:41
  • 1
    In WooCommerce 3 you get a notice if you access these properties directly. @JesúsCarrera suggested using `get_title()` in his answer below and that is a better alternative. – Tuure May 28 '17 at 19:32
  • get_cart() causes problems if it is executed in the admin area so wrap everything in conditional:if( ! is_admin() ) { } – AJD Apr 29 '19 at 17:30
  • To get the price of the cart item (and not the product price): https://stackoverflow.com/questions/28576667/get-cart-item-name-quantity-all-details-woocommerce/28576861#66080003 – Vincenzo Di Gaetano Feb 26 '21 at 18:34
56

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.

Jesús Carrera
  • 11,275
  • 4
  • 63
  • 55
  • 5
    This is the better solution as it follows the 'WC way' of doing things and doesn't access the post_meta directly. – Larzan May 30 '18 at 11:14
  • 2
    This is also better than the accepted solution since it gets the actual price of the product that the customer is paying which includes any role or quantity based discounts, etc. – rmmoul Aug 26 '20 at 22:15
25

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
}
Vincenzo Di Gaetano
  • 3,892
  • 3
  • 13
  • 32
  • Great answer... does $categories give you the name or slug? Thanks – Si8 Apr 02 '22 at 01:16
  • Thanks. [`wc_get_product_category_list`](https://woocommerce.github.io/code-reference/files/woocommerce-includes-wc-product-functions.html#source-view.1149) is an alias for the Wordpress [`get_the_term_list`](https://developer.wordpress.org/reference/functions/get_the_term_list/) function. So `$categories` will contain a list (anchor elements) with the **names** of the product categories. – Vincenzo Di Gaetano Apr 02 '22 at 07:17
  • 1
    @Si8 If you want to get only slugs or names as strings separated by delimiter, you can use **benklocek's** answer found here: https://developer.wordpress.org/reference/functions/get_the_terms/#div-comment-2587. You can use the `slug` and `name` parameters according to your needs. So: `$terms = get_the_terms( $product->get_id(), 'product_cat' ); $slugs = join( ',', wp_list_pluck( $terms, 'slug' ) );` or `$terms = get_the_terms( $product->get_id(), 'product_cat' ); $names = join( ',', wp_list_pluck( $terms, 'name' ) );`. – Vincenzo Di Gaetano Apr 02 '22 at 07:32
6

This will show only Cart Items Count.

 global $woocommerce; 
    echo $woocommerce->cart->cart_contents_count;
Rashid Wasim
  • 85
  • 1
  • 4
4

you can get the product name like this

foreach ( $cart_object->cart_contents as  $value ) {
        $_product     = apply_filters( 'woocommerce_cart_item_product', $value['data'] );

        if ( ! $_product->is_visible() ) {
                echo $_product->get_title();
        } else {
                echo $_product->get_title();
        }


     }
  • Filters like this is meant to be used in internals or for helper functions. The prefered way to get product should be wc_get_product(). – Navidot Jul 26 '19 at 14:04
2

Most of the time you want to get the IDs of the products in the cart so that you can make some comparison with some other logic - example settings in the backend.

In such a case you can extend the answer from @Rohil_PHPBeginner and return the IDs in an array as follows :

<?php 

    function njengah_get_ids_of_products_in_cart(){
    
            global $woocommerce;
    
            $productsInCart = array(); 
            
            $items = $woocommerce->cart->get_cart(); 
            
            foreach($items as $item => $values) { 
            
                $_product =  wc_get_product( $values['data']->get_id()); 
                
               /* Display Cart Items Content  */ 
               
                echo "<b>".$_product->get_title().'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
                $price = get_post_meta($values['product_id'] , '_price', true);
                echo "  Price: ".$price."<br>";
                
                /**Get IDs and in put them in an  Array**/ 
                
                $productsInCart_Ids[] =  $_product->get_id();
            }
           
           /** To Display **/ 
              
           print_r($productsInCart_Ids);
           
           /**To Return for Comparision with some Other Logic**/ 
           
           return $productsInCart_Ids; 
    
        }
NJENGAH
  • 955
  • 15
  • 12