loop through the cart items and return it's ow

2019-09-04 18:08发布

问题:

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)
    {
        $productId=""; // an iterate here
        $cart = Mage::getModel('checkout/cart')->getQuote();

            foreach ($cart->getAllItems() as $item) {
                 $productId = $item->getProduct()->getId();
                 $productPrice = $item->getProduct()->getFinalPrice();
            }


        return 'ID: '.$productId;


    }

Now it returns everything in one row therefore i have my result like this, should i use "," to splite them?

P.S: File i editing is Data.php under \app\code\core\Mage\Checkout\Helper

I assume the first product's ID is 471186 and the second should be 463089, do i need another foreach loop to do that?

UPDATE2 : Even i split it, it will just display like 471186, 463089 but i want it display according to there current product, i assuem i need something else, does the magento library supply some method like that?

UPDATE3: I have seen your latest method, which store variable in array and return it. After some modify depends on your code i have:

 $productId =array();
 $cart = Mage::getModel('checkout/cart')->getQuote();
    foreach($cart->getAllItems() as $item) {
 $productId[]= $item->getProduct()->getId();     
  }

    $productId =array_filter($productId);
        //remove empty array 
        foreach($productId as $id){
        return $id; //return $productId;
        }

If i use return $productId , it give me "Array" the data type as result which is no use and I tried print out the $id it gives the first product ID only as before. I will use print_r under this situation but it seem not allowing me to do that.

UPDATE4: I tried the internal for loop and i assume it's going to loop and display the price until it's value smaller than $index which is 0 means null.

So i re-factor my codes like this:

    $productId =array();
    $cart = Mage::getModel('checkout/cart')->getQuote();

foreach($cart->getAllItems() as $item) {
     $productId['id']= $item->getProduct()->getId();  
     $productId['price'] = $item->getProduct()->getFinalPrice();   
      }

        $productId =array_filter($productId);
        for($index=0; $index<count($productId); $index++){ 
  return $productId[$index]['price']; //cannot use echo, printf and print_r
            }

But it return's null only, nothing display on the shopping cart.

回答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'];
}