Where to start with Magento Shopping Cart Abandonment?
Asked Answered
I

2

8

I am attempting some type of shopping cart abandonment system with Magento using it's built-in cron module. What I basically need is a system that checks for abandoned shopping carts every 15 mins and sends select cart data to another web service if certain criteria is met with each cart.

Basically here is my process (but feel free to suggest a better way):

the process

  1. Get list of abandoned carts
  2. For each abandoned cart...
    • Add 15 (mins) to that cart's abandoned_duration field in database
    • Check if the abandoned_duration is at 45 or 1440 (1 day) or 4320 (3 days)
      • If yes,
        • send cart information to another web service
        • If abandoned_duration is at 4320 (3 days),
          • Delete abandoned cart
      • Else,
        • continue
  3. Repeat every 15 mins using Magento cron

the questions

  1. Is this possible in Magento?
  2. Is there a better process to do this using Magento?
  3. What are the steps needed to go about implementing this? For example...
    • Which core modules are necessary?
    • Which controllers need to be extended?
    • Should I create my own module for this?
    • What is the best way to get abandoned shopping carts as an array?

The reason I am reaching out to the community is because the Magento documentation and tutorials are very vague. I am new to the Magento MVC however I am not new to PHP, OOP, and MVCs.

Any guidance here would be stellar. Cheers.

Italic answered 10/2, 2012 at 6:42 Comment(0)
A
22

All this can be done using Magento, but as you said, this is a very broad question. I'll give answers to the specific topics, but I suggest you take time to study the fundamentals of Magento module development. Here is an excellent tutorial by Alan Storm (read the whole series).

Get list of abandoned carts

In Magento, the cart is simply a wrapper for the sales/quote object, so that is the entity you will be working with.
Instead of adding increments to a abandoned_duration attribute, I suggest simply checking against the updated_at field.

$adapter = Mage::getSingleton('core/resource')->getConnection('sales_read');
$minutes = 15;
$from = $adapter->getDateSubSql(
    $adapter->quote(now()), 
    $minutes, 
    Varien_Db_Adapter_Interface::INTERVAL_MINUTE
);
$quotes = Mage::getResourceModel('sales/quote_collection')
    ->addFieldToFilter('converted_at', $adapter->getSuggestedZeroDate())
    ->addFieldToFilter('updated_at', array('to' => $from));

This will give you a collection (think of it as an array with methods) of all quotes that haven't been updated for 15 minutes. You can iterate over them like an array

foreach ($quotes as $quote) {
    /* @var $quote Mage_Sales_Model_Quote */
}

Magento Cronjobs

In Magento, all cron jobs are listed in the configuration structure. So first you need to add it to the config.xml of your module (refer to the linked tutorial for more information on Magento configuration).
This XML registers a cronjob with Magento.

<crontab>
    <jobs>
        <process_abandoned_carts>
            <schedule>
                <cron_expr>*/15 * * * *</cron_expr>
            </schedule>
            <run>
                <model>your_module/observer::processAbandonedCarts</model>
            </run>
        </process_abandoned_carts>
    </jobs>
</crontab>

Now, whenever Magento runs the cronjob, it will instantiate the class your_module/observer and call the processAbandonedCarts() method.
In order for Magento to process configured cronjobs, you need to set the system up to do so. This consists of two parts: the system configuration and the triggering of the cron jobs.

The system configuration is done in the administrative interface under System > Configuration > System > Cron (Scheduled Tasks)

The triggering consists of setting up a way to periodically execute the cron.php (or cron.sh) script in the Magento root directory. This can be done using a regular crontab entry (man 5 crontab on any decent unix system for more information). Another option preferred by many is to execute the cron.php script through a curl or wget call so the processing user ID matches the regular Magento user ID (i.e. the Apache user). This might be important if APC caching is configured, but this is getting off topic.
However, on topic is that you need to call it often enough so it matches the settings you specify in the administrative interface!
I generally recommend executing the cron.php script every 5 minutes. If there is nothing scheduled it will simply exit.

One really useful extension for Magento when working with cronjobs is Aoe_Scheduler. It adds much of the user interfaces for cronjobs and interactive functionality that should be part of the core system.

Other

Sending the data to a web service is not Magento specific, but rather regular PHP, so I'll wont go into more detail.

To delete a cart, simple call $quote->delete() on the loaded sales/quote instance.

Please ask mor specific questions for further information.

Adinaadine answered 10/2, 2012 at 8:30 Comment(9)
in addition don't forget to compare this time to your quote lifetime valueEricerica
@Adinaadine thank you for time and expertise. This was exactly what I was looking for more or less. My apologies for the ambiguity, but it is tough to ask specific questions as a new Magento developer. For now I am more interested in how to "think with magento" vs how to "program with magento". I am sure I will have more specific questions as I implement so stay tuned.Italic
@Italic glad I could help, it was a good question, and good luck getting deep into Magento!Adinaadine
@Adinaadine thanks again for you help. Unfortunately, a bit of your code resulted in a fatal error for which I can't seem to find a solution. '->addFieldToFilter('converted_at', $adapter->getSuggestedZeroDate())' results in the following: Fatal error: Call to undefined method Varien_Db_Adapter_Pdo_Mysql::getSuggestedZeroDate() Any suggestions?Italic
Are you using Magento 1.6 or newer? If not, simply use '0000-00-00 00:00:00' in place of that method call.Adinaadine
It appears I am using Magento ver. 1.11.0.0, but your other solution works. Thank you @Adinaadine !Italic
Hmm. @Adinaadine wouldn't I also want to filter on the is_active field? Will that retrieve carts that have not been converted to an order? Also I am not sure what the converted_at field returns and why I would want to filter on that. Finally, even though I want to check for abandoned carts every 15 mins, I only care about carts abandoned starting at 45 minutes or longer. With that said, shouldn't I change the $minutes variable to 45?Italic
Checking against is_active is a good idea. The converted_at field doesn't seem to be used by the core, but I've seen some extensions use it, so I suggest leaving the check against it in place, even if you don't have to. Change the $minutes to whatever you need, i.e. 45 is good if you want to check quotes that haven't been updated in 45 minutes.Adinaadine
checking against is_active worked for me. plus I also added a filter $quote->hasItems() coz many of my quotes were active with no items in them.Cameo
L
0

here is mine to get all active abandoned cart quotes

$quotes = Mage::getResourceModel('sales/quote_collection')
        ->addFieldToFilter('converted_at', array('null' => true))
        ->addFieldToFilter('customer_email', array('notnull' => true))
        ->addFieldToFilter('items_count', array('gteq' => 1))
        ->addFieldToFilter('customer_is_guest', array('eq' => 0))
        ->addFieldToFilter('customer_id', array('in' => $this->getCustomerIds()))

        ->addFieldToFilter('is_active', 1)

        //->addFieldToFilter('updated_at', array('to' => $from));
        ;
Leonhard answered 23/3, 2015 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.