遍历购物车物品并返回其自己的ID [英] loop through the cart items and return it's own ID

查看:47
本文介绍了遍历购物车物品并返回其自己的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想回想一下添加到购物车中的所有商品,并返回其自己的ID.

I want to loop thought all the items which added into the shopping cart and return it's own ID.

UPDATE1 :我已经更新了这样的方法

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:文件i的编辑文件是 Data.php ,位于\ app \ code \ core \ Mage \ Checkout \ Helper

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

我假设第一个产品的ID为471186,第二个产品的ID为463089,我是否需要另一个foreach循环来做到这一点?

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 : 即使我拆分它,它也会显示为 471186, 463089但是我希望它根据当前的产品显示,我是否还需要其他东西,magento库是否提供了类似的方法?

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:我见过您的最新方法,该方法将变量存储在数组中并返回它.经过一些修改取决于您的代码,我有:

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;
        }

如果我使用 return $ productId ,它会给我数组"数据类型,这是没有用的结果,我尝试打印出$ id,它只像以前一样提供第一个产品ID.在这种情况下,我将使用print_r,但似乎不允许我这样做.

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:我尝试了内部for循环,并假定它将循环播放并显示价格,直到其值小于$ index的值(即0表示空)为止.

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
            }

但是它只返回null,购物车上没有任何显示.

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

推荐答案

在一个函数中,您只能返回一个值.您应该将每次迭代的结果连接起来,然后返回

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;

或使用数组

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

这篇关于遍历购物车物品并返回其自己的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