Magento:在购物车中获取给定产品的数量 [英] Magento: Get the quantity in cart for a given product

查看:92
本文介绍了Magento:在购物车中获取给定产品的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

// $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 />";
}

要获取有关购物车中产品的信息.

如何仅获取给定产品(productId)的数量

非常感谢.

解决方案

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

  • 捆绑商品
    • 捆绑产品报价项目由捆绑产品的一项表示,而每个选项分别由简单商品表示.简单产品项目具有针对它们存储的数量信息.简单物料具有一个父物料ID,该ID引用捆绑产品报价项目,就像可配置产品报价项目的子物料一样.

您会看到处理所有方案都很麻烦.我希望我可以告诉您,通过资源模型更容易做到这一点,但是事实是,在数据库中,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:

  • Configurable items
    • For each option, you'll have the configurable product as an item and the simple product from which the option is derived as an item. If you want to display the total count by the configurable parent, you'll need to tally the total configurable qty in the loop:

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

  • Grouped Items
    • Simple products which are added to the quote as part of a grouped item are separate from simple items which are added individually and other groups to which they may belong. If you want to aggregate these, you can loop through like so:

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

  • Bundle Items
    • Bundle product quote items are represented by one item for the bundle product and separate simple product items for each option. The simple product items have the quantity information stored against them. The simple items have a parent item ID which referes back to the bundle product quote item, just as configurable product quote items' children items do.

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.

这篇关于Magento:在购物车中获取给定产品的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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