Jump to content

How to get all product ids in current cart?


Tárraga

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.

Link to comment
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 4
Link to comment
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

Link to comment
Share on other sites

  • 1 month later...

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
×
×
  • Create New...