Skip to content

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiagoebizmarts authored Oct 18, 2017
2 parents bdef5d0 + 31f6b4b commit b518173
Show file tree
Hide file tree
Showing 17 changed files with 431 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ class Ebizmarts_MailChimp_Block_Adminhtml_System_Config_Account extends Mage_Adm
{
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$firstErrorKey = Ebizmarts_MailChimp_Model_System_Config_Source_Account::NO_STORE_TEXT_KEY;
$firstMigrationKey = Ebizmarts_MailChimp_Model_System_Config_Source_Account::STORE_MIGRATION_TEXT_KEY;
$syncLabelKey = Ebizmarts_MailChimp_Model_System_Config_Source_Account::SYNC_LABEL_KEY;
$values = $element->getValues();

$html = '<ul class="checkboxes">';

foreach ($values as $dat) {
if ($dat['value'] > 7 && $dat['value'] < 10) {
if ($dat['value'] >= $firstErrorKey && $dat['value'] < $firstMigrationKey) {
$html .= "<li style='color:red;font-weight: bold;'>{$dat['label']}</li>";
} elseif ($dat['value'] == 10) {
} elseif ($dat['value'] == $firstMigrationKey) {
$html .= "<li style='color:forestgreen;font-weight: bold;'>{$dat['label']}</li>";
} elseif($dat['value'] == 3) {
} elseif($dat['value'] == $syncLabelKey) {
$textArray = explode(':', $dat['label']);
if (strstr($textArray[1], $this->__('Finished'))) {
$html .= "<li>{$textArray[0]} : <span style='color:forestgreen;font-weight: bold;'>{$textArray[1]}</span></li>";
Expand Down
136 changes: 124 additions & 12 deletions app/code/community/Ebizmarts/MailChimp/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1029,29 +1029,42 @@ public function getMailChimpProductImageUrl($parentImageUrl, $productImageUrl)
* Returns product image url by id, if it does not have one returns null.
*
* @param $productId
* @param $magentoStoreId
* @return null
*/
public function getImageUrlById($productId)
public function getImageUrlById($productId, $magentoStoreId)
{
$productMediaConfig = $this->getProductMediaConfig();
$product = $this->loadProductById($productId);
$productImage = $product->getImage();
$model = Mage::getResourceModel('catalog/product');
$configImageSize = $this->getImageSize($magentoStoreId);

if ($configImageSize == 0) {
$productImage = $model->getAttributeRawValue($productId, 'image', $magentoStoreId);
} else if ($configImageSize == 1){
$productImage = $model->getAttributeRawValue($productId, 'small_image', $magentoStoreId);
} else {
$productImage = $model->getAttributeRawValue($productId, 'thumbnail', $magentoStoreId);
}

if ($productImage == 'no_selection' || $productImage == null) {
$imageUrl = null;
} else {
$oldStoreId = Mage::app()->getStore()->getId();
Mage::app()->setCurrentStore($magentoStoreId);
$productMediaConfig = $this->getProductMediaConfig();
$imageUrl = $productMediaConfig->getMediaUrl($productImage);
Mage::app()->setCurrentStore($oldStoreId);
}
return $imageUrl;
}

private function getProductMediaConfig()
public function getImageSize($scopeId, $scope = null)
{
return Mage::getModel('catalog/product_media_config');
return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_IMAGE_SIZE, $scopeId, $scope);
}

private function loadProductById($productId)
private function getProductMediaConfig()
{
return Mage::getModel('catalog/product')->load($productId);
return Mage::getModel('catalog/product_media_config');
}

/**
Expand Down Expand Up @@ -1126,9 +1139,14 @@ protected function getOrderCollectionByCustomerEmail($subscriberEmail)
public function getMCJs()
{
$script = '';
$url = null;
$storeId = Mage::app()->getStore()->getId();
if ($this->isEcomSyncDataEnabled($storeId)) {
$url = $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $storeId);
$currentUrl = $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $storeId);
if ($this->areJsUrlAndListScopesEqual($storeId)) {
$url = $currentUrl;
}

if (!$url) {
$url = $this->getApiStores()->getMCJsUrl($storeId, 'stores');
}
Expand Down Expand Up @@ -1651,6 +1669,7 @@ public function loadListSubscriber($listId, $email)
$customer = $this->loadListCustomer($listId, $email);
if ($customer) {
$subscriber->setStoreId($customer->getStoreId());
$subscriber->setCustomerId($customer->getId());
} else {
/**
* No customer with that address. Just assume the first
Expand Down Expand Up @@ -1822,7 +1841,7 @@ public function getRealScopeForConfig($path, $scopeId, $scope = 'stores')
$scopeSoFar = null;
foreach ($configCollection as $config) {
//Discard possible extra website or store
if ($this->isExtraEntry($config, $scope, $websiteId)) {
if ($this->isExtraEntry($config, $scope, $scopeId, $websiteId)) {
continue;
}
switch ($config->getScope()) {
Expand Down Expand Up @@ -1850,12 +1869,13 @@ public function getRealScopeForConfig($path, $scopeId, $scope = 'stores')
*
* @param $config
* @param $scope
* @param $scopeId
* @param $websiteId
* @return bool
*/
protected function isExtraEntry($config, $scope, $websiteId)
protected function isExtraEntry($config, $scope, $scopeId, $websiteId)
{
return $config->getScopeId() != 0 && (($config->getScope() == 'stores' && $scope != 'stores') || ($config->getScope() == 'websites' && $scope == 'stores' && $config->getScopeId() != $websiteId));
return $this->isNotDefaultScope($config) && ($this->isIncorrectScope($config, $scope) || $this->isDifferentWebsite($config, $scope, $websiteId) || $this->isDifferentStoreView($config, $scope, $scopeId));
}

public function updateSubscriberSyndData($itemId, $syncDelta = null, $syncError = null, $syncModified = 0, $syncDeleted = null)
Expand Down Expand Up @@ -2310,4 +2330,96 @@ public function getStorePhone($scopeId, $scope = 'stores')
{
return $this->getConfigValueForScope('general/store_information/phone', $scopeId, $scope);
}

public function getAllMailChimpStoreIds()
{
$collection = Mage::getResourceModel('core/config_data_collection')
->addFieldToFilter('path', array('eq' => Ebizmarts_MailChimp_Model_Config::GENERAL_MCSTOREID));
$mailchimpStoreIdsArray = array();
foreach ($collection as $row) {
$scopeData = $row->getScope().'_'.$row->getScopeId();
$mailchimpStoreIdsArray[$scopeData] = $row->getValue();
}
return $mailchimpStoreIdsArray;
}

public function subscribeMember($subscriber, $forceUpdateStatus = false)
{
$subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->setSubscriberConfirmCode($subscriber->randomSequence());
if ($forceUpdateStatus) {
$subscriber->setMailchimpSyncModified(1);
}
$this->setMemberGeneralData($subscriber);
}

public function unsubscribeMember($subscriber)
{
$subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED);
$this->setMemberGeneralData($subscriber);
}

protected function setMemberGeneralData($subscriber)
{
$subscriber->setImportMode(true);
$subscriber->setSubscriberSource(Ebizmarts_MailChimp_Model_Subscriber::SUBSCRIBE_SOURCE);
$subscriber->setIsStatusChanged(true);
$subscriber->save();
}

/**
* @param $config
* @param $scope
* @param $scopeId
* @return bool
*/
protected function isDifferentStoreView($config, $scope, $scopeId)
{
return $config->getScope() == 'stores' && $scope == 'stores' && $scopeId != $config->getScopeId();
}

/**
* @param $config
* @param $scope
* @param $websiteId
* @return bool
*/
protected function isDifferentWebsite($config, $scope, $websiteId)
{
return ($config->getScope() == 'websites' && $scope == 'stores' && $config->getScopeId() != $websiteId);
}

/**
* @param $config
* @param $scope
* @return bool
*/
protected function isIncorrectScope($config, $scope)
{
return ($config->getScope() == 'stores' && $scope != 'stores');
}

/**
* @param $config
* @return bool
*/
protected function isNotDefaultScope($config)
{
return $config->getScopeId() != 0;
}

/**
* @param $storeId
* @return bool
*/
protected function areJsUrlAndListScopesEqual($storeId)
{
$scopesMatch = false;
$realScopeList = Mage::helper('mailchimp')->getRealScopeForConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, $storeId);
$realScopeJs = Mage::helper('mailchimp')->getRealScopeForConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $storeId);
if ($realScopeList && $realScopeJs && $realScopeList['scope'] == $realScopeJs['scope'] && $realScopeList['scope_id'] == $realScopeJs['scope_id']) {
$scopesMatch = true;
}
return $scopesMatch;
}
}
4 changes: 2 additions & 2 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,14 @@ protected function GeneratePOSTPayload($order, $mailchimpStoreId, $magentoStoreI
)
)
->addAttributeToFilter('customer_email', array('eq' => $order->getCustomerEmail()));
$totalOrders = 1;
$totalOrders = 0;
$totalAmountSpent = (int)$order->getGrandTotal();
foreach ($orderCollection as $customerOrder) {
$totalOrders++;
$totalAmountSpent += ($customerOrder->getGrandTotal() - $customerOrder->getTotalRefunded() - $customerOrder->getTotalCanceled());
}

