Magento: Get the quantity in cart for a given product

前端 未结 4 641
鱼传尺愫
鱼传尺愫 2021-01-31 06:31

I use this code :

// $items = Mage::getModel(\'checkout/cart\')->getQuote()->getAllItems();
$items = Mage::getSingleton(\'checkout/session\')->getQuote(         


        
相关标签:
4条回答
  • 2021-01-31 06:55

    you could try:

    $quote = Mage::getSingleton('checkout/session')->getQuote();
    $item = Mage::getModel('sales/quote_item')->getCollection()
                    ->addFieldToFilter('quote_id', $quote->getId())
                    ->addFieldToFilter('product_id', $productId)
                    ->getFirstItem();
    //to get the quantity: $item->getQty();
    
    0 讨论(0)
  • 2021-01-31 06:57

    You should use the

    setQUote
    

    method when calling the collection. Should looks like this

    $quote = Mage::getModel('sales/quote')->load($quote_id);
    $salesQuoteItem = Mage::getModel('sales/quote_item')->getCollection()
                ->setQuote($quote)
                ->addFieldToFilter('quote_id', $quote_id)
                ->addFieldToFilter('product_id', $data['product_id'])
                ->getFirstItem();
    
    0 讨论(0)
  • 2021-01-31 06:58

    Try this:

    // if you don't have the product object already...
    $_product = Mage::getModel('catalog/product')->load($productid);
    
    $_quote = Mage::getSingleton('checkout/session')->getQuote();
    $_item = $_quote ? $_quote->getItemByProduct($_product) : false;
    $qty = $_item ? $_item->getQty() : 0;
    
    0 讨论(0)
  • 2021-01-31 07:10

    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.

    0 讨论(0)
提交回复
热议问题