From 4a9290698aa4d1c49e29799a0f998178e33175ca Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 26 Jul 2016 10:20:20 -0300 Subject: [PATCH] Removing orders canceled from MailChimp. --- .../Ebizmarts/MailChimp/Model/Api/Batches.php | 6 +- .../Ebizmarts/MailChimp/Model/Api/Orders.php | 92 ++++++++++++------ .../MailChimp/Model/Api/Products.php | 94 +------------------ .../MailChimp/Model/Api/Subscribers.php | 4 +- .../mailchimp_setup/mysql4-install-0.0.1.php | 4 +- .../mysql4-upgrade-1.0.1-1.0.2.php | 2 +- .../mysql4-upgrade-1.0.3-1.0.4.php | 2 +- ...0.5.php => mysql4-upgrade-1.0.5-1.0.6.php} | 3 +- 8 files changed, 76 insertions(+), 131 deletions(-) rename app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/{mysql4-upgrade-1.0.4-1.0.5.php => mysql4-upgrade-1.0.5-1.0.6.php} (85%) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php index d57096331..2e5669fd7 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php @@ -84,10 +84,14 @@ public function _sendEcommerceBatch($mailchimpStoreId) $batchArray['operations'] = array_merge($batchArray['operations'],$productsArray); if (empty($productsArray)) { //order operations - $cartsArray =Mage::getModel('mailchimp/api_carts')->createBatchJson($mailchimpStoreId); + $cartsArray = Mage::getModel('mailchimp/api_carts')->createBatchJson($mailchimpStoreId); $batchArray['operations'] = array_merge($batchArray['operations'],$cartsArray); $ordersArray = Mage::getModel('mailchimp/api_orders')->createBatchJson($mailchimpStoreId); $batchArray['operations'] = array_merge($batchArray['operations'],$ordersArray); + if(empty($ordersArray)){ + $ordersCanceledArray = Mage::getModel('mailchimp/api_orders')->createCanceledBatchJson($mailchimpStoreId); + $batchArray['operations'] = array_merge($batchArray['operations'],$ordersCanceledArray); + } } if (!empty($batchArray['operations'])) { try { diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php index d4297931c..99c0f8a45 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php @@ -17,38 +17,70 @@ class Ebizmarts_MailChimp_Model_Api_Orders public function createBatchJson($mailchimpStoreId) { - //create missing products first - $collection = Mage::getModel('sales/order')->getCollection() - ->addAttributeToSelect('status') - ->addAttributeToSelect('mailchimp_sync_delta') - ->addAttributeToSelect('entity_id') - ->addFieldToFilter('status', 'complete') - ->addFieldToFilter('mailchimp_sync_delta', array( - array('null' => true), - array('eq' => ''), - array('lt' => Mage::helper('mailchimp')->getMCMinSyncDateFlag()) - )); - $collection->getSelect()->limit(self::BATCH_LIMIT); - - $batchArray = array(); - $batchId = Ebizmarts_MailChimp_Model_Config::IS_ORDER.'_'.date('Y-m-d-H-i-s'); - $counter = 0; - foreach ($collection as $order) { - $orderJson = $this->GeneratePOSTPayload($order,$mailchimpStoreId); - if (!empty($orderJson)) { - $batchArray[$counter]['method'] = "POST"; - $batchArray[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/orders'; - $batchArray[$counter]['operation_id'] = $batchId . '_' . $order->getEntityId(); - $batchArray[$counter]['body'] = $orderJson; - - //update order delta - $order->setData("mailchimp_sync_delta", Varien_Date::now()); - $order->save(); - } - $counter += 1; + //create missing products first + $collection = Mage::getModel('sales/order')->getCollection() + ->addAttributeToSelect('status') + ->addAttributeToSelect('mailchimp_sync_delta') + ->addAttributeToSelect('entity_id') + ->addFieldToFilter('state', 'complete') + ->addFieldToFilter('mailchimp_sync_delta', array( + array('null' => true), + array('eq' => ''), + array('lt' => Mage::helper('mailchimp')->getMCMinSyncDateFlag()) + )); + $collection->getSelect()->limit(self::BATCH_LIMIT); + + $batchArray = array(); + $batchId = Ebizmarts_MailChimp_Model_Config::IS_ORDER.'_'.date('Y-m-d-H-i-s'); + $counter = 0; + foreach ($collection as $order) { + $orderJson = $this->GeneratePOSTPayload($order,$mailchimpStoreId); + if (!empty($orderJson)) { + $batchArray[$counter]['method'] = "POST"; + $batchArray[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/orders'; + $batchArray[$counter]['operation_id'] = $batchId . '_' . $order->getEntityId(); + $batchArray[$counter]['body'] = $orderJson; + + //update order delta + $order->setData("mailchimp_sync_delta", Varien_Date::now()); + $order->save(); + } + $counter += 1; + } + + return $batchArray; + } + + public function createCanceledBatchJson($mailchimpStoreId) + { + //create missing products first + $collection = Mage::getModel('sales/order')->getCollection() + ->addAttributeToSelect('status') + ->addAttributeToSelect('mailchimp_sync_delta') + ->addAttributeToSelect('entity_id') + ->addFieldToFilter('state', 'canceled') + ->addFieldToFilter('mailchimp_sync_delta', array( + array('null' => true), + array('eq' => ''), + array('lt' => Mage::helper('mailchimp')->getMCMinSyncDateFlag()) + )); + $collection->getSelect()->limit(self::BATCH_LIMIT); + + $batchArray = array(); + $counter = 0; + foreach ($collection as $order) { + if (!empty($orderJson)) { + $batchArray[$counter]['method'] = "DELETE"; + $batchArray[$counter]['path'] = '/ecommerce/stores/' . $mailchimpStoreId . '/orders/' . $order->getEntityId(); + + //update order delta + $order->setData("mailchimp_sync_delta", Varien_Date::now()); + $order->save(); } + $counter += 1; + } - return $batchArray; + return $batchArray; } protected function GeneratePOSTPayload($order_from_collection,$mailchimpStoreId) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php index b8a657717..d179a254e 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php @@ -163,7 +163,7 @@ protected function _buildProductData($product, $isVarient = true, $variants = nu //image $productMediaConfig = Mage::getModel('catalog/product_media_config'); - $data["image_url"] = $productMediaConfig->getMediaUrl($product->getImage());; + $data["image_url"] = $productMediaConfig->getMediaUrl($product->getImage()); //missing data $data["published_at_foreign"] = ""; @@ -207,100 +207,8 @@ protected function _buildProductData($product, $isVarient = true, $variants = nu public function update($product) { if (Mage::helper('mailchimp')->isEcomSyncDataEnabled()) { -// $product->setData('mailchimp_sync_delta', null); $product->setData('mailchimp_sync_error', ''); $product->setData('mailchimp_sync_modified',1); } } -// public function old_update($product) -// { -// try { -// -// if (Mage::helper('mailchimp')->isEcomSyncDataEnabled()) { -// $apiKey = Mage::helper('mailchimp')->getConfigValue(Ebizmarts_MailChimp_Model_Config::GENERAL_APIKEY); -// $mailchimpStoreId = Mage::helper('mailchimp')->getMCStoreId(); -// -// if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE || $product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL || $product->getTypeId() == "downloadable") { -// $data = $this->_buildProductData($product); -// -// $parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($product->getId()); -//// $parentIds = $product->getTypeInstance()->getParentIdsByChild($product->getId()); -// -// if (empty($parentIds)) { -// $parentIds = array($product->getId()); -// } -// -// //add or update variant -// $mailchimpApi = new Ebizmarts_Mailchimp($apiKey); -// foreach ($parentIds as $parentId) { -// $mailchimpApi->ecommerce->products->variants->addOrModify( -// $mailchimpStoreId, -// $parentId, -// $data["id"], -// $data["title"], -// $data["url"], -// $data["sku"], -// $data["price"], -// $data["inventory_quantity"], -// $data["image_url"], -// $data["backorders"], -// $data["visibility"] -// ); -// } -// -// } else if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { -// //check if it was never uploaded and create it -// if (!$product->getMailchimpSyncDelta()) { -// -// $dataRootProduct = $this->_buildProductData($product, false, array($product)); -// -// $mailchimpApi = new Ebizmarts_Mailchimp($apiKey); -// $mailchimpApi->ecommerce->products->add( -// $mailchimpStoreId, -// $dataRootProduct["id"], -// $dataRootProduct["title"], -// $dataRootProduct["handle"], -// $dataRootProduct["url"], -// $dataRootProduct["description"], -// $dataRootProduct["type"], -// $dataRootProduct["vendor"], -// $dataRootProduct["image_url"], -// $dataRootProduct["variants"], -// $dataRootProduct["published_at_foreign"] -// ); -// } else { -// -// throw new Exception('MailChimp root products can not be updated'); -// } -// -// } else { -// //@toDo bundle -// -// throw new Exception('These type of products are not yet supported'); -// } -// -// //update product delta -// $product->setData("mailchimp_sync_delta", Varien_Date::now()); -// $product->setData("mailchimp_sync_error", ""); -// $product->save(); -// } -// -// } catch (Mailchimp_Error $e) { -// Mage::helper('mailchimp')->logError($e->getFriendlyMessage()); -// -// //update product delta -// $product->setData("mailchimp_sync_delta", Varien_Date::now()); -// $product->setData("mailchimp_sync_error", $e->getFriendlyMessage()); -// $product->save(); -// -// } catch -// (Exception $e) { -// Mage::helper('mailchimp')->logError($e->getMessage()); -// -// //update product delta -// $product->setData("mailchimp_sync_delta", Varien_Date::now()); -// $product->setData("mailchimp_sync_error", $e->getMessage()); -// $product->save(); -// } -// } } \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php index 546b166e2..a39394728 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php @@ -219,7 +219,9 @@ protected function getMergeVars($subscriber) $mergeVars[$key] = $customerGroup[$group_id]; break; default: - $mergeVars[$key] = $customer->getData($attributeCode); + if($customer->getData($attributeCode)) { + $mergeVars[$key] = $customer->getData($attributeCode); + } break; } } diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-install-0.0.1.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-install-0.0.1.php index c8bba2e5e..eb1248e1a 100755 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-install-0.0.1.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-install-0.0.1.php @@ -87,7 +87,7 @@ } catch (Exception $e) { - Mage::helper('mailchimp')->logError($e->getMessage()); + Mage::log($e->getMessage()); } $installer->run(" @@ -106,7 +106,7 @@ mkdir($baseDir . DS . 'var' . DS . 'mailchimp'); } catch (Exception $e){ - Mage::helper('mailchimp')->logError($e->getMessage()); + Mage::log($e->getMessage()); } $installer->endSetup(); diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.1-1.0.2.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.1-1.0.2.php index bc789e6ef..862e508bf 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.1-1.0.2.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.1-1.0.2.php @@ -37,7 +37,7 @@ "); } catch(Exception $e){ - Mage::helper('mailchimp')->logError($e->getMessage()); + Mage::log($e->getMessage()); } $installer->endSetup(); \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.3-1.0.4.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.3-1.0.4.php index 50680c581..c5a2e52ce 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.3-1.0.4.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.3-1.0.4.php @@ -49,7 +49,7 @@ } catch (Exception $e) { - Mage::helper('mailchimp')->logError($e->getMessage()); + Mage::log($e->getMessage()); } $installer->endSetup(); \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.4-1.0.5.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.5-1.0.6.php similarity index 85% rename from app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.4-1.0.5.php rename to app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.5-1.0.6.php index 59ad4b7e1..69b85acf0 100644 --- a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.4-1.0.5.php +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.0.5-1.0.6.php @@ -18,7 +18,6 @@ try { $installer->run(" - ALTER TABLE `{$this->getTable('sales_flat_quote')}` ADD column `mailchimp_sync_delta` datetime NOT NULL; ALTER TABLE `{$this->getTable('sales_flat_quote')}` ADD column `mailchimp_deleted` INT(1) NOT NULL DEFAULT 0; ALTER TABLE `{$this->getTable('sales_flat_quote')}` ADD column `mailchimp_token` VARCHAR(255) NOT NULL DEFAULT ''; ALTER TABLE `{$this->getTable('sales_flat_quote')}` ADD column `mailchimp_abandonedcart_flag` INT(1) NOT NULL DEFAULT 0; @@ -27,7 +26,7 @@ } catch (Exception $e) { - Mage::helper('mailchimp')->logError($e->getMessage()); + Mage::log($e->getMessage()); } $installer->endSetup(); \ No newline at end of file