2

I am trying to get the collection based on product and quote Id

    $quotecollection = Mage::getModel('sales/quote_item')->getCollection()->addFieldToFilter('quote_id',$quoteId)->addFieldToFilter('product_id',$product_id);

   $quotecollectionaArr =  $quotecollection->getData();
    echo $quotecollection->getQty();

Gave me the following error

Fatal error: Call to undefined method Mage_Sales_Model_Resource_Quote_Item_Collection::getQty() in C:\xampp\htdocs.... Please help.

7
  • have you got array inside $quotecollectionaArr? Feb 21 '17 at 13:34
  • ye i am able to print it @RakeshJesadiya
    – Melvin
    Feb 21 '17 at 13:39
  • try with $quotecollectionaArr['qty'] and check Feb 21 '17 at 13:41
  • this give blank
    – Melvin
    Feb 21 '17 at 13:47
  • 1
    @Melvin check my answer I think it solve your concern Feb 21 '17 at 13:56
1

try below code

$quotecollection = Mage::getModel('sales/quote_item')->getCollection()->addFieldToFilter('quote_id',$quoteId)->addFieldToFilter('product_id',$product_id);

//$quotecollectionaArr =  $quotecollection->getData();
foreach($quotecollection as $quote){
   echo $quote->getQty()
}

2ndWay

$quotecollection = Mage::getModel('sales/quote_item')->getCollection()->addFieldToFilter('quote_id',$quoteId)->addFieldToFilter('product_id',$product_id)->getFirstItem();
  //getFirstItem will do the trick  
  echo $quotecollection->getQty(); 
3
  • there is no other way without using forech ?
    – Melvin
    Feb 21 '17 at 14:00
  • 1
    yes there is a way give me 5 minute I will update Feb 21 '17 at 14:02
  • 1
    @Melvin please check updated code Feb 21 '17 at 14:07
0

First, your $quotecollection contains a collection of quote items. So calling getData on it will not let you retrieve the qty for each item.

To do so, I suggest you do the following:

$qtysFromCollection = $quotecollection->getColumnValues('qty');

You will get an array that will contain the quantity for each quote item in your collection.

This getColumnValues will do the loop and generate the array for you, you can find its code under the Varien_Data_Collection class:

/**
 * Retrieve field values from all items
 *
 * @param   string $colName
 * @return  array
 */
public function getColumnValues($colName)
{
    $this->load();

    $col = array();
    foreach ($this->getItems() as $item) {
        $col[] = $item->getData($colName);
    }
    return $col;
}
2
  • Fatal error: Call to a member function getColumnValues() on a non-object
    – Melvin
    Feb 21 '17 at 14:01
  • @Melvin hmmmm I had a typo see my updated answer Feb 21 '17 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.