I know that magento has cron jobs by default for specific tasks, I need the cron job to cancel unprocessed orders that have a time greater than 48 hours. Please help me
1 Answer
create your custom module to run cron job. I have explain you how to do this
- create/open your module config.xml file and put below code in
To run a cron job every 48 hours.The fields denote (from left-to-right):Minute, Hour, Day of Month, Month, Day of Week. The "*/2" in the Day of Month field means "every two days".
<config>
<modules>
<Cancelorder_Cron>
<version>0.0.1</version>
</Cancelorder_Cron>
</modules>
<global>
<models>
<Cancelorder_Cron>
<class>Cancelorder_Cron_Model</class>
</Cancelorder_Cron>
</models>
</global>
<crontab>
<jobs>
<Cancelorder_Cron>
<schedule>
<cron_expr>0 2 */2 * *</cron_expr>
</schedule>
<run>
<model>Cancelorder_Cron/cron::cancelUnprocessedOrders</model>
</run>
</Cancelorder_Cron>
</jobs>
</crontab>
</config>
- create/open your module model file Cancelorder/Cron/Model/Cron.php And add below code
<?php class Cancelorder_Cron_Model_Cron { public function cancelUnprocessedOrders($observer) { $orderCollection = Mage::getModel('sales/order')->getCollection() ->addAttributeToFilter('status', array('neq' => Mage_Sales_Model_Order::STATE_PENDING_PAYMENT)) ->addAttributeToSelect('*'); $orders =""; foreach($orderCollection->getItems() as $order) { $orderModel = Mage::getModel('sales/order'); $orderModel->load($order['entity_id']); if(!$orderModel->canCancel()) continue; $orderModel->cancel(); $orderModel->setState(Mage_Sales_Model_Order::STATE_CANCELED, true); $orderModel->save(); } } }
-
And can I call this cron through the browser to test? Commented Jul 24, 2018 at 12:40
-
Yes you can . please review this link magento.stackexchange.com/questions/78715/testing-cron-module . and update your code– ShorabhCommented Jul 24, 2018 at 12:46
-
-
Where can I create the module? some specific directory? Commented Jul 24, 2018 at 19:03
-