dpq39825 2015-04-01 04:44
浏览 13

循环浏览购物车项目并返回自己的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)
    {
        $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? enter image description here

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. enter image description here

  • 写回答

1条回答 默认 最新

  • dpxyfa4718 2015-04-01 05:08
    关注

    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'];
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?