Skip to content Skip to sidebar Skip to footer

Loop Through The Cart Items And Return It's Own Id

I want to loop thought all the items which added into the shopping cart and return it's own ID. UPDATE1 : I have updated method like this public function formatPrice($price)

Solution 1:

In a function you can only return ONE value. You should concatenate the results for each iteration and then return

$productId= "";
foreach($cart->getAllItems() as$item) {
 $productId.= $item->getProduct()->getId();
 $productPrice = $item->getProduct()->getFinalPrice();
  }
return'ID: '.$productId;

or use an array

$productId =array();
foreach($cart->getAllItems() as$item) {
     $productId['id']= $item->getProduct()->getId();  
     $productId['price'] = $item->getProduct()->getFinalPrice();   
      }
$productId =array_filter($productId);
//remove empty array for($index=0; $index<count($productId); $index++){ 
 echo$productId[$index]['id'];
 echo$productId[$index]['price'];
}

Post a Comment for "Loop Through The Cart Items And Return It's Own Id"