Skip to content

Commit

Permalink
After phpcbf ran.
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiagoebizmarts committed Mar 30, 2017
1 parent 3007ae5 commit 811d7ae
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 17 deletions.
3 changes: 2 additions & 1 deletion app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function handleEcommerceBatches()
$this->_getResults($store->getId());
$this->_sendEcommerceBatch($store->getId());
}

foreach ($stores as $store) {
if (Mage::helper('mailchimp')->getIsReseted($store->getId())) {
$scopeToReset = Mage::helper('mailchimp')->getMailChimpScopeByStoreId($store->getId());
Expand Down Expand Up @@ -54,7 +55,6 @@ protected function _getResults($magentoStoreId)
->addFieldToFilter('status', array('eq' => 'pending'));
foreach ($collection as $item) {
try {

$files = $this->getBatchResponse($item->getBatchId(), $magentoStoreId);
if (count($files)) {
if (isset($files['error'])) {
Expand Down Expand Up @@ -126,6 +126,7 @@ public function _sendEcommerceBatch($magentoStoreId)
}
}
}

$itemAmount = ($customerAmount + $productAmount + $orderAmount);
if (Mage::helper('mailchimp')->getMCIsSyncing($magentoStoreId) && $itemAmount == 0) {
$isSyncing = false;
Expand Down
11 changes: 8 additions & 3 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Carts.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId)
array('m4m.*')
);
// be sure that the quotes are already in mailchimp and not deleted
$modifiedCarts->getSelect()->where("m4m.mailchimp_sync_modified = 1 AND m4m.mailchimp_sync_deleted = 0
AND m4m.mailchimp_sync_delta < '" . new Zend_Db_Expr('updated_at') . "'");
$modifiedCarts->getSelect()->where(
"m4m.mailchimp_sync_modified = 1 AND m4m.mailchimp_sync_deleted = 0
AND m4m.mailchimp_sync_delta < '" . new Zend_Db_Expr('updated_at') . "'"
);
// limit the collection
$modifiedCarts->getSelect()->limit(self::BATCH_LIMIT);
foreach ($modifiedCarts as $cart) {
Expand Down Expand Up @@ -154,6 +156,7 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId)

$allCartsForEmail->clear();
}

// avoid carts abandoned as guests when customer email associated to a registered customer.
if (!$cart->getCustomerId() && $customer->getEmail()==$cart->getCustomerEmail()) {
$this->_updateSyncData($cartId, $mailchimpStoreId, Varien_Date::now());
Expand All @@ -180,8 +183,8 @@ protected function _getModifiedQuotes($mailchimpStoreId, $magentoStoreId)
} else {
$this->_updateSyncData($cartId, $mailchimpStoreId, Varien_Date::now());
}
$this->_token = null;

$this->_token = null;
}

return $allCarts;
Expand All @@ -205,6 +208,7 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId)
if ($this->_firstDate) {
$newCarts->addFieldToFilter('created_at', array('gt' => $this->_firstDate));
}

//join with mailchimp_ecommerce_sync_data table to filter by sync data.
$newCarts->getSelect()->joinLeft(
array('m4m' => $mailchimpTableName),
Expand Down Expand Up @@ -272,6 +276,7 @@ protected function _getNewQuotes($mailchimpStoreId, $magentoStoreId)
} else {
$this->_updateSyncData($cartId, $mailchimpStoreId, Varien_Date::now());
}

$this->_token = null;
}

Expand Down
14 changes: 9 additions & 5 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId)
//get customers
$mailchimpTableName = Mage::getSingleton('core/resource')->getTableName('mailchimp/ecommercesyncdata');
$collection = Mage::getModel('customer/customer')->getCollection();
$collection->addFieldToFilter('store_id',array('eq' => $magentoStoreId));
$collection->addFieldToFilter('store_id', array('eq' => $magentoStoreId));
$collection->getSelect()->joinLeft(
array('m4m' => $mailchimpTableName),
"m4m.related_id = e.entity_id and m4m.type = '".Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER."'
AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'",
array('m4m.*')
);
$collection->getSelect()->where("m4m.mailchimp_sync_delta IS null ".
"OR m4m.mailchimp_sync_modified = 1");
$collection->getSelect()->where(
"m4m.mailchimp_sync_delta IS null ".
"OR m4m.mailchimp_sync_modified = 1"
);
$collection->getSelect()->limit(self::BATCH_LIMIT);
$customerArray = array();

