Skip to content

Commit

Permalink
Merge pull request #5 from erfanimani/improvements
Browse files Browse the repository at this point in the history
Added pause sync feature in case third-party API isn't available, cleaned up code, incremented version to 0.1.0
  • Loading branch information
ProxiBlue committed Oct 12, 2015
2 parents 26388cd + 7a0e2a1 commit 3d819ad
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 26 deletions.
11 changes: 9 additions & 2 deletions app/code/community/ProxiBlue/OrderSyncQueRunner/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php

class ProxiBlue_OrderSyncQueRunner_Helper_Data extends Mage_Core_Helper_Abstract
class ProxiBlue_OrderSyncQueRunner_Helper_Data
extends Mage_Core_Helper_Abstract
{

public function isSyncPaused()
{
$path = 'ordersyncquerunner/general/pause_order_sync';
return (bool)Mage::getStoreConfig($path);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class ProxiBlue_OrderSyncQueRunner_Model_Cron
public static function sync($schedule)
{
try {
$syncModel = Mage::getModel('ordersyncquerunner/que')
$queueCollection = Mage::getModel('ordersyncquerunner/que')
->getCollection()
->addFieldToFilter('synced_at', array('null' => true));
ProxiBlue_OrderSyncQueRunner_Model_Que::doSync($syncModel);
ProxiBlue_OrderSyncQueRunner_Model_Que::doSync($queueCollection);
} catch (Exception $e) {
// save any errors.
Mage::logException($e);
Expand Down Expand Up @@ -55,6 +55,4 @@ public static function clean($schedule)
}
}



}
19 changes: 11 additions & 8 deletions app/code/community/ProxiBlue/OrderSyncQueRunner/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ProxiBlue_OrderSyncQueRunner_Model_Observer
*/
public function sales_order_place_after(Varien_Event_Observer $observer)
{

$order = $observer->getEvent()->getOrder();
try {
$syncModel = Mage::getModel('ordersyncquerunner/que');
Expand All @@ -31,14 +30,18 @@ public function sales_order_place_after(Varien_Event_Observer $observer)
$syncModel->setData($data);
$syncModel->save();
} catch (Exception $e) {
Mage::log('could not place order into sync que !' . $e->getMessage());
// attempt to sync right now.
Mage::dispatchEvent(
'sales_order_place_after_que',
array('order'=>$order)
);
Mage::log("Couldn't place order into sync queue! " . $e->getMessage());

// Attempt to sync right now if sync isn't paused
if (!Mage::helper('ordersyncquerunner')->isSyncPaused()) {
Mage::dispatchEvent(
'sales_order_place_after_que',
array('order' => $order)
);
}

}
return $this;
}

}
}
19 changes: 14 additions & 5 deletions app/code/community/ProxiBlue/OrderSyncQueRunner/Model/Que.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
<?php

/**
*
* Queue model
*
* @category ProxiBlue
* @package ProxiBlue_OrderSyncQueRunner
* @author Lucas van Staden (support@proxiblue.com.au)
*/
class ProxiBlue_OrderSyncQueRunner_Model_Que extends Mage_Core_Model_Abstract
{
protected function _construct(){

protected function _construct()
{
$this->_init("ordersyncquerunner/que");
}

/**
* Handle sync of data
* Dispatches new event to efect sync
* Dispatches new event to effect sync
*
* @param type $syncModel
* @param ProxiBlue_OrderSyncQueRunner_Model_Resource_Que_Collection $syncModel
*/
static public function doSync($syncModel) {
static public function doSync(
ProxiBlue_OrderSyncQueRunner_Model_Resource_Que_Collection $syncModel
) {
$helper = Mage::helper('ordersyncquerunner');

if ($helper->isSyncPaused()) {
return;
}

foreach ($syncModel as $sync) {
$order = Mage::getModel('sales/order')->load($sync->getEntityId());
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
*
* Queue collection
*
* @category ProxiBlue
* @package ProxiBlue_OrderSyncQueRunner
Expand All @@ -12,7 +12,8 @@ class ProxiBlue_OrderSyncQueRunner_Model_Resource_Que_Collection
extends Mage_Core_Model_Mysql4_Collection_Abstract
{

public function _construct() {
public function _construct()
{
$this->_init("ordersyncquerunner/que");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
<children>
<config>
<children>
<ordersyncquemanager>
<title>Sync Queue Manager</title>
</ordersyncquemanager>
<ordersyncquerunner>
<title>Order Sync Queue Manager</title>
</ordersyncquerunner>
</children>
</config>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<ProxiBlue_OrderSyncQueRunner>
<version>0.0.2</version>
<version>0.1.0</version>
</ProxiBlue_OrderSyncQueRunner>
</modules>
<global>
Expand Down Expand Up @@ -83,4 +83,11 @@
</adminhtml>
</routers>
</admin>
<default>
<ordersyncquerunner>
<general>
<pause_order_sync>0</pause_order_sync>
</general>
</ordersyncquerunner>
</default>
</config>
34 changes: 34 additions & 0 deletions app/code/community/ProxiBlue/OrderSyncQueRunner/etc/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<config>
<sections>
<ordersyncquerunner translate="label" module="ordersyncquerunner">
<label>Order Sync Queue Manager</label>
<tab>sales</tab>
<frontend_type>text</frontend_type>
<sort_order>1100</sort_order>
<show_in_default>1</show_in_default>
<!-- Don't show in other scopes because it's run only on admin anyway -->
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<groups>
<general translate="label">
<label>General</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<expanded>1</expanded>
<fields>
<pause_order_sync translate="label">
<label>Pause queue sync</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<comment>Orders will still be saved to the queue, but won't be synced until unpaused. This could cause a lot of processing if you unpause it after a long time period.</comment>
</pause_order_sync>
</fields>
</general>
</groups>
</ordersyncquerunner>
</sections>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
->addColumn('synced_at', Varien_Db_Ddl_Table::TYPE_DATETIME, null, array(
'unsigned' => true,
'nullable' => true,
), 'Synced') ;
), 'Synced');

$installer->getConnection()->createTable($table);
$installer->endSetup();

0 comments on commit 3d819ad

Please sign in to comment.