Skip to content

Instantly share code, notes, and snippets.

@mystix
Last active July 6, 2021 05:23
Show Gist options
  • Save mystix/2568084 to your computer and use it in GitHub Desktop.
Save mystix/2568084 to your computer and use it in GitHub Desktop.
Delete abandoned Magento shopping carts -- see http://www.magentocommerce.com/boards/viewthread/53981/P15/
-- deactivate all abandoned shopping carts belonging to noone
UPDATE sales_flat_quote
SET is_active = 0
AND updated_at < '2012-12-05';
-- remove all deactivated shopping carts
DELETE FROM sales_flat_quote WHERE is_active = 0;
<?php
/*
To make the cleaning expired carts automatic, override cleanExpiredQuotes with the following
version which will also take care of the old active carts: app/code/core/Mage/Sales/Model/Observer.php
*/
public function cleanExpiredQuotes($schedule)
{
// extending limit
ini_set( "memory_limit", "2000M" );
$lifetimes = Mage::getConfig()->getStoresConfigByPath('checkout/cart/delete_quote_after');
// cleaning expired quotes that have been converted to orders
foreach ($lifetimes as $storeId=>$lifetime) {
$lifetime *= 86400;
$quotes = Mage::getModel('sales/quote')->getCollection();
/* @var $quotes Mage_Sales_Model_Mysql4_Quote_Collection */
$quotes->addFieldToFilter('store_id', $storeId);
$quotes->addFieldToFilter('updated_at', array('to'=>date("Y-m-d", time()-$lifetime)));
$quotes->addFieldToFilter('is_active', 0);
$quotes->walk('delete');
}
// cleaning expired quotes that have not been converted to orders
// change by [email protected]
foreach ($lifetimes as $storeId=>$lifetime) {
$lifetime *= 86400;
// let's be safe and allow double of the expiration period for those quotes that have not been
// converted to orders
$lifetime *= 2;
$quotes = Mage::getModel('sales/quote')->getCollection();
/* @var $quotes Mage_Sales_Model_Mysql4_Quote_Collection */
$quotes->addFieldToFilter('store_id', $storeId);
$quotes->addFieldToFilter('updated_at', array('to'=>date("Y-m-d", time()-$lifetime)));
// not converted
$quotes->addFieldToFilter('is_active', 1);
$quotes->walk('delete');
}
return $this;
}
@ASGcbrugh
Copy link

what would the sql command look like if you only wanted to target specific site id and set any of those to 0?
for example, if i wanted to set 0 for only sites with id: 3 & 4 from 2016-02-01

@alecat88
Copy link

Hi @sybarite @mystix , is this still working for magento 1?
@sybarite what do you mean by "change to check for update_at"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment