2

I have created a new module that has a custom cron task to export orders. What i'm trying to do though is to only export orders that have been placed since the cron task last completed successfully.

Here's my etc/config.xml

<crontab>
    <jobs>
        <orders_exportsales>
            <schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
            <run><model>Orders/export::ExportSales</model></run>
        </orders_exportsales>
    </jobs>
</crontab>

My controller

public function exportSalesAction()
{
    Mage::getModel('Garraways_OrderSync/export')->OrderExport();
}

In my model, I am then grabbing the orders to export, but I would like to use the 'start from' as the time that the cron task last finished so that I can get new orders. I assume that i will need to check for the relevant job_code, status and finished_at but i'm not sure how to do that from within the model so that it just gets the orders since the cron task last ran?

public function OrderExport()
{

    //Check here for job_code, status and finished_at?
    $lastRun = ;
    $current = now();

    //A collection of orders
    $orderCollection = Mage::getModel('sales/order')->getCollection();

    $orderCollection
        ->addAttributeToFilter('status', array('in' => array('pending', 'complete')))
        ->addFieldToFilter('start_date', array('gteq' => $lastRun))
        ->addFieldToFilter('end_date', array("lteq" => $current));

}
2
  • 1
    It would be more robust to log which orders have been exported rather than rely on the time. Jul 26, 2017 at 10:36
  • @WilliamOakley Thank you. I'm looking into logging these as well. Jul 26, 2017 at 13:15

3 Answers 3

2

As @william-oakley suggested and unless you need to stick to the lastRunTime approach for some reason, it would be more robust to read and log which orders have been exported rather than rely on the time.

public function ExportSales()
{
    //A collection of orders
    $orderCollection = Mage::getModel('sales/order')->getCollection();

    $orderCollection
    ->addAttributeToFilter('status', array('in' => array('pending', 'complete')))

   // Filter by Non exported
    ->addFieldToFilter('exported', array('eq' => '0'));
}

Of course for this to work you will need to create the exported field in the database and update it as exported when you finish exporting the order.

2
  • Thanks. I realised using the filter on a custom order attribute might be best. I guess I can just update the particular order attribute each time the cron task runs so that it doesn't include those orders the next time. Jul 26, 2017 at 13:14
  • Glad to help. If this answer or any other one helped to solve your issue, please mark it as accepted.
    – diazwatson
    Jul 26, 2017 at 13:17
0

To get last finished date you can try this:

$tasks = Mage::getModel('cron/schedule')->getCollection()
    ->addFieldToSelect('finished_at')
    ->addFieldToFilter('job_code', 'orders_exportsales')
    ->addFieldToFilter('status', 'success');
$tasks->getSelect()
    ->limit(1)
    ->order('finished_at DESC');

$lastRun = $tasks->getFirstItem()->getFinishedAt();

Bug:

app/code/core/Mage/Cron/Model/Resource/Schedule/Collection.php

Mage::getModel('cron/schedule')->getCollection()
    ->orderByScheduledAt($dir)

Sort direction is always set to ASC ... to fix replace

 $this->getSelect()->order('scheduled_at', $dir);

with

$this->getSelect()->order('scheduled_at ' . $dir);
0
-1

I think you are using here little bit incorrect strategy. Why you do not want to write last date into config (in case orders were exported successfully), and on next cron running use this date as "Start_from". To store date you can use something like

Mage::getConfig()->saveConfig('custom_module/order_export/last_run', date('Y-m-d H:i:s'), 'default', 0);

To get this date back use

Mage::getStoreConfig('custom_module/order_export/last_run');

Please note that this date can be cached. In that case you can get config date directly from DB (core_config_data table)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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