Skip to content

Commit

Permalink
Implemented store based attribute labels (refs danslo#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Opitz committed Oct 17, 2017
1 parent 2c823b7 commit 3009ff5
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion code/Model/Import/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Danslo_ApiImport_Model_Import_Api
/**
* Cached import model.
*
* @var Mage_ApiImport_Model_Import
* @var Danslo_ApiImport_Model_Import
*/
protected $_api;

Expand All @@ -36,6 +36,11 @@ class Danslo_ApiImport_Model_Import_Api
*/
protected $_catalogProductEntityTypeId;

/**
* @var array
*/
protected $_storeCodeToId;

/**
* Sets up the import model and loads area parts.
*
Expand Down Expand Up @@ -91,6 +96,8 @@ public function importAttributes(array $data, $behavior = null)
$behavior = Mage_ImportExport_Model_Import::BEHAVIOR_APPEND;
}
$this->_init();
/** @var Mage_Eav_Model_Config $config */
$config = Mage::getSingleton('eav/config');

if (Danslo_ApiImport_Model_Import::BEHAVIOR_DELETE_IF_NOT_EXIST === $behavior) {
$this->_pruneAttributes($data);
Expand All @@ -102,7 +109,25 @@ public function importAttributes(array $data, $behavior = null)

if (Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE === $behavior
|| Mage_ImportExport_Model_Import::BEHAVIOR_APPEND === $behavior) {
$labels = array();
foreach ($this->_storeCodeToId as $storeCode => $storeId) {
$key = 'label-' . $storeCode;
if (array_key_exists($key, $attribute)) {
$labels[$storeId] = $attribute[$key];
unset($attribute[$key]);
}
}
$this->_setup->addAttribute($this->_catalogProductEntityTypeId, $attributeCode, $attribute);
if ($labels) {
try {
$attributeId = $this->_setup->getAttributeId($this->_catalogProductEntityTypeId, $attributeCode);
$config->getAttribute($this->_catalogProductEntityTypeId, $attributeId)
->setData('store_labels', $labels)
->save();
} catch (Exception $exception) {
$this->_api->addLogComment("Could not update labels for " . $attributeCode);
}
}
} elseif (Mage_ImportExport_Model_Import::BEHAVIOR_DELETE === $behavior) {
$this->_setup->removeAttribute($this->_catalogProductEntityTypeId, $attributeCode);
}
Expand Down Expand Up @@ -176,6 +201,11 @@ protected function _init()
{
$this->_setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('catalog_product_attribute_set');
$this->_catalogProductEntityTypeId = $this->_setup->getEntityTypeId(Mage_Catalog_Model_Product::ENTITY);

foreach (Mage::app()->getStores() as $store) {
/** @var Mage_Core_Model_Store $store */
$this->_storeCodeToId[$store->getCode()] = $store->getId();
}
}

/**
Expand Down

0 comments on commit 3009ff5

Please sign in to comment.