Magento Cron: Persistent Clear Expired

Magento Persistent Shopping Cart save unsold shopping cart content, and provide to customer in his next visit. All the information of products stored in cookies and database. This functionality helps to increase the sale of your store. Customer can add multiple product in persistent session from different devices and from different browsers. ‘persistent_session’ table contains customer_id, session data as info, website_id and updated date. If there are multiple customer then table contains many records. Un-updated record should be delete after limited time(life time). This cron helps you to remove over period (expired) records.

Magento Cron Job Name: persistent_clear_expired

  • XML of cron job as in config.xml is:
  • persistent_clear_expired_config

    From this we can make out that this cron job will be executed every day at 12:00 AM(Mid Night)

    Function Code:

          public function clearExpiredCronJob(Mage_Cron_Model_Schedule $schedule)
          {
    	 //collection of Id's from 'core_website' table.
    	 $websiteIds = Mage::getResourceModel('core/website_collection')->getAllIds();
    	   if (!is_array($websiteIds))
               {
                 return $this;
    	   }
    	 foreach ($websiteIds as $websiteId) 
             {
    	   //deleteExpired is a function that perform Delete Over(Expired) Records from 'persistent_session' table.
    	   Mage::getModel('persistent/session')->deleteExpired($websiteId);
    	 }
    	return $this;
           }
  • Configuration details:
  • persistent_clear_expired_lifetime

    This can be edited in file app/code/core/Mage/Persistent/etc/config.xml. The default lifetime is one year(31536000 seconds).

  • Database related comments:-
  • The following database table will get cleaned after this cron job over.

    ‘persistent_session’:-

    This table stores customer_id and website_id. If lifetime is over(expired) record is deleted from the table.

  • Sql query to delete:-DELETE FROM `persistent_session` WHERE (website_id = ‘1’) AND (updated_at ‘2013-06-10 12:40:30’)

    Schema of Table:

    FieldTypeNullKeyDefault
    persistent_idint(10)NoPrimaryNone
    keyVarchar(50)NoNone
    customer_idint(10)YesNull
    website_idsmallint(5)No0
    infotextYesNull
    updated_idtimestampYesNull

    Example:

    persistent_idkeycustomer_idwebsite_idinfoupdated_at
    3S4gHRbM7m6fsaLA8QY2pyVjvQl0y1p9XIyh1SQU27e93TlXk5H61{“entity_id”:”6″,”entity_type_id”:”1″,”attribute_s…10/06/14 11:09