3

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']);
}
3
    $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();
  • Unfortunately, this foreach does not loop the collection. Here is the print_r of the $quote_collection for customer 136: pastebin.com/gG7myfrk – nicolallias Jul 29 '15 at 9:46
  • About your edit of 11h51 (GMT), got exactly what I am describing in "first attempt": PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 58285416 bytes) – nicolallias Jul 29 '15 at 12:00
  • Did you manage to check the size of sales_flat_quote table? 500Mb from sample data seems to be huge. – eetzen Jul 29 '15 at 13:23
  • The table sales_flat_quote has 408 lines, total weighting 192,0 Kio ; I think this is a memory leak but cannot figure it out. – nicolallias Jul 29 '15 at 13:31
  • Many thanks, your first answer was good, my PHP skills were in cause. Now, hoping that table never go too big! :) – nicolallias Jul 29 '15 at 14:07

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.