Skip to content

Commit

Permalink
Merge pull request #9 from Agence-DnD/release/100.1.2
Browse files Browse the repository at this point in the history
Release/100.1.2
  • Loading branch information
Dnd-Gimix authored Feb 21, 2019
2 parents 047f448 + ece3a51 commit c22e77e
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 71 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
Fix price set to 0 if attribute price is empty

Fix media import with wrong entity_type_id

### 100.1.2 :
Force attribute code to be lowercase

Manage attribute scope on import
2 changes: 2 additions & 0 deletions app/code/community/Pimgento/Api/Helper/Product.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ private function formatAssociations(stdClass $associations, array $values)
*/
private function getKey($attribute, array $specifics)
{
/** @var string $attribute */
$attribute = strtolower($attribute);
if (isset($specifics['locale']) && isset($specifics['scope'])) {
return sprintf('%s-%s-%s', $attribute, $specifics['locale'], $specifics['scope']);
}
Expand Down
101 changes: 55 additions & 46 deletions app/code/community/Pimgento/Api/Helper/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function getStores($arrayKey = 'store_id')

/** @var mixed[] $data */
$data = [];
/** @var string[] $websiteDefaultStores */
$websiteDefaultStores = $this->getWebsiteDefaultStores(true);
/** @var Pimgento_Api_Helper_Configuration $configHelper */
$configHelper = $this->getConfigurationHelper();
/** @var mixed[] $mapping */
Expand All @@ -49,20 +51,36 @@ public function getStores($arrayKey = 'store_id')
continue;
}

/** @var string $currency */
$currency = $website->getBaseCurrencyCode();
/** @var string[] $siblings */
$siblings = $website->getStoreIds();
/** @var Mage_Core_Model_Store[] $store */
$stores = $website->getStores();
/** @var Mage_Core_Model_Store $store */
foreach ($stores as $store) {
/** @var int $storeId */
$storeId = $store->getId();
/** @var string $storeCode */
$storeCode = $store->getCode();
/** @var string $storeLang */
$storeLang = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $storeId);
/** @var bool $isDefault */
$isDefault = false;
if (in_array($storeId, $websiteDefaultStores)) {
$isDefault = true;
}

/** @var mixed[] $combine */
$combine = [];
/** @var string $key */
foreach ($arrayKey as $key) {
switch ($key) {
case 'store_id':
$combine[] = $store->getId();
$combine[] = $storeId;
break;
case 'store_code':
$combine[] = $store->getCode();
$combine[] = $storeCode;
break;
case 'website_id':
$combine[] = $websiteId;
Expand All @@ -74,16 +92,13 @@ public function getStores($arrayKey = 'store_id')
$combine[] = $channel;
break;
case 'lang':
$combine[] = Mage::getStoreConfig(
Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE,
$store->getId()
);
$combine[] = $storeLang;
break;
case 'currency':
$combine[] = $store->getDefaultCurrencyCode();
$combine[] = $currency;
break;
default:
$combine[] = $store->getId();
$combine[] = $storeId;
break;
}
}
Expand All @@ -92,16 +107,15 @@ public function getStores($arrayKey = 'store_id')
$key = implode('-', $combine);

$data[$key][] = [
'store_id' => $store->getId(),
'store_code' => $store->getCode(),
'website_id' => $websiteId,
'website_code' => $websiteCode,
'channel_code' => $channel,
'lang' => Mage::getStoreConfig(
Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE,
$store->getId()
),
'currency' => $store->getDefaultCurrencyCode(),
'store_id' => $storeId,
'store_code' => $storeCode,
'is_website_default' => $isDefault,
'siblings' => $siblings,
'website_id' => $websiteId,
'website_code' => $websiteCode,
'channel_code' => $channel,
'lang' => $storeLang,
'currency' => $currency,
];
}
}
Expand Down Expand Up @@ -130,30 +144,6 @@ public function getAllStores()
return $stores;
}

