Magento运输模块:获取当前购物车中的所有商品

时间:2012-02-21 07:08:19

标签: magento module product shipping

我正在为Magento开发一个运输模块,但却陷入了如何获取当前在该会话中的购物车中的物品。

我在互联网上关注一些教程,他们使用:

if ($request->getAllItems()) { 
    foreach ($request->getAllItems() as $item) {
      //do something here.....

    }
}

我的问题是我不确切知道$ item变量上的信息/数据是什么?

我想获得当前购物车上的产品的重量和价格来计算运费。我尝试使用 Mage :: log 打印$ item值,或者使用print_rvar_dump将其打印到屏幕上但不成功。日志为空,变量不会在屏幕上打印。

有人可以告诉我如何获取$item attributes/method或者是否有其他方式来获取当前购物车中的产品信息?

非常感谢任何帮助:)

3 个答案:

答案 0 :(得分:2)

您可以使用Mage::getSingleton('checkout/session')->getQuote();

中提供的三种方法之一来实现此目的
  • getItemsCollection() - 正在审核sales/quote_items集合
  • getAllItems() - 检索所有项目
  • getAllVisibleItems() - 检索未删除但有parent_item_id != null
  • 的项目

答案 1 :(得分:1)

我正在寻找这个解决方案,并在下面找到。但未经测试。

$CartSession = Mage::getSingleton('checkout/session');
foreach($CartSession->getQuote()->getAllItems() as $item)
{
  $productWeight = $item->getWeight();
  $productExPrice  = $item->getPrice(); // price excluding tax
  $productIncPrice = $item->getPriceInclTax(); // price excluding tax
}

答案 2 :(得分:0)

如果您想了解有关$ item的信息,可以执行以下操作:

print_r($item->getData());

如果你想获得物品重量,请按下:

$item->getWeight();
相关问题