Skip to content

Commit

Permalink
Merge pull request #9 from ctidigital/attribute-store-labels
Browse files Browse the repository at this point in the history
Add store labels functionality to attributes
  • Loading branch information
chevli committed Apr 11, 2016
2 parents 795e986 + fbd5534 commit 3fe62c1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ We can also specify product options for select type attributes as so:
- Orange
```

We can also specify store labels for attributes as so:
```
- attributes:
- attribute_code:
frontend_label: Attribute Code
frontend_input: select
- store_labels:
default: Default Store Label
en: Label for store code en
```

Please note, certain attribute configurations follow certain rules so do ensure you're familiar with how Magento product attributes work in order to make best use of this component. An attribute's configuration elements are simply fields in the `catalog_eav_attribute` table with a few exceptions.

### Attribute Sets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ private function _createOrUpdateAttribute($code,$data) {
}

foreach ($data as $key=>$value) {
// Skip store labels to be processed after
if ($key == 'store_labels') {
continue;
}

// YAML will pass product types as an array which needs to be imploded
if ($key == "product_types") {
Expand Down Expand Up @@ -85,6 +89,9 @@ private function _createOrUpdateAttribute($code,$data) {
if ($data['options']) {
$this->_maintainAttributeOptions($attribute,$data['options']);
}
if ($data['store_labels']) {
$this->_maintainAttributeStoreLabels($attribute, $data['store_labels']);
}
} catch (Exception $e) {
throw $e;
}
Expand Down Expand Up @@ -183,6 +190,34 @@ private function _maintainAttributeOptions($attribute,$options) {
}


/**
* Assign store labels to an attribute
*
* @param $attribute
* @param $labels
*/
private function _maintainAttributeStoreLabels($attribute, $labels)
{
//get existing labels
$storeLabels = Mage::getModel('eav/entity_attribute')->getResource()
->getStoreLabelsByAttributeId($attribute->getId());

//build up array of storeId - Label
foreach ($labels as $storeCode => $label) {
try{
$store = Mage::app()->getStore($storeCode);
$storeId = $store->getId();
$storeLabels[$storeId] = $label;
} catch (Exception $e){
$this->log($this->__('Skipping label for store "' . $storeCode . '" - Check the store is setup '));
}
}

//save the new store labels
$attribute->setStoreLabels($storeLabels)->save();

}

/**
* @return array
*/
Expand Down

0 comments on commit 3fe62c1

Please sign in to comment.