Question

The goal is to get all the quotes for a given customer (considering id, email, whatever).

But this quotes collection seems to work differently from those of customers and products.

First attempt: getCollection

$quote_collection = Mage::getModel('sales/quote')->getCollection()
  ->addFieldToFilter('customer_id', $customer->getId())
  //or //addFieldToFilter('customer_email', $customer->getEmail())

Gives a frustrating (even by filtering on created_at)

Allowed memory size of 268435456 bytes exhausted ...

To avoid messing around with server's memory, tried appending

  ->limit(10)

Which gives the error

Call to undefined method Mage_Sales_Model_Resource_Quote_Collection::limit()

Deserately, try to limit to only one result, using getFirstItem: it works perfectly and the code is now

$quote_collection = Mage::getModel('sales/quote')->getCollection()
  ->addFieldToFilter('customer_id', $customer->getId())
  ->getFirstItem(); //->limit(10);

Too well in fact, since I do not want only one quote but all of them.

Second attempt: getSelect

$quote_collection = Mage::getModel('sales/quote')->getCollection()
  ->getSelect()
  ->where('(customer_id='.$customer->getId().')');
Mage::log($quote_collection->__toString());

The sql is exatly what I want:

SELECT `main_table`.*
FROM `sales_flat_quote` AS `main_table`
WHERE ((customer_id=136))

Which gives 8 lines on the Magento sample data, for customer 136.

But I do not know a way to get the results from this query (not even that), since

$quote_collection = Mage::getModel('sales/quote')->getCollection()
  ->getSelect()
  ->where('(customer_id='.$customer->getId().')')
  ->getItems();

Results in a error report telling:

Unrecognized method 'getItems()'

Any suggestion? TIA!

EDIT: Finally, the first part was nearlyy right, my memory issues were caused by some var_dump :)

The working code is

$quote_collection = Mage::getModel('sales/quote')->getCollection()
  ->addFieldToFilter('customer_id', $customer->getId());
foreach($quote_collection as $key => $quote)
{
  $quote_data=$quote->getData();
  Mage::log('quote '.$quote_data['entity_id'].')
  Mage::log('  created at '.$quote_data['created_at']);
  if($quote_data['is_active']) {
    Mage::log('  is active');
  }
  Mage::log('  $'.$quote_data['grand_total']);
}
Was it helpful?

Solution

    $quote_collection = Mage::getResourceModel('sales/quote_collection')
        ->addFieldToSelect('entity_id')
        ->addFieldToSelect('subtotal')
        ->addFieldToFilter('customer_id', 5);
    foreach ($quote_collection as $quote) {
        $data = $quote->getData();
        print_r($data);
    }   

Or if you know which field you need, for example Subtotal:

$subtotal = $quote->getSubtotal();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top