$data["customer"]["orders_count"] = $totalOrders;
$data["customer"]["orders_count"] = (int)$totalOrders;
$data["customer"]["total_spent"] = $totalAmountSpent;
$jsonData = "";
//enconde to JSON
Expand Down
48 changes: 34 additions & 14 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected function _buildProductData($product, $magentoStoreId, $isVariant = tru
$data["url"] = $url;

//image
$imageUrl = $this->getMailChimpHelper()->getMailChimpProductImageUrl($this->_parentImageUrl, $this->getMailChimpHelper()->getImageUrlById($product->getId()));
$imageUrl = $this->getMailChimpHelper()->getMailChimpProductImageUrl($this->_parentImageUrl, $this->getMailChimpHelper()->getImageUrlById($product->getId(), $magentoStoreId));
if ($imageUrl) {
$data["image_url"] = $imageUrl;
}
Expand All @@ -236,11 +236,10 @@ protected function _buildProductData($product, $magentoStoreId, $isVariant = tru
$data["description"] = ($product->getDescription()) ? $product->getDescription() : $product->getDefaultDescription();
}

//mailchimp product type (magento category)
$categoryId = $product->getCategoryId();
if ($categoryId) {
$category = Mage::getResourceModel('catalog/category')->checkId($categoryId);
$data["type"] = $category->getName();
//mailchimp product type and vendor (magento category)
$categoryName = $this->getProductCategories($product, $magentoStoreId);
if ($categoryName) {
$data["type"] = $categoryName;
$data["vendor"] = $data["type"];
}

Expand All @@ -257,7 +256,7 @@ protected function _buildProductData($product, $magentoStoreId, $isVariant = tru
if ($visibility != Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE) {
$this->_parentUrl = $data['url'];
}
$price = (float)$product->getDefaultPrice();
$price = ((float)$product->getPrice()) ? (float)$product->getPrice() : (float)$product->getDefaultPrice();
if ($price) {
$this->_parentPrice = $price;
}
Expand Down Expand Up @@ -522,13 +521,11 @@ public function joinProductAttributes($collection, $magentoStoreId)
foreach ($attributeCodes as $_code) {
$attributeName = $config->getAttribute("catalog_product", $_code);

if ($_code != 'price') {
$collection->joinField(
$_code, $attributeName->getBackendTable(), 'value', 'entity_id = entity_id',
'{{table}}.store_id = ' . $magentoStoreId . ' AND {{table}}.attribute_id = ' . $attributeName->getId(), 'left'
);
}

$collection->joinField(
$_code, $attributeName->getBackendTable(), 'value', 'entity_id = entity_id',
'{{table}}.store_id = ' . $magentoStoreId . ' AND {{table}}.attribute_id = ' . $attributeName->getId(), 'left'
);

$collection->joinField(
'default_' . $_code, $attributeName->getBackendTable(), 'value', 'entity_id = entity_id',
'{{table}}.store_id = 0 AND {{table}}.attribute_id = ' . $attributeName->getId(), 'left'
Expand Down Expand Up @@ -736,4 +733,27 @@ protected function getProductUrl($product, $magentoStoreId)
Mage::app()->setCurrentStore($oldStoreId);
return $url;
}

protected function getProductCategories($product, $magentoStoreId)
{
$categoryIds = $product->getCategoryIds();
$categoryNames = array();
$categoryName = null;
if (is_array($categoryIds) && count($categoryIds)) {
/* @var $collection Mage_Catalog_Model_Resource_Category_Collection */
$collection = Mage::getModel('catalog/category')->getCollection();
$collection->addAttributeToSelect(array('name'))
->setStoreId($magentoStoreId)
->addAttributeToFilter('is_active', array('eq'=>'1'))
->addAttributeToFilter('entity_id', array('in' => $categoryIds))
->addAttributeToSort('level', 'asc');

/* @var $category Mage_Catalog_Model_Category */
foreach ($collection as $category) {
$categoryNames[] = $category->getName();
}
$categoryName = (count($categoryNames)) ? implode(" - ", $categoryNames) : 'None';
}
return $categoryName;
}
}
15 changes: 13 additions & 2 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Stores.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ public function getMCJsUrl($scopeId, $scope)
try {
$api = Mage::helper('mailchimp')->getApi($scopeId, $scope);
$mailchimpStoreId = Mage::helper('mailchimp')->getMCStoreId($scopeId, $scope);
$response = $api->ecommerce->stores->get($mailchimpStoreId, 'connected_site');
$response = $this->getStoreConnectedSiteData($api, $mailchimpStoreId);
if (isset($response['connected_site']['site_script']['url'])) {
$url = $response['connected_site']['site_script']['url'];
$configValues = array(array(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $url));
$realScope = Mage::helper('mailchimp')->getRealScopeForConfig(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $scopeId, $scope);
$realScope = Mage::helper('mailchimp')->getRealScopeForConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, $scopeId, $scope);
Mage::helper('mailchimp')->saveMailchimpConfig($configValues, $realScope['scope_id'], $realScope['scope']);
return $url;
}
Expand All @@ -139,4 +139,15 @@ public function editIsSyncing($mailchimpApi, $isSincingValue, $mailchimpStoreId,
$configValue = array(array(Ebizmarts_MailChimp_Model_Config::GENERAL_MCISSYNCING, (int)$isSincingValue));
Mage::helper('mailchimp')->saveMailchimpConfig($configValue, $scopeToEdit['scope_id'], $scopeToEdit['scope']);
}

/**
* @param $api
* @param $mailchimpStoreId
* @return mixed
*/
protected function getStoreConnectedSiteData($api, $mailchimpStoreId)
{
$response = $api->ecommerce->stores->get($mailchimpStoreId, 'connected_site');
return $response;
}
}
Loading

0 comments on commit b518173

Please sign in to comment.