Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are unconverted quote records ever removed in Magento?

Tags:

tl;dr: There appears to be no other out-of-box mechanism for deleting quote records which were never converted to orders.

The Mage_Sales module has a scheduled job to clean expired quotes, but this only deletes sales_flat_quote records which are marked as inactive (i.e. is_active = 0). To my knowledge, quotes are only marked as inactive when a quote converts to an order. If this is the case, then the quote table will only grow larger and larger.

Ref Mage_Sales_Model_Observer::cleanExpiredQuotes()

class Mage_Sales_Model_Observer
{
    //...

    public function cleanExpiredQuotes($schedule)
    {
        Mage::dispatchEvent('clear_expired_quotes_before', array('sales_observer' => $this));

        $lifetimes = Mage::getConfig()->getStoresConfigByPath('checkout/cart/delete_quote_after');
        foreach ($lifetimes as $storeId=>$lifetime) {
            $lifetime *= 86400;

            /** @var $quotes Mage_Sales_Model_Mysql4_Quote_Collection */
            $quotes = Mage::getModel('sales/quote')->getCollection();

            $quotes->addFieldToFilter('store_id', $storeId);
            $quotes->addFieldToFilter('updated_at', array('to'=>date("Y-m-d", time()-$lifetime)));
            $quotes->addFieldToFilter('is_active', 0);

            foreach ($this->getExpireQuotesAdditionalFilterFields() as $field => $condition) {
                $quotes->addFieldToFilter($field, $condition);
            }

            $quotes->walk('delete');
        }
        return $this;
    }

    //...
}
like image 999
benmarks Avatar asked Sep 22 '12 15:09

benmarks


People also ask

How do I delete a quote in Magento 2?

Here is how you can flush the quote for a specific customer in Magento2. mysql> DELETE FROM quote WHERE customer_email = '[email protected]'; Remove the WHERE clause to flush the quote for all customers.

What is quote table in Magento2?

The quote table ( sales_flat_quote on M1) contains records on every shopping cart created in your store, whether they were abandoned or converted to a purchase. Each row represents one cart.

What is quote in Magento?

Magento uses a quote to perform tasks such as. Track each item the customer wants to buy, including the quantity and base cost. Gather information about the customer, including billing and shipping addresses.

What is a quote table?

A quote table is used to display data in a table format on one of the quote tabs. This data could be used by the quote template when creating a printed quote, for example.


1 Answers

Overwrite the observer with your own cleanup routine. We keep specific quotes for specific periods of time, hence 4 tiers of deletion. Guest carts get remail and are recoverable, empty registered customer quotes expire beside completed quotes and registered carts with contents get saved for a long period per observed customer behavior.

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Sales
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


/**
 * Sales observer
 *
 * @category   Chief
 * @package    Chief_Sales
 * @author     Magento Core Team <[email protected]>
 */

/* Valid for 1.4.2.0, 1.5.1.0 */

class Chief_Sales_Model_Observer extends Mage_Sales_Model_Observer
{
    /**
     * Clean expired quotes (cron process)
     *
     * @param Mage_Cron_Model_Schedule $schedule
     * @return Mage_Sales_Model_Observer
     */
    public function cleanExpiredQuotes($schedule)
    {
        $lifetimes = Mage::getConfig()->getStoresConfigByPath('checkout/cart/delete_quote_after');

        /* Quotes 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);            // Filled Quotes
            $quotes->walk('delete');
        }


        /* Quotes abandoned by Guest Carts */
        foreach ($lifetimes as $storeId=>$lifetime) {
            $lifetime *= 86400;

            // triple lifetime for abandoned cart remail
            $lifetime *= 3;

            $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', 1);            // Active Quotes
            $quotes->addFieldToFilter('customer_group_id', 0);    // Which are Group NLI (Guest)
            $quotes->walk('delete');
        }


        /* Quotes abandoned by Registered carts no contents */
        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', 1);                      // Active Quotes
            $quotes->addFieldToFilter('customer_group_id', array('gt'=>0)); // For all other groups
            $quotes->addFieldToFilter('items_qty', 0);                      // For empty carts
            $quotes->walk('delete');
        }


        /* Quotes abandoned by Registered carts */
        foreach ($lifetimes as $storeId=>$lifetime) {
            $lifetime *= 86400;

            // Registered cart lifetime for abandoned cart remail 7*25 = 175 days
            $lifetime *= 25;

            $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', 1);                      // Active Quotes
            $quotes->addFieldToFilter('customer_group_id', array('gt'=>0)); // For all other groups
            $quotes->addFieldToFilter('items_qty', array('gt'=>0));         // For expired carts
            $quotes->walk('delete');
        }
        return $this;
    }
}
like image 78
Fiasco Labs Avatar answered Sep 22 '22 21:09

Fiasco Labs