Hi all
I want to run an observer function through a Cron job. Can anyone share the simple observer function.?
Here is my code
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="abandon_cron_test" instance="Abc\Abandon\Observer\Abandon" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>
Observer file
Anandon.php
namespace Abc\Abandon\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class Abandon implements ObserverInterface
{
protected $_objectManager;
/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager
) {
$this->_objectManager = $objectManager;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/abondoncronrunobserver.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('cron successfully');
}
}
Any Idea?
Thanks
Hi All
Does anyone have a solution for this?
Thanks
Hi @Shiwani Miglani
You can not trigger observer file directly through cron because you will not get data in observer file(i.e $observer variable will not have data) which you are willing to get.
You can do one thing create a separate cron file and trigger that event whose observer you are trying to call in this way you can send data to observer when you will trigger the event.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> <group id="default"> <job name="abandon_cron_test" instance="Abc\Abandon\Cron\Test" method="execute"> <schedule>* * * * *</schedule> </job> </group> </config>
Now create Test.php file and trigger same event,
$this->_eventManager->dispatch('event_name_trigger', ['product' => $productData]);
Or you can add the code in observer file to helper file and can use in observer and cron both. In this way you can write code at one place and use at both places.
Thanks for the reply
If I am using this code in the observer and want to get data in cron/test.php through observer with below event.
I want to write code in observer file and want to use in cron/test.php.
$this->_eventManager->dispatch('event_name_trigger', ['product' => $productData]);
How I can use?
Can you please describe?
Thanks
Hi All,
Does anyone have a solution for this?
Can you tell me the event for which you have created observer?
Lets say you have created an observer for event test_save_after and added some code in it.
When you run cron/test.php you want observer code to be executed so you can trigger that event from test.php file by using below code.
$this->_eventManager->dispatch('test_save_after', ['data' => $Data]);
It would be great if you will share your observer code so that I could help you in much better manner.
Thanks for the reply
Can you please share the test code of cron and observer with dispatch event passing data from observer to cron file?. I am not aware of this. On R&D getting a simple solution.
I need to get quote data(abandon cart data) through observer to cron file. And need to send data to the third party via cronjob.
<?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 name="abandon_cron_run" instance="Abc\Abandon\Cron\Run" method="execute"> <schedule>* * * * *</schedule> </job> </group> </config>
Thanks
I need to get quote data(abandon cart data) through observer to cron file. And need to send data to the third party via cronjob.
--I dont think it is possible. You cant get observer data in cron file. I was thinking that you want to run any specifiec observer though cron. But getting quote data in cron is not possible.
For getting quote data you need checkout session which will not be available on your cron file.
I was thinking that you want to run any specific observer through cron.
--- This is also not working. We can't run observer through cron.