Magento Cron: Persistent Clear Expired Magento Persistent Shopping Cart saves unsold shopping cart content and provides to the 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 products 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 customers then table contains many records. A un-updated record should be deleted after the limited time(lifetime). 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:

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:


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:
    Field Type Null Key Default
    persistent_id int(10) No Primary None
    key Varchar(50) No None
    customer_id int(10) Yes Null
    website_id smallint(5) No 0
    info text Yes Null
    updated_id timestamp Yes Null

    ✽ Example:

    persistent_id key customer_id website_id info updated_at
    3 S4gHRbM7m6fsaLA8QY2pyVjvQl0y1p9XIyh1SQU27e93TlXk5H 6 1 {“entity_id”:”6″,”entity_type_id”:”1″,”attribute_s… 10/06/14 11:09