How to Set up Custom Cron Job in Magento 2
Cron job enables to run a set of commands at scheduled date and time repeatedly. Fortunately, Magento 2 store owners can use this functionality in the store! However, before making any changes to your live site, it’s essential to set up a staging environment in Magento 2. This allows you to test any updates or changes without risking disruption to your live site.
The method to set up custom cron job in Magento 2 helps the store owner to effectively manage tasks like keeping the caches and indexes up to date and timely cleanups. Moreover, there are tasks that need to be done later in order to avoid system overload, or simply due to the requirement of condition fulfillment. But if want some of the tasks to be done in decided hours you need to Dynamically Schedule Cron Job in Magento 2 System Configuration.
For example, the customers who bought your product are expected to leave a review, and a reminder Email is supposed to be sent after a few days of the purchase. The review reminder Email can be automated in the Magento 2 store using the below method!
There are other tasks for which a custom cron job can be helpful such as:
- Generating Google sitemaps
- Customer alerts or notifications regarding product price change, product back in stock, etc.
- All Magento e-mails (including order confirmation and transactional)
- Catalog price rules
- Newsletters
- Reindexing
- Private sales (Magento Commerce only)
- Automatic updating of currency rates
Method to Set up Custom Cron Job in Magento 2:
- Create registration.php file in app\code\[Vendor]\[Namespace]\
123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'[Vendor]_[Namespace]',__DIR__); - Create module.xml file in app\code\[Vendor]\[Namespace]\etc
1234<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="[Vendor]_[Namespace]" setup_version="1.0.0"/></config> - Create crontab.xml file in app\code\[Vendor]\[Namespace]\etc
12345678<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"><group id="default"><job instance="[Vendor]\[Namespace]\Cron\Reminder" method="execute" name="vendor_namespace_cron"><schedule>0 4 * * *</schedule> <!-- here you need to schedule time to run cron job --></job></group></config> - Create Reminder.php file in app\code\[Vendor]\[Namespace]\Cron
12345678910111213141516171819<?phpnamespace [Vendor]\[Namespace]\Cron;class Reminder{public function execute(){/*** Here you need to define logic**/return $this;}}
With the above method, automate the repetitive tasks and optimize the store management system!
Do let me know how you are going to use the method in the Comments section below!
Any doubts in the implementation are welcome and I’d be happy to help 🙂
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Use Custom Variables in Transactional Email in Magento 2
How to Create Product Attribute in Magento 2
Next