views:

55

answers:

2

I'm working on a custom module that needs to know if/when a user has aborted the checkout process. "Aborting" simply means they landed on the checkout's indexAction but didn't complete the process. It's absolutely essential that I know if/when this happens.

I was thinking maybe set a session variable that they've entered checkout. On each page load, have a block of code run that checks this variable. If true, it will check which controller is being used. If it isn't the checkout controller, I'll know they've left.

Two problems with my idea:

  1. I don't have the slightest clue where to put this controller-checking code so it runs on each page load.
  2. I don't know how to find which controller is handling the request.

I'd greatly appreciate if you could help answer those questions, or even suggest a better approach!

A: 

I'd take a slightly different approach to the abandoned cart problem.

Every time you "create a shopping cart" by adding a product, you're creating a Mage_Sales_Model_Quote that magento stores in the database. Every time you complete an order, you're creating a Mage_Sales_Model_Order object. Mage_Sales_Model_Order objects keep a reference to their Original quote IDs.

So, you can query for all the orders to get a list of quote IDs, and then create a quote collection and filter quotes for which there are orders. This will give you a list of each time a cart's been abandoned.

//may start to degrade as orders pile up.  Add date filters to 
//prevent or look into properly joining things in.
$orders = Mage::getModel('sales/order')
->getCollection()
->addAttributeToSelect('*');

var_dump('Order Count', count($orders));

$quote_ids  = $orders->getColumnValues('quote_id');
$quote_ids = array_filter($quote_ids, 'is_numeric');

$carts      = Mage::getModel('sales/quote')
->getCollection();

var_dump('All Cart Count:');
var_dump(count($carts));

$carts      = Mage::getModel('sales/quote')
->getCollection()
->addFieldToFilter('entity_id',array('nin'=>$quote_ids));

var_dump('Filtered Cart Count:');
var_dump(count($carts));

foreach($carts as $cart)
{
    var_dump('abandoned cart', $cart);
}

As for your other questions, they're way too involved to get into a single stack overflow question. I'd read up on Magento to get a feel for the basic architecture, and then use the live Commerce Bug demo to figure out which controller you're after. (both projects linked to above are mine, self link, buyer beware, etc.)

Alan Storm
Thanks for the detailed help Alan! Unfortunately I needed to know in real-time (for the current user only) so your solution wasn't practical in this case. I did learn alot from it though, and I really dig your Commerce Bug module! Thanks again :)
Colin O'Dell
A: 

Since I need to know in real-time the instant a visitor left the checkout, I used a session variable named IsUserInCheckout. This is set to true when the checkout's indexAction() executes.

I then created an event observer which ran before the request was dispatched to a controller. It checks which controller is being called. If the request isn't for the checkout controller, I look at the session variable to see if the user had started checkout. Once I run some custom logic, I change the session variable IsUserInCheckout to false.

I feel like its light-weight enough. It does run with every page load, but 98% of the time it justs reads the session variables, sees false, and does nothing.

Colin O'Dell

related questions