Skip to content

Commit

Permalink
Loaded Order Model to get the full data.
Browse files Browse the repository at this point in the history
Added guest customer purchase data sent.
Fixed new problem when sending products and customers.
  • Loading branch information
Santiagoebizmarts committed Sep 13, 2016
1 parent c9e668d commit 1f4fc49
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
14 changes: 7 additions & 7 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 9 additions & 5 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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";
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion app/code/community/Ebizmarts/MailChimp/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1f4fc49

Please sign in to comment.