Jump to content

How to get all product ids in current cart?


Tárraga
 Share

Recommended Posts

Hello! I'm trying to get in a array all the product ids in current cart.

 

I have created a new function called getProductsId(), I copied the getProducts() function deleting all info that I don't need, but it seems that won't work.

 

I just need a function that gets the products id in a array, could some one help me?

 

Thanks.

Share this post


Link to post
Share on other sites

In Cart.php, you can do the following:

$products = $this->getProducts();
$product_ids = array();
foreach ($products as $product)
  $product_ids[] = (int)$product['id_product']; 

Or, if you want inside another php file:

$products = Context::getContext()->cart->getProducts();
$product_ids = array();
foreach ($products as $product) 
  $product_ids[] = (int)$product['id_product'];
Edited by cristic (see edit history)
  • Like 3

Share this post


Link to post
Share on other sites

  • 6 years later...
On 5/4/2015 at 10:53 PM, cristic said:

In Cart.php, you can do the following:

$products = $this->getProducts();
$product_ids = array();
foreach ($products as $product)
  $product_ids[] = (int)$product['id_product']; 

Or, if you want inside another php file:

$products = Context::getContext()->cart->getProducts();
$product_ids = array();
foreach ($products as $product) 
  $product_ids[] = (int)$product['id_product'];

Hi,
```Context::getContext()->cart->getProducts();```
I'm getting 'Fatal Error` as when I add product into the cart, Context::getContext()->cart is coming as null.
Any idea, what, when wrong with it?
Also, I'm getting ID

Share this post


Link to post
Share on other sites

  • 1 month later...

You can check if the cart contains products:

$cart = Context::getContext()->cart;
if ($cart->hasProducts()) {
	$products = $cart->getProducts();
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More