Skip to content

Commit

Permalink
Removing orders canceled from MailChimp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiagoebizmarts committed Jul 26, 2016
1 parent 081de00 commit 4a92906
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 131 deletions.
6 changes: 5 additions & 1 deletion app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
92 changes: 62 additions & 30 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
94 changes: 1 addition & 93 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = "";
Expand Down Expand Up @@ -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();
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
}
catch (Exception $e)
{
Mage::helper('mailchimp')->logError($e->getMessage());
Mage::log($e->getMessage());
}

$installer->run("
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
");
}
catch(Exception $e){
Mage::helper('mailchimp')->logError($e->getMessage());
Mage::log($e->getMessage());
}

$installer->endSetup();
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
}
catch (Exception $e)
{
Mage::helper('mailchimp')->logError($e->getMessage());
Mage::log($e->getMessage());
}

$installer->endSetup();
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,7 @@
}
catch (Exception $e)
{
Mage::helper('mailchimp')->logError($e->getMessage());
Mage::log($e->getMessage());
}

$installer->endSetup();

1 comment on commit 4a92906

@Santiagoebizmarts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closes #4

Please sign in to comment.