Skip to content

Commit

Permalink
Merge pull request #537 from mailchimp/inherit-image-issue513
Browse files Browse the repository at this point in the history
Send parent image when child has no image and belongs to a configurable
  • Loading branch information
Santiagoebizmarts authored Oct 31, 2017
2 parents ac666f5 + 99745dd commit 8e72142
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 79 deletions.
106 changes: 80 additions & 26 deletions app/code/community/Ebizmarts/MailChimp/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Ebizmarts_MailChimp_Helper_Data extends Mage_Core_Helper_Abstract
public function getConfigValueForScope($path, $scopeId, $scope = null)
{
if ($scope == 'websites') {
$configValue = Mage::app()->getWebsite($scopeId)->getConfig($path);
$configValue = $this->getMageApp()->getWebsite($scopeId)->getConfig($path);
} else {
$configValue = Mage::getStoreConfig($path, $scopeId);
}
Expand All @@ -48,7 +48,7 @@ public function getConfigScopeId($storeId = null, $websiteId = null)
} elseif ($code = Mage::getSingleton('adminhtml/config_data')->getWebsite()) {
// website level
$websiteId = Mage::getModel('core/website')->load($code)->getId();
$storeId = Mage::app()->getWebsite($websiteId)->getDefaultStore()->getId();
$storeId = $this->getMageApp()->getWebsite($websiteId)->getDefaultStore()->getId();
}
$scopeArray['websiteId'] = $websiteId;
$scopeArray['storeId'] = $storeId;
Expand Down Expand Up @@ -86,15 +86,15 @@ public function getMCStoreName($scopeId, $scope)
$storeName = null;
switch ($scope) {
case 'stores':
$store = Mage::app()->getStore($scopeId);
$store = $this->getMageApp()->getStore($scopeId);
$storeName = $store->getFrontendName();
break;
case 'websites':
$website = Mage::app()->getWebsite($scopeId);
$website = $this->getMageApp()->getWebsite($scopeId);
$storeName = $website->getDefaultStore()->getFrontendName();
break;
case 'default':
$storeView = Mage::app()->getDefaultStoreView();
$storeView = $this->getMageApp()->getDefaultStoreView();
$storeName = $storeView->getWebsite()->getDefaultStore()->getFrontendName();
break;
}
Expand All @@ -120,7 +120,7 @@ public function getStoreDomain($scopeId, $scope)
*/
public function getStoreRelation()
{
$stores = Mage::app()->getStores();
$stores = $this->getMageApp()->getStores();
$storeRelation = array();
foreach ($stores as $storeId => $store) {
$mcStoreId = $this->getMCStoreId($storeId);
Expand Down Expand Up @@ -1003,12 +1003,12 @@ public function getDefaultStoreIdForMailChimpScope($magentoStoreId)
$scopeArray = $this->getMailChimpScopeByStoreId($magentoStoreId);
if ($scopeArray) {
if ($scopeArray['scope'] == 'websites') {
$magentoStoreId = Mage::app()
$magentoStoreId = $this->getMageApp()
->getWebsite($scopeArray['scope_id'])
->getDefaultGroup()
->getDefaultStoreId();
} elseif ($scopeArray['scope'] == 'default') {
$magentoStoreId = Mage::app()
$magentoStoreId = $this->getMageApp()
->getWebsite(true)
->getDefaultGroup()
->getDefaultStoreId();
Expand Down Expand Up @@ -1056,7 +1056,8 @@ public function getMailChimpProductImageUrl($parentImageUrl, $productImageUrl)
*/
public function getImageUrlById($productId, $magentoStoreId)
{
$model = Mage::getResourceModel('catalog/product');
$productResourceModel = $this->getProductResourceModel();
$productModel = $this->getProductModel();
$configImageSize = $this->getImageSize($magentoStoreId);
switch ($configImageSize) {
case 0:
Expand All @@ -1073,27 +1074,80 @@ public function getImageUrlById($productId, $magentoStoreId)
break;
}

$productImage = $model->getAttributeRawValue($productId, $imageSize, $magentoStoreId);
$productImage = $productResourceModel->getAttributeRawValue($productId, $imageSize, $magentoStoreId);
$productModel->setData('image', $productImage);

if ($productImage == 'no_selection' || $productImage == null) {
$imageUrl = null;
} else {
$curStore = Mage::app()->getStore()->getId();
Mage::app()->setCurrentStore($magentoStoreId);
$productImageModel = $this->getProductImageModel();
$productImageModel->setDestinationSubdir($imageSize);
$productImageModel->setBaseFile($productImage);
$imageUrl = $productImageModel->getUrl();
Mage::app()->setCurrentStore($curStore);
$curStore = $this->getCurrentStoreId();
$this->setCurrentStore($magentoStoreId);
$upperCaseImage = str_replace('_', '', ucwords($imageSize, "_"));
$functionName = "get{$upperCaseImage}Url";
$imageUrl = $productModel->$functionName();
$this->setCurrentStore($curStore);
}
return $imageUrl;
}

/**
* Return Catalog Product Image helper instance
*
* @return Mage_Catalog_Helper_Image
*/
protected function _getImageHelper()
{
return Mage::helper('catalog/image');
}

public function getImageSize($scopeId, $scope = null)
{
return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_IMAGE_SIZE, $scopeId, $scope);
}

/**
* @return Object
*/
protected function getProductResourceModel()
{
$productResourceModel = Mage::getResourceModel('catalog/product');
return $productResourceModel;
}

/**
* @return false|Mage_Core_Model_Abstract
*/
protected function getProductModel()
{
$productModel = Mage::getModel('catalog/product');
return $productModel;
}

/**
* @return Mage_Core_Model_App
*/
protected function getMageApp()
{
return Mage::app();
}

/**
* @return int
*/
protected function getCurrentStoreId()
{
$curStore = $this->getMageApp()->getStore()->getId();
return $curStore;
}

/**
* @param $magentoStoreId
*/
protected function setCurrentStore($magentoStoreId)
{
$this->getMageApp()->setCurrentStore($magentoStoreId);
}

private function getProductImageModel()
{
return Mage::getModel('catalog/product_image');
Expand Down Expand Up @@ -1172,7 +1226,7 @@ public function getMCJs()
{
$script = '';
$url = null;
$storeId = Mage::app()->getStore()->getId();
$storeId = $this->getMageApp()->getStore()->getId();
if ($this->isEcomSyncDataEnabled($storeId)) {
$currentUrl = $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::ECOMMERCE_MC_JS_URL, $storeId);
if ($this->areJsUrlAndListScopesEqual($storeId)) {
Expand Down Expand Up @@ -1350,7 +1404,7 @@ protected function _migrateProductsFrom115($mailchimpStoreId, $initialTime)
$productCollection, $mailchimpStoreId, $initialTime, function ($product, $mailchimpStoreId) {
$productId = $product->getEntityId();
$_resource = Mage::getResourceSingleton('catalog/product');
$syncDelta = $_resource->getAttributeRawValue($productId, 'mailchimp_sync_delta', Mage::app()->getStore());
$syncDelta = $_resource->getAttributeRawValue($productId, 'mailchimp_sync_delta', $this->getMageApp()->getStore());
$syncError = null;
$syncModified = null;
if ($product->getMailchimpSyncError()) {
Expand Down Expand Up @@ -1529,7 +1583,7 @@ protected function _makeForCollectionItem($collection, $mailchimpStoreId, $initi
*/
public function timePassed($initialTime)
{
$storeCount = count(Mage::app()->getStores());
$storeCount = count($this->getMageApp()->getStores());
$timePassed = false;
$finalTime = time();
$difference = $finalTime - $initialTime;
Expand Down Expand Up @@ -1565,7 +1619,7 @@ protected function _migrateFrom116($initialTime)
*/
protected function _setIsSyncingIfFinishedInAllStores($syncValue)
{
$stores = Mage::app()->getStores();
$stores = $this->getMageApp()->getStores();
foreach ($stores as $storeId => $store) {
$this->setIsSyncingIfFinishedPerScope($syncValue, $storeId);
}
Expand All @@ -1583,7 +1637,7 @@ protected function _migrateOrdersFrom116($initialTime)
$finished = false;
if (!$this->timePassed($initialTime)) {
$finished = true;
$stores = Mage::app()->getStores();
$stores = $this->getMageApp()->getStores();
foreach ($stores as $storeId => $store) {
Mage::getModel('mailchimp/api_batches')->replaceAllOrders($initialTime, $storeId);
if ($this->timePassed($initialTime)) {
Expand Down Expand Up @@ -1619,7 +1673,7 @@ public function migrationFinished()
*/
public function delete116MigrationConfigData()
{
$stores = Mage::app()->getStores();
$stores = $this->getMageApp()->getStores();
$this->getConfig()->deleteConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, 'default', 0);
foreach ($stores as $storeId => $store) {
$this->getConfig()->deleteConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_LAST_ORDER_ID, 'stores', $storeId);
Expand Down Expand Up @@ -1654,7 +1708,7 @@ public function getMagentoStoreIdsByListId($listId)
{
$storeIds = Mage::registry('mailchimp_store_ids_for_list_' . $listId);
if ($storeIds === null) {
$stores = Mage::app()->getStores();
$stores = $this->getMageApp()->getStores();
$storeIds = array();
foreach ($stores as $storeId => $store) {
if ($this->isMailChimpEnabled($storeId)) {
Expand Down Expand Up @@ -1839,7 +1893,7 @@ protected function createNewWebhook($scopeId, $scope, $listId)

protected function getWebhookUrl()
{
$store = Mage::app()->getDefaultStoreView();
$store = $this->getMageApp()->getDefaultStoreView();
$webhooksKey = $this->getWebhooksKey();
//Generating Webhooks URL
$url = Ebizmarts_MailChimp_Model_ProcessWebhook::WEBHOOKS_PATH;
Expand Down Expand Up @@ -2100,7 +2154,7 @@ protected function allResendItemsSent($scopeId, $scope = 'stores')
$storeIds = $website->getStoreIds();
$allItemsSent = $this->allResendItemsSentPerScope($storeIds);
} else {
$stores = Mage::app()->getStores();
$stores = $this->getMageApp()->getStores();
$allItemsSent = $this->allResendItemsSentPerScope($stores);
}
}
Expand Down
8 changes: 5 additions & 3 deletions app/code/community/Ebizmarts/MailChimp/Model/Api/Batches.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ public function handleEcommerceBatches()
$helper->handleResendDataBefore();
foreach ($stores as $store) {
$storeId = $store->getId();
$this->_getResults($storeId);
$this->_sendEcommerceBatch($storeId);
if ($helper->isEcomSyncDataEnabled($storeId)) {
$this->_getResults($storeId);
$this->_sendEcommerceBatch($storeId);
}
}
$helper->handleResendDataAfter();

Expand Down Expand Up @@ -646,7 +648,7 @@ protected function handleSyncingValue($syncedDateArray)
if ($date) {
$api = $helper->getApi($magentoStoreId);
$isSyncingDate = $helper->getDateSyncFinishByMailChimpStoreId($mailchimpStoreId);
if (!$isSyncingDate) {
if (!$isSyncingDate && $mailchimpStoreId) {
Mage::getModel('mailchimp/api_stores')->editIsSyncing($api, false, $mailchimpStoreId);
$scopeToEdit = Mage::helper('mailchimp')->getMailChimpScopeByStoreId($magentoStoreId);
if ($scopeToEdit['scope'] != 'stores') {
Expand Down
Loading

0 comments on commit 8e72142

Please sign in to comment.