Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series.
#magento2 #magento #magento2tutorial
Today, I am going to show everybody the best practice, How to create a custom cron job in Magento 2.
Before doing this practice, we need to learn the concept cron job in Magento 2, which the case should we use it?
What is the cron job in Magento 2?
Cron job in Magento helps to execute the scripts which will run systematically in exact time and date. In Magento 2, Cron job runs scheduled tasks, re-indexing, generating newsletters, sitemaps and more.
Which the case should we use the cron job?
The cron jobs will be the perfect choice for repeated tasks every minute, every hour, every day or every week.
Let’s understand more Cron job in Magento 2, we are going to do a practice related to the newsletter subscription in Magento 2.
We will create a custom cron job in Magento 2 that allows checking all customers if these customers don’t subscribe to the newsletter, we will set their account subscribing to the newsletter. We will use the default cron group and this task will be repeated each minute.
So what will I do in this practice?
1. We will create a new module named PHPCuong_CustomCronJob
2. We will declare a new job code named phpcuong_customcronjob_newsletter_subscription executing the above task
Okie, let’s go.
Let’s complete this practice you need to follow steps by step:
Step 1: Declaring the new module called PHPCuong_CustomCronJob
– Create the namespace PHPCuong in the path appcode
– Create the module named CustomCronJob in the path appcodePHPCuong
– Create the new file named registration.php in the path appcodePHPCuongCustomCronJob
– Create the new file named module.xml in the path appcodePHPCuongCustomCronJobetc
End of step #1, I have been completed the codes to create the new module called PHPCuong_CustomCronJob. The version of this module is 2.3.0
Step 2: Declaring a new job code named phpcuong_customcronjob_newsletter_subscription
– Create the new file named crontab.xml in the path appcodePHPCuongCustomCronJobetc
– Create the new file named NewsletterSubscription.php in the path appcodePHPCuongCustomCronJobCron
Step 4: Test and see the results
Run the following command lines:
php bin/magento setup:upgrade –keep-generated
php bin/magento cache:flush
php bin/magento cron:run
I will try to create a new customer account.
All the customer subscribed to the newsletter will be displayed here.
I didn’t subscribe to the newsletter.
For now, I will run the cron job
All the job codes will be saved this table.
The new cron job code saved and it run succeed
Yeah! It works perfectly.
Thank you for watching guide. If you have any questions about this practice please feel free to leave a comment below.
Don’t forget to like, comment, share and subscribe to my channel for getting the latest videos.
Please do not hesitate to contact me if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2.
Get the source code of this practice in the Github