1

I'm setting up a custom controller to extend Mage_Core_Controller_Front_Action. If a user adds an item to the cart, I want to check the quote lifetime. If it is old enough, then the user should be given a new quote. Nothing needs to be done for younger quotes. This added functionality will help us work around an abandoned cart issue that the client is facing. We aren't worried about any orphaned quotes, we just want the old quote duplicated into a new one, giving us a brand new quoteId.

In the CartController.php, I can get the current quoteId and the items inside with:

$current_cart = Mage::getSingleton('checkout/session');
$quote_id= $current_cart->getQuoteId();

$old_items = $this->_getQuote()->getAllVisibleItems();

But I don't know how to check for the quote lifetime (i.e. current time minus the last quote update), nor do I now how to initialize a brand new quote. I know all of this is stored in the sales_flat_quote and sales_flat_quote_item tables, but I hoped Magento was capable of doing this without mucking about with dangerous SQL statements.

Any ideas?

0dyss3us
  • 91
  • 1
  • 6

2 Answers2

1

Magento is set up to clean old quotes via cron job. Default quote lifetime is 30 days, you can change that from admin in System > Configuration > Sales > Checkout.

Cron job is defined in app/code/core/Mage/Sales/etc/config.xml

<sales_clean_quotes>
    <schedule>
        <cron_expr>0 0 * * *</cron_expr>
    </schedule>
    <run>
        <model>sales/observer::cleanExpiredQuotes</model>
    </run>
</sales_clean_quotes>

Also if you look at cleanExpiredQuotes method inside Mage_Sales_Model_Observer you will notice that only inactive quotes are deleted. Quote is marked as inactive once it's converted to order.

$quotes->addFieldToFilter('is_active', 0);

If you remove that, Magento will delete all expired quotes. Easiest way to do it would be to create your own observer and redefine <model>sales/observer::cleanExpiredQuotes</model> in your config.xml

I'm unsure if this is the answer you are looking for but I hope it will at least be of some use.

Lord Skeletor
  • 531
  • 3
  • 10
1

You could extend Mage_Checkout_Model_Session::getQuote() with something like

public function getQuote()
{
    $quote = parent::getQuote();
    // If older than 24 hours...
    if (strtotime($quote->getUpdatedAt()) < Mage::getSingleton('core/date')->gmtTimestamp() - 60 * 60 * 24) {
        // Kill this quote
        $this->setQuoteId(null);
        // Go get a new one
        return parent::getQuote();
    }
    return $quote;
}
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124