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'
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityThe 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' ));
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');
}