/**
* Retrieve all stores from website/channel mapping
*
* @return mixed[]
* @throws Exception
*/
public function getMappedWebsitesStores()
{
/** @var string[] $mapping */
$mapping = $this->getConfigurationHelper()->getWebsiteMapping();
if (empty($mapping)) {
return [];
}

/** @var mixed[] $stores */
$stores = $this->getStores('website_code');
/** @var mixed[] $mappedWebsites */
$mappedWebsites = array_column($mapping, 'website');
$mappedWebsites = array_flip($mappedWebsites);
$mappedWebsites = array_intersect_key($stores, $mappedWebsites);

return $mappedWebsites;
}

/**
* Retrieve needed store ids from website/channel mapping
*
Expand All @@ -162,8 +152,8 @@ public function getMappedWebsitesStores()
*/
public function getMappedWebsitesStoreIds()
{
/** @var string[] $websites */
$websites = $this->getMappedWebsitesStores();
/** @var mixed[] $websites */
$websites = $this->getStores('website_code');
/** @var string[] $storeIds */
$storeIds = [];
/** @var mixed[] $website */
Expand All @@ -184,8 +174,8 @@ public function getMappedWebsitesStoreIds()
*/
public function getMappedWebsitesStoreLangs()
{
/** @var string[] $websites */
$websites = $this->getMappedWebsitesStores();
/** @var mixed[] $websites */
$websites = $this->getStores('website_code');
/** @var string[] $langs */
$langs = [];
/** @var mixed[] $website */
Expand All @@ -198,6 +188,25 @@ public function getMappedWebsitesStoreLangs()
return $langs;
}

/**
* Get websites default stores
*
* @param bool $withAdmin
*
* @return string[]
*/
public function getWebsiteDefaultStores($withAdmin = false)
{
/** @var Mage_Core_Model_Resource_Website $websiteResource */
$websiteResource = Mage::getResourceModel('core/website');
/** @var Varien_Db_Select $select */
$select = $websiteResource->getDefaultStoresSelect($withAdmin);
/** @var string[] $websiteDefaultStores */
$websiteDefaultStores = $websiteResource->getReadConnection()->fetchPairs($select);

return $websiteDefaultStores;
}

/**
* Retrieve admin store lang setting
* Default: return Mage_Core_Model_Locale::DEFAULT_LOCALE
Expand Down
6 changes: 3 additions & 3 deletions app/code/community/Pimgento/Api/Model/Job/Attribute.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function insertData($task)
*/
foreach ($attributes as $index => $attribute) {
/** @var string $attributeCode */
$attributeCode = $attribute['code'];
$attributeCode = strtolower($attribute['code']);
/** @var int $id */
if ($attributeHelper->isAttributeCodeReserved($attributeCode)) {
if (!$isPrefixEnabled) {
Expand All @@ -107,9 +107,9 @@ public function insertData($task)

continue;
}

$attribute['code'] = Pimgento_Api_Helper_Attribute::RESERVED_ATTRIBUTE_CODE_PREFIX . $attributeCode;
$attributeCode = Pimgento_Api_Helper_Attribute::RESERVED_ATTRIBUTE_CODE_PREFIX . $attributeCode;
}
$attribute['code'] = $attributeCode;
/** @var string[] $columns */
$columns = $entitiesHelper->getColumnsFromResult($attribute);
/** @var bool $result */
Expand Down
90 changes: 68 additions & 22 deletions app/code/community/Pimgento/Api/Model/Job/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class Pimgento_Api_Model_Job_Product extends Pimgento_Api_Model_Job_Abstract
'groups',
'parent',
'enabled',
'created',
'updated',
'associations',
'PACK',
'SUBSTITUTION',
'UPSELL',
'X_SELL',
];

/**
Expand Down Expand Up @@ -581,10 +588,10 @@ public function updateOption($task)
continue;
}

/** @var string[] $columnPrefix */
$columnPrefix = explode('-', $column);
/** @var string[] $columnParts */
$columnParts = explode('-', $column, 2);
/** @var string $columnPrefix */
$columnPrefix = reset($columnPrefix);
$columnPrefix = reset($columnParts);
$columnPrefix = sprintf('%s_', $columnPrefix);
/** @var int $prefixLength */
$prefixLength = strlen($columnPrefix) + 1;
Expand Down Expand Up @@ -693,12 +700,16 @@ public function setValues($task)
$connection = $resourceEntities->getReadConnection();
/** @var string $tmpTable */
$tmpTable = $resourceEntities->getTableName();
/** @var string[] $attributeScopeMapping */
$attributeScopeMapping = $resourceEntities->getAttributeScopeMapping();
/** @var Pimgento_Api_Helper_Store $storeHelper */
$storeHelper = Mage::helper('pimgento_api/store');
/** @var mixed[] $stores */
$stores = $storeHelper->getAllStores();
/** @var string[] $columns */
$columns = array_keys($connection->describeTable($tmpTable));
/** @var string $adminBaseCurrency */
$adminBaseCurrency = Mage::app()->getBaseCurrencyCode();
/** @var mixed[] $values */
$values = [
0 => [
Expand All @@ -714,35 +725,70 @@ public function setValues($task)

/** @var string $column */
foreach ($columns as $column) {
if (in_array($column, $this->getExcludedColumns()) || preg_match('/-unit/', $column)) {
/** @var string[] $columnParts */
$columnParts = explode('-', $column, 2);
/** @var string $columnPrefix */
$columnPrefix = $columnParts[0];

if (in_array($columnPrefix, $this->getExcludedColumns()) || preg_match('/-unit/', $column)) {
continue;
}

/** @var string[] $columnPrefix */
$columnPrefix = explode('-', $column);
/** @var string $columnPrefix */
$columnPrefix = reset($columnPrefix);
if (!isset($attributeScopeMapping[$columnPrefix])) {
// If no scope is found, attribute does not exist
$task->setStepWarning($this->getHelper()->__('Attribute %s was not found. Please try re-importing attributes.', $columnPrefix));

/**
* @var string $suffix
* @var mixed[] $affected
*/
foreach ($stores as $suffix => $affected) {
if (!preg_match(sprintf('/^%s-%s$/', $columnPrefix, $suffix), $column)) {
continue;
}

if (empty($columnParts[1]) && !isset($values[0][$columnPrefix])) {
// No channel and no locale found: attribute scope naturally is Global
$values[0][$columnPrefix] = $column;

continue;
}

/** @var int $scope */
$scope = (int)$attributeScopeMapping[$columnPrefix];
if ($scope === Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL && !empty($columnParts[1]) && $columnParts[1] === $adminBaseCurrency) {
// This attribute has global scope with a suffix: it is a price with its currency
// Only set this Price value if currency matches default Magento currency
// If Price scope is set to Website, it will be processed afterwards as any website scoped attribute
$values[0][$columnPrefix] = $column;

continue;
}

/** @var string $columnSuffix */
$columnSuffix = $columnParts[1];
if (!isset($stores[$columnSuffix])) {
// No corresponding store found for this suffix
$task->setStepWarning($this->getHelper()->__('Column %s was ignored and passed.', $column));

continue;
}

/** @var mixed[] $affectedStores */
$affectedStores = $stores[$columnSuffix];
/** @var mixed[] $store */
foreach ($affectedStores as $store) {
// Handle website scope
if ($scope === Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE && !$store['is_website_default']) {
continue;
}

/** @var mixed[] $store */
foreach ($affected as $store) {
if (!isset($values[$store['store_id']])) {
$values[$store['store_id']] = [];
}
if ($scope === Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE || empty($store['siblings'])) {
$values[$store['store_id']][$columnPrefix] = $column;

continue;
}
}

if (!isset($values[0][$columnPrefix])) {
$values[0][$columnPrefix] = $column;
/** @var string[] $siblings */
$siblings = $store['siblings'];
/** @var string $storeId */
foreach ($siblings as $storeId) {
$values[$storeId][$columnPrefix] = $column;
}
}
}

Expand Down
Loading

0 comments on commit c22e77e

Please sign in to comment.