Tárraga Posted May 4, 2015 Share Posted May 4, 2015 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 More sharing options...
Rolige Posted May 4, 2015 Share Posted May 4, 2015 (edited) Inside the shopping-cart.tpl you can get the ID of each product.. {foreach $products as $product} ... {$product.id_product} ... {/foreach} Edited May 4, 2015 by COTOKO (see edit history) 1 Link to comment Share on other sites More sharing options...
Tárraga Posted May 4, 2015 Author Share Posted May 4, 2015 Well I need it in Cart.php because I'm trying to do a longer function Link to comment Share on other sites More sharing options...
cristic Posted May 4, 2015 Share Posted May 4, 2015 (edited) 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 May 4, 2015 by cristic (see edit history) 4 Link to comment Share on other sites More sharing options...
Tárraga Posted May 4, 2015 Author Share Posted May 4, 2015 Thanks, that helped me a lot! Link to comment Share on other sites More sharing options...
Gouravdev Posted January 25, 2022 Share Posted January 25, 2022 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 More sharing options...
Ress Posted March 5, 2022 Share Posted March 5, 2022 You can check if the cart contains products: $cart = Context::getContext()->cart; if ($cart->hasProducts()) { $products = $cart->getProducts(); } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now