Removing Old Carts with Cronjob

Carts are not deleted after the session is closed. This way it is still possible to restore the cart later when the user gets back online. However, to avoid storing large amount of old carts you can use the cronjob to remove them.

Cronjob

The OldCartRemovalCronJob item type is defined in the ycommercewebservices-items.xml file. There are three attributes defining which carts should be removed:

  • sites: Collection of sites for which old carts will be removed
  • cartRemovalAge: Carts older then a specified number of seconds will be removed
  • anonymousCartRemovalAge: Anonymous carts older then a specified number of seconds will be removed

ycommercewebservices-items.xml

...
		<itemtype code="OldCartRemovalCronJob" autocreate="true"
			generate="true" extends="CronJob"
			jaloclass="de.hybris.platform.ycommercewebservices.jalo.OldCartRemovalCronJob">				
		<attributes>
			<attribute type="BaseSiteCollection" qualifier="sites">
					<modifiers/>
					<persistence type="property"/>
					<description>BaseSites for which old carts should be removed</description>
			</attribute>
			<attribute type="java.lang.Integer" qualifier="cartRemovalAge">
				<modifiers/>
				<persistence type="property"/>
				<defaultvalue>Integer.valueOf(2419200)</defaultvalue>
				<description>After specified number of seconds carts will be cleaned up. Default is 28 days.</description>
			</attribute>				
			<attribute type="java.lang.Integer" qualifier="anonymousCartRemovalAge">
					<modifiers/>
					<persistence type="property"/>
					<defaultvalue>Integer.valueOf(1209600)</defaultvalue>
					<description>After specified number of seconds carts will be cleaned up. Default is 14 days.</description>
			</attribute>
		</attributes>				
	</itemtype>
...

Job Object

The OldCartRemovalJob class defines the logic responsible for removing old cards. The relevant bean is defined in the ycommercewebservices-spring.xml file. See the code sample for an example.

...
	<bean id="oldCartRemovalJob" class="de.hybris.platform.ycommercewebservices.cronjob.OldCartRemovalJob"  parent="abstractJobPerformable">
		<property name="commerceCartDao" ref="commerceCartDao"/>
		<property name="timeService" ref="timeService"/>
		<property name="userService" ref="userService"/>
	</bean>
...

Trigger

The sample OldCartRemovalCronJob is created during the init/update process. It remains active and is triggered daily at 3:30 AM. The script located in the ycommercewebservices/resource/ directory provides an example of adding a cronjob.

...
# Old Cart Cleanup CronJobs
# This is only sample cron job but to make it work correctly you need to set also sites property for it
INSERT_UPDATE OldCartRemovalCronJob;code[unique=true];job(code);cartRemovalAge;anonymousCartRemovalAge;sessionLanguage(isoCode)[default=en]
;oldCartRemovalCronJob;oldCartRemovalJob;2419200;1209600

INSERT_UPDATE Trigger;cronJob(code)[unique=true];second;minute;hour;day;month;year;relative;active;maxAcceptableDelay
;oldCartRemovalCronJob;0;30;3;-1;-1;-1;false;true;-1
...

Example for adding the sites attribute with help of the ImpEx script file.

#Setting Old Cart Cleanup CronJobs for wsTest site
INSERT_UPDATE OldCartRemovalCronJob;code[unique=true];job(code);sites(uid)
;oldCartRemovalCronJob;oldCartRemovalJob;wsTest