From 1f4fc49966d4eae5b908a775373b22183caa8be8 Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 13 Sep 2016 10:18:25 -0300 Subject: [PATCH] Loaded Order Model to get the full data. Added guest customer purchase data sent. Fixed new problem when sending products and customers. --- .../Ebizmarts/MailChimp/Model/Api/Batches.php | 2 ++ .../Ebizmarts/MailChimp/Model/Api/Customers.php | 14 +++++++------- .../Ebizmarts/MailChimp/Model/Api/Orders.php | 14 +++++++++----- .../Ebizmarts/MailChimp/Model/Api/Products.php | 7 ++++++- .../Ebizmarts/MailChimp/Model/Observer.php | 1 - 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php index e800c9170..0dbcf5417 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php @@ -295,6 +295,7 @@ protected function processEachResponseFile($files) $p = Mage::getModel('catalog/product')->load($id); if ($p->getId()==$id) { $p->setData("mailchimp_sync_error", $error); + $p->setMailchimpUpdateObserverRan(true); $p->save(); } else { Mage::helper('mailchimp')->logError("Error: product ".$id." not found"); @@ -304,6 +305,7 @@ protected function processEachResponseFile($files) $c = Mage::getModel('customer/customer')->load($id); if ($c->getId()==$id) { $c->setData("mailchimp_sync_error", $error); + $c->setMailchimpUpdateObserverRan(true); $c->save(); } else { Mage::helper('mailchimp')->logError("Error: customer ".$id." not found"); diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php index d8220df9b..555a1d24f 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php @@ -315,13 +315,13 @@ public function getMergeVars($object) } public function createGuestCustomer($guestId, $order) { - $guestCustomer = Mage::getModel('customer/customer') - ->setId($guestId) - ->setEmail($order->getCustomerEmail()) - ->setFirstName($order->getCustomerFirstname()) - ->setLastName($order->getCustomerLastname()) - ->setPrimaryBillingAddress($order->getBillingAddress()) - ->setPrimaryShippingAddress($order->getShippingAddress()); + $guestCustomer = Mage::getModel('customer/customer')->setId($guestId); + foreach ($order->getData() as $key => $value) { + $keyArray = explode('_', $key); + if ($value && isset($keyArray[0]) && $keyArray[0] == 'customer') { + $guestCustomer->{'set' . ucfirst($keyArray[1])}($value); + } + } return $guestCustomer; } } \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php index 5859c8ad1..ddf7b7755 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php @@ -25,9 +25,6 @@ public function createBatchJson($mailchimpStoreId) { //create missing products first $collection = Mage::getModel('sales/order')->getCollection() - ->addAttributeToSelect('status') - ->addAttributeToSelect('mailchimp_sync_delta') - ->addAttributeToSelect('mailchimp_sync_error') ->addAttributeToSelect('entity_id') ->addFieldToFilter('state', 'complete') ->addFieldToFilter('mailchimp_sync_delta', array( @@ -40,7 +37,8 @@ public function createBatchJson($mailchimpStoreId) $batchArray = array(); $batchId = Ebizmarts_MailChimp_Model_Config::IS_ORDER.'_'.date('Y-m-d-H-i-s'); $counter = 0; - foreach ($collection as $order) { + foreach ($collection as $item) { + $order = Mage::getModel('sales/order')->load($item->getEntityId()); $orderJson = $this->GeneratePOSTPayload($order, $mailchimpStoreId); if (!empty($orderJson)) { $batchArray[$counter]['method'] = "POST"; @@ -160,9 +158,15 @@ protected function GeneratePOSTPayload($orderFromCollection,$mailchimpStoreId) $api = $this->_getApi(); $customers = $api->ecommerce->customers->getByEmail($mailchimpStoreId, $order->getCustomerEmail()); if ($customers['total_items']>0) { + $id = $customers['customers'][0]['id']; $data['customer'] = array( - 'id' => $customers['customers'][0]['id'] + 'id' => $id ); + $guestCustomer = Mage::getModel('mailchimp/api_customers')->createGuestCustomer($id, $order); + $mergeFields = Mage::getModel('mailchimp/api_customers')->getMergeVars($guestCustomer); + if (is_array($mergeFields)) { + $data['customer'] = array_merge($mergeFields, $data['customer']); + } } else { if ((bool)$order->getCustomerIsGuest()) { $guestId = "GUEST-" . date('Y-m-d-H-i-s'); diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php index 8e27f329c..901735b72 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php @@ -40,7 +40,12 @@ public function createBatchJson($mailchimpStoreId) //define variants and root products if ($product->getMailchimpSyncModified()&&$product->getMailchimpSyncDelta()) { $batchArray = array_merge($this->_buildOldProductRequest($product, $batchId, $mailchimpStoreId), $batchArray); - $counter = (count($batchArray) - 1); + $counter = (count($batchArray)); + $product->setData("mailchimp_sync_delta", Varien_Date::now()); + $product->setData("mailchimp_sync_error", ""); + $product->setData('mailchimp_sync_modified', 0); + $product->setMailchimpUpdateObserverRan(true); + $product->save(); } else { $data = $this->_buildNewProductRequest($product, $batchId, $mailchimpStoreId); } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index a8789f869..f8bccbcec 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -181,7 +181,6 @@ public function productSaveBefore(Varien_Event_Observer $observer) } else { $product->setMailchimpUpdateObserverRan(true); } - //update mailchimp ecommerce data for that product variant Mage::getModel('mailchimp/api_products')->update($product); return $observer;