Expand Down Expand Up @@ -76,11 +78,13 @@ protected function _buildCustomerData($customer, $magentoStoreId)

//customer orders data
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('state',
->addFieldToFilter(
'state',
array(
array('neq' => Mage_Sales_Model_Order::STATE_CANCELED),
array('neq' => Mage_Sales_Model_Order::STATE_CLOSED)
))
)
)
->addAttributeToFilter('customer_id', array('eq' => $customer->getId()));

$totalOrders = 0;
Expand Down
17 changes: 15 additions & 2 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ protected function _getNewOrders($mailchimpStoreId, $magentoStoreId)
if ($this->_firstDate) {
$newOrders->addFieldToFilter('created_at', array('gt' => $this->_firstDate));
}

$newOrders->getSelect()->joinLeft(
array('m4m' => $mailchimpTableName),
"m4m.related_id = main_table.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_ORDER . "'
Expand All @@ -138,6 +139,7 @@ protected function _getNewOrders($mailchimpStoreId, $magentoStoreId)
$this->_counter++;
}
}

$orderJson = $this->GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreId);
if (!empty($orderJson)) {
$batchArray[$this->_counter]['method'] = "POST";
Expand Down Expand Up @@ -231,6 +233,7 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
} else {
$variant = $productId;
}

if ($productSyncData->getMailchimpSyncDelta() && $productSyncData->getMailchimpSyncError() == 0) {
$itemCount++;
$data["lines"][] = array(
Expand All @@ -243,10 +246,12 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
);
}
}

if (!$itemCount) {
unset($data['lines']);
return "";
}

//customer data
$api = Mage::helper('mailchimp')->getApi($magentoStoreId);
$customers = array();
Expand All @@ -255,6 +260,7 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
} catch (Mailchimp_Error $e) {
Mage::helper('mailchimp')->logError($e->getFriendlyMessage(), $magentoStoreId);
}

if (!$isModifiedOrder) {
if (isset($customers['total_items']) && $customers['total_items'] > 0) {
$id = $customers['customers'][0]['id'];
Expand Down Expand Up @@ -310,6 +316,7 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
if ($order->getCustomerLastname()) {
$data["customer"]["last_name"] = $order->getCustomerLastname();
}

$billingAddress = $order->getBillingAddress();
$street = $billingAddress->getStreet();
$address = array();
Expand Down Expand Up @@ -342,6 +349,7 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
$address["country"] = $data['billing_address']["country"] = $countryName;
$address["country_code"] = $data['billing_address']["country_code"] = $billingAddress->getCountry();
}

if (count($address)) {
$data["customer"]["address"] = $address;
}
Expand All @@ -354,6 +362,7 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
if ($billingAddress->getCompany()) {
$data["customer"]["company"] = $data["billing_address"]["company"] = $billingAddress->getCompany();
}

$shippingAddress = $order->getShippingAddress();
if ($shippingAddress) {
$street = $shippingAddress->getStreet();
Expand Down Expand Up @@ -394,13 +403,16 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
$data["shipping_address"]["company"] = $shippingAddress->getCompany();
}
}

//customer orders data
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('state',
->addFieldToFilter(
'state',
array(
array('neq' => Mage_Sales_Model_Order::STATE_CANCELED),
array('neq' => Mage_Sales_Model_Order::STATE_CLOSED)
))
)
)
->addAttributeToFilter('customer_email', array('eq' => $order->getCustomerEmail()));
$totalOrders = 1;
$totalAmountSpent = (int)$order->getGrandTotal();
Expand All @@ -419,6 +431,7 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
//json encode failed
Mage::helper('mailchimp')->logError("Order " . $order->getEntityId() . " json encode failed", $magentoStoreId);
}

return $jsonData;
}

Expand Down
13 changes: 11 additions & 2 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId)
AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'",
array('m4m.*')
);
$collection->getSelect()->where("m4m.mailchimp_sync_delta IS null ".
"OR m4m.mailchimp_sync_modified = 1");
$collection->getSelect()->where(
"m4m.mailchimp_sync_delta IS null ".
"OR m4m.mailchimp_sync_modified = 1"
);
$collection->getSelect()->limit(self::BATCH_LIMIT);

$batchArray = array();
Expand All @@ -40,6 +42,7 @@ public function createBatchJson($mailchimpStoreId, $magentoStoreId)
if ($item->getMailchimpSyncDeleted()) {
$batchArray = array_merge($this->buildProductDataRemoval($product, $batchId, $mailchimpStoreId, $magentoStoreId), $batchArray);
}

//define variants and root products
if ($item->getMailchimpSyncModified() && $item->getMailchimpSyncDelta() && $item->getMailchimpSyncDelta() > Mage::helper('mailchimp')->getMCMinSyncDateFlag($magentoStoreId)) {
$batchArray = array_merge($this->_buildOldProductRequest($product, $batchId, $mailchimpStoreId, $magentoStoreId), $batchArray);
Expand Down Expand Up @@ -130,6 +133,7 @@ protected function _buildOldProductRequest($product, $batchId, $mailchimpStoreI
if ($imageUrl) {
$variendata["image_url"] = $imageUrl;
}

$this->_parentImageUrl = null;
$variendata["backorders"] = $data["backorders"];
$variendata["visibility"] = $data["visibility"];
Expand Down Expand Up @@ -167,6 +171,7 @@ protected function _buildProductData($product, $isVarient = true, $variants = ar
if ($imageUrl) {
$data["image_url"] = $imageUrl;
}

//missing data
$data["published_at_foreign"] = "";

Expand Down Expand Up @@ -204,9 +209,11 @@ protected function _buildProductData($product, $isVarient = true, $variants = ar
if (isset($data["image_url"])) {
$this->_parentImageUrl = $data["image_url"];
}

foreach ($variants as $variant) {
$data["variants"][] = $this->_buildProductData($variant);
}

$this->_parentImageUrl = null;
}

Expand Down Expand Up @@ -248,6 +255,7 @@ public function sendModifiedProduct($order, $mailchimpStoreId, $magentoStoreId)
if ($product->getId()) {
$this->_updateSyncData($product->getId(), $mailchimpStoreId, Varien_Date::now(), "This product type is not supported on MailChimp.");
}

continue;
}

Expand All @@ -259,6 +267,7 @@ public function sendModifiedProduct($order, $mailchimpStoreId, $magentoStoreId)
$this->_updateSyncData($product->getId(), $mailchimpStoreId, Varien_Date::now());
}
}

return $data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function deleteMailChimpStore($mailchimpStoreId, $scopeId, $scope)
} catch (Exception $e) {
Mage::helper('mailchimp')->logError($e->getMessage(), $scopeId, $scope);
}

$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$resource = Mage::getResourceModel('mailchimp/synchbatches');
$connection->update($resource->getMainTable(), array('status'=>'canceled'), "status = 'pending'");
Expand Down
17 changes: 13 additions & 4 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function getMergeVars($subscriber)
if (!$firstName) {
$firstName = $subscriber->getSubscriberFirstname();
}

if ($firstName) {
$eventValue = $mergeVars[$key] = $firstName;
}
Expand All @@ -166,6 +167,7 @@ public function getMergeVars($subscriber)
if (!$lastName) {
$lastName = $subscriber->getSubscriberLastname();
}

if ($lastName) {
$eventValue = $mergeVars[$key] = $lastName;
}
Expand Down Expand Up @@ -196,11 +198,14 @@ public function getMergeVars($subscriber)
}
break;
}
Mage::dispatchEvent('mailchimp_merge_field_send_before', array(

Mage::dispatchEvent(
'mailchimp_merge_field_send_before', array(
'subscriber_email' => $subscriberEmail,
'merge_field_tag' => $attributeCode,
'merge_field_value' => &$eventValue
));
)
);
}
}
} else {
Expand Down Expand Up @@ -261,12 +266,16 @@ public function getMergeVars($subscriber)
}
break;
}
Mage::dispatchEvent('mailchimp_merge_field_send_before', array(

Mage::dispatchEvent(
'mailchimp_merge_field_send_before', array(
'subscriber_email' => $subscriberEmail,
'merge_field_tag' => $customAtt,
'merge_field_value' => &$eventValue
));
)
);
}

if ($eventValue) {
$mergeVars[$key] = $eventValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function send()
//If email is not an order confirmation email, it will check if Mandrill enable in default config
$storeId = Mage::app()->getStore()->getId();
}

if (Mage::helper('mailchimp/mandrill')->isMandrillEnabled($storeId)) {
$parameters = new Varien_Object($message->getMessageParameters());
$mailer = $this->getMail($storeId);
Expand Down
4 changes: 4 additions & 0 deletions app/code/community/Ebizmarts/MailChimp/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public function saveConfig(Varien_Event_Observer $observer)
if ($generalEnabled && $listId) {
$this->_createWebhook($listId, $scopeArray[1], $scopeArray[0]);
}

if (isset($post['groups']['general']['fields']['list']['inherit']) && Mage::helper('mailchimp')->getIfMCStoreIdExistsForScope($scopeArray[1], $scopeArray[0])) {
Mage::helper('mailchimp')->removeEcommerceSyncData($scopeArray[1], $scopeArray[0]);
Mage::helper('mailchimp')->resetCampaign($scopeArray[1], $scopeArray[0]);
Mage::helper('mailchimp')->clearErrorGrid($scopeArray[1], $scopeArray[0], true);
Mage::helper('mailchimp')->deleteStore($scopeArray[1], $scopeArray[0]);
}

if (isset($post['groups']['general']['fields']['list']['value']) && !Mage::helper('mailchimp')->getIfMCStoreIdExistsForScope($scopeArray[1], $scopeArray[0]) && Mage::helper('mailchimp')->isEcomSyncDataEnabled($scopeArray[1], $scopeArray[0], true)) {
Mage::helper('mailchimp')->createStore($post['groups']['general']['fields']['list']['value'], $scopeArray[1], $scopeArray[0]);
}
Expand Down Expand Up @@ -65,6 +67,7 @@ protected function _createWebhook($listId, $scopeId, $scope)
if (FALSE != strstr($hookUrl, '?', true)) {
$hookUrl = strstr($hookUrl, '?', true);
}

$api = Mage::helper('mailchimp')->getApi($scopeId, $scope);
if (Mage::helper('mailchimp')->getTwoWaySyncEnabled($scopeId, $scope)) {
$events = array(
Expand Down Expand Up @@ -534,6 +537,7 @@ public function productAttributeUpdate(Varien_Event_Observer $observer)
if ($storeId === 0) {
$scope = 'default';
}

$mailchimpStoreId = Mage::helper('mailchimp')->getMCStoreId($storeId, $scope);
Mage::log($storeId, null, 'observer.log', true);
foreach ($productIds as $productId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ protected function _profile(array $data)
if (count($customerCollection) > 0) {
$customerId = $customerCollection->getFirstItem()->getEntityId();
}

if ($customerId) {
$toUpdate = Mage::getModel('customer/customer')->load($customerId);
$toUpdate->setFirstname($data['data']['merges']['FNAME']);
Expand All @@ -179,6 +180,7 @@ protected function _profile(array $data)
$toUpdate->setSubscriberFirstname($data['data']['merges']['FNAME']);
$toUpdate->setSubscriberLastname($data['data']['merges']['LNAME']);
}

$toUpdate->save();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected function _afterSave()
} else {
$listId = Mage::helper('mailchimp')->getGeneralList($this->getScopeId(), $this->getScope());
}

$thisScopeHasMCStoreId = Mage::helper('mailchimp')->getIfMCStoreIdExistsForScope($this->getScopeId(), $this->getScope());

if ($thisScopeHasMCStoreId && (!$this->getValue() || !$moduleIsActive || !$listId)) {
Expand All @@ -30,6 +31,7 @@ protected function _afterSave()
Mage::helper('mailchimp')->clearErrorGrid($this->getScopeId(), $this->getScope(), true);
Mage::helper('mailchimp')->deleteStore($this->getScopeId(), $this->getScope());
}

if ($moduleIsActive && $listId && $this->getValue() && !$thisScopeHasMCStoreId) {
Mage::helper('mailchimp')->createStore($this->getValue(), $this->getScopeId(), $this->getScope());
}
Expand Down
Loading

0 comments on commit 811d7ae

Please sign in to comment.