Magento中获取购物车项目的两个命令之间的区别 [英] Difference between two commands of fetching Shopping Cart Items in Magento
问题描述
在Magento中,如果您需要获取/获取购物车的商品详细信息,则可以通过两种可能的方式中的任何一种来完成,这将为您提供阵列中所有已购物商品:
In Magento, if you need to get / fetch the Shopping Cart's Item details, you can do it in any of the two possible ways, which will provide you with all the shopped Items in an array:-
-
$ cartItems1 = $ cart-> getQuote()-> getAllItems();
-
$ cartItems2 = $ cart-> getItems()-> getData();
但是在使用以上两种方法中的任何一种之前,您需要将购物车对象初始化为:-
But before using any one of the above two methods, you need to initialize the shopping cart object as:-
$cart = new Mage_Checkout_Model_Cart();
$cart->init();
任何人都可以详细说明这两个选项提供的内容吗?彼此之间的差异以及可能的用法.
Magento中提供了更多此类选项,任何人都可以突出显示它吗?
推荐答案
如果您查看购物车和Quote类的代码,一切将变得清晰.
If you look at the code of the Cart and Quote classes everything will become clear.
这是$ cart-> getItems()的代码:
Here's the code for $cart->getItems():
public function getItems()
{
return $this->getQuote()->getAllVisibleItems();
}
简单明了-它仅调用Quote对象的方法.所以现在的问题是: getAllVisibleItems()和 getAllItems()有什么区别?
Plain and simple - it just calls a method of the Quote object. So the question now is: What is the difference between getAllVisibleItems() and getAllItems()?
让我们看一下这两种方法的代码:
Let's look at the code of both methods:
public function getAllItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted()) {
$items[] = $item;
}
}
return $items;
}
public function getAllVisibleItems()
{
$items = array();
foreach ($this->getItemsCollection() as $item) {
if (!$item->isDeleted() && !$item->getParentItemId()) {
$items[] = $item;
}
}
return $items;
}
唯一的区别: getAllVisibleItems()对每个项目都有一个附加检查:
The only difference: getAllVisibleItems() has an additional check for each item:
!$item->getParentItemId()
测试产品是否有父产品(换句话说,它测试它是否是简单产品).因此,与 getAllItems()相反,此方法的return数组将缺少简单的乘积.
which tests if the product has a parent (in other words, it tests if it is a simple product). So this method's return array will be missing simple products, as opposed to getAllItems().
还有其他检索项目的方法吗?
一种方法是直接从quote对象获取产品集合:
One would be to directly get the product collection from the quote object:
$productCollection = $cart->getQuote()->getItemsCollection();
这篇关于Magento中获取购物车项目的两个命令之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!