4

I need a Magento code query to set all quotes to Not active. How do I write this query as a Magento code?

UPDATE `sales_flat_quote` SET `is_active`=0 WHERE `customer_email`='user@gmail.com' 

2 Answers 2

5

The usual Magento way would be to load the collection first, then save the attribute for each item. However, this means there are at least n+1 queries for n quotes (1x SELECT and nx UPDATE)

For your case, that should be okay, since there won't be huge numbers of quotes for one email address but if you really need to optimize this into one single update query, you can use this code to create the query manually:

$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$resource = Mage::getResourceModel('sales/quote');
$connection->update(
    $resource->getMainTable(),
    array('is_active' => 0),
    array('customer_email = ?' => 'user@gmail.com' ));
1
  • Thank you fschmengler for the solution and the explanation. Works like magic!
    – Elad
    Feb 24, 2015 at 13:23
2

First,get Quote collection By using below code:

$collection = Mage::getResourceModel('sales/quote_collection');

then filter this collection by Customer email address and active.

$collection
->addAttributeToSelect('entity_id')
->addAttributeToFilter('is_active', 1)
->addAttributeToFilter('customer_email', 'user@gmail.com');

Then update all quotes

foreach($collection as $quote) {
    $quote->setIsActive(false);
    $quote->_getResource()->saveAttribute($quote, 'is_active');
}
3
  • elad ,please check... i have modify the code
    – Amit Bera
    Feb 24, 2015 at 13:49
  • Thank you 7ochem for noticing the mistake. Thank you Amir for the code. Now the code is doing what it suppose to do!
    – Elad
    Feb 24, 2015 at 14:54
  • That will be really slow for big batches
    – Erdal G.
    Nov 22, 2016 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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