更新时间:2023-11-30 10:31:52
tl; dr:该问题的答案因产品类型(可配置,简单等)的复杂性而异. 这可能会推动您设置目录和设置简单的产品可见性.如果您的目录中所有商品都是简单物品,那么此任务将非常简单.另外:请参见最后的编辑.
有很多场景需要考虑.例如,如果您将一个可配置项目添加到购物车,则每个选项的报价将有两个报价项目:一个可配置父产品的报价项目将包含选项数量信息,而一个报价项目将提供该选项的简单产品数据.捆绑商品将具有父捆绑商品以及每个捆绑商品选项的简单商品.
这归结为您需要决定如何向用户表示每种产品类型的数量;例如,您想将一个衬衫的型号与一个订单项代表所有尺寸的数量,还是要针对每种尺寸将它们分开(后者是Magento如何在购物车/评论区域中呈现它们的方式).>
我建议将您的产品添加到购物车,然后在前端使用它来查看报价项目数据:
<?php
include 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app(); // Mage_Core_Model_App
Mage::getSingleton('core/session', array('name'=>'frontend'));
$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
echo count($quote->getItemsCollection());
foreach ($quote->getItemsCollection() as $item){
Zend_Debug::dump($item->debug());
}
我建议使用带有示例数据的安装程序,并将每种产品类型的不同数量添加到购物车中.基于以上代码的输出,您将发现遍历循环时有几种方法和陷阱.令人困惑的情况是当您具有以下条件时:
代码:
$configurables = array();
// ...then, inside your foreach
if($item->getProductType() === 'configurable'){
if(isset($configurables[$item->getProductId()])){
//add to the other options' quantity
$configurables[$item->getProductId()] += $item->getQty();
}
else {
$configurables[$item->getProductId()] = $item->getQty();
}
}
代码:
$grouped = array();
// ...then, inside your foreach
if($item->getProductType() === 'grouped' || $item->getProductType() === 'simple'){
if(isset($grouped[$item->getProductId()])){
//add to the other options' quantity
$grouped[$item->getProductId()] += $item->getQty();
}
else {
$grouped[$item->getProductId()] = $item->getQty();
}
}
您会看到处理所有方案都很麻烦.我希望我可以告诉您,通过资源模型更容易做到这一点,但是事实是,在数据库中,sales_flat_quote_item_option
表将数量信息存储在序列化数据的列中,因此您需要将其放入PHP以与他们取得联系(通过资源模型为您完成).
对不起,这是一个很长的答案,但是您可以看到,根据目录设置,您已经在进行仔细的考虑和测试,以确保您的基础被覆盖.
在尝试更全面的同时,我没有提及您应该查看sales_flat_quote_item
表.根据您的要求,您可以轻松地使用其中的数据来快速,优雅,高效地对满足显示要求所需的数据进行建模.
I use this code :
// $items = Mage::getModel('checkout/cart')->getQuote()->getAllItems();
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {
echo 'ID: '.$item->getProductId().'<br />';
echo 'Name: '.$item->getName().'<br />';
echo 'Sku: '.$item->getSku().'<br />';
echo 'Quantity: '.$item->getQty().'<br />';
echo 'Price: '.$item->getPrice().'<br />';
echo "<br />";
}
To get informations about products in Cart.
How can I get only the quantity for a given product (productId)
Thanks a lot.
tl;dr: The answer to this question varies in complexity based on product type (configurable, simple, etc.). This may drive how you setup your catalog and set simple product visibility. If all that you have in your catalog is simple items, this task is very easy. Also: see edit at end.
There are lots of scenarios to consider. For example, if you add a configurable item to the cart, the quote will have two quote items per option: one quote item for the configurable parent product which will contain option quantity information and one quote item which will provide the option's simple product data. Bundle items will have a parent bundle item as well as a simple item for each bundle option.
What this boils down to is that you need to decide how you represent each product type's quantity to the user; for example, do you want to represent the same model of shirt as one line item with all size quantities, or do you want to separate these out for each size (the latter being how Magento renders them in cart / review areas).
I recommend adding your products to the cart, then using this in the frontend to see the quote item data:
<?php
include 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app(); // Mage_Core_Model_App
Mage::getSingleton('core/session', array('name'=>'frontend'));
$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
echo count($quote->getItemsCollection());
foreach ($quote->getItemsCollection() as $item){
Zend_Debug::dump($item->debug());
}
I recommend using an install with the sample data and adding different quantities of each product type to the cart. Based on this + the output from the above code, you'll see that you have several approaches and gotchas as you loop through. The confounding scenarios are when you have the following:
Code:
$configurables = array();
// ...then, inside your foreach
if($item->getProductType() === 'configurable'){
if(isset($configurables[$item->getProductId()])){
//add to the other options' quantity
$configurables[$item->getProductId()] += $item->getQty();
}
else {
$configurables[$item->getProductId()] = $item->getQty();
}
}
Code:
$grouped = array();
// ...then, inside your foreach
if($item->getProductType() === 'grouped' || $item->getProductType() === 'simple'){
if(isset($grouped[$item->getProductId()])){
//add to the other options' quantity
$grouped[$item->getProductId()] += $item->getQty();
}
else {
$grouped[$item->getProductId()] = $item->getQty();
}
}
You can see that handling all scenarios is cumbersome. I wish I could tell you that it's easier to do this via resource models, but the fact is that in the database the sales_flat_quote_item_option
table stores quantity info in a column of serialized data, so you'll need to pull things into PHP to get anywhere with them (which is done for you via the resource model).
Sorry this is such a long answer, but you can see that, based on your catalog setup, you're in for some careful consideration and testing to make sure you've got your bases covered.
EDIT: While trying to be thorough, I failed to mention that you should look into the sales_flat_quote_item
table. Depending on your requirements, you can easily use the data in there to quickly, gracefully, and efficiently model the data that you need to achieve your display requirements.