0

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 1

0

create your custom module to run cron job. I have explain you how to do this

  1. 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>
  1. 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(); 
        }
    } 
}
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.