-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ctidigital/attributes
Attributes
- Loading branch information
Showing
10 changed files
with
480 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
|
||
namespace CtiDigital\Configurator\Model\Component; | ||
|
||
use CtiDigital\Configurator\Model\Exception\ComponentException; | ||
use CtiDigital\Configurator\Model\LoggingInterface; | ||
use Magento\Eav\Api\AttributeSetRepositoryInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Eav\Api\Data\AttributeSetInterface; | ||
use Magento\Eav\Setup\EavSetup; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\Eav\Model\AttributeSetRepository; | ||
|
||
/** | ||
* Class AttributeSets | ||
* @package CtiDigital\Configurator\Model\Component | ||
* @SuppressWarnings(PHPMD.LongVariable) | ||
*/ | ||
class AttributeSets extends YamlComponentAbstract | ||
{ | ||
protected $alias = 'attribute_sets'; | ||
protected $name = 'Attribute Sets'; | ||
protected $description = 'Component to create/maintain attribute sets.'; | ||
|
||
/** | ||
* @var EavSetup | ||
*/ | ||
protected $eavSetup; | ||
|
||
/** | ||
* @var AttributeSetRepository | ||
*/ | ||
protected $attributeSetRepository; | ||
|
||
/** | ||
* AttributeSets constructor. | ||
* @param LoggingInterface $log | ||
* @param ObjectManagerInterface $objectManager | ||
* @param EavSetup $eavSetup | ||
* @param AttributeSetRepositoryInterface $attributeSetRepository | ||
*/ | ||
public function __construct( | ||
LoggingInterface $log, | ||
ObjectManagerInterface $objectManager, | ||
EavSetup $eavSetup, | ||
AttributeSetRepositoryInterface $attributeSetRepository | ||
) { | ||
|
||
parent::__construct($log, $objectManager); | ||
|
||
$this->eavSetup = $eavSetup; | ||
$this->attributeSetRepository = $attributeSetRepository; | ||
} | ||
|
||
/** | ||
* @param array $attributeConfigurationData | ||
*/ | ||
protected function processData($attributeConfigurationData = null) | ||
{ | ||
try { | ||
foreach ($attributeConfigurationData['attribute_sets'] as $attributeSetConfiguration) { | ||
$this->processAttributeSet($attributeSetConfiguration); | ||
} | ||
} catch (ComponentException $e) { | ||
$this->log->logError($e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $attributeSetConfig | ||
*/ | ||
protected function processAttributeSet(array $attributeSetConfig) | ||
{ | ||
$this->eavSetup->addAttributeSet(Product::ENTITY, $attributeSetConfig['name']); | ||
|
||
$attributeSetId = $this->eavSetup->getAttributeSetId(Product::ENTITY, $attributeSetConfig['name']); | ||
$attributeSetEntity = $this->attributeSetRepository->get($attributeSetId); | ||
if (array_key_exists('inherit', $attributeSetConfig)) { | ||
$attributeSetEntity->initFromSkeleton($this->getAttributeSetId($attributeSetConfig['inherit'])); | ||
$this->attributeSetRepository->save($attributeSetEntity); | ||
} | ||
|
||
if (array_key_exists('groups', $attributeSetConfig) && count($attributeSetConfig['groups']) > 0) { | ||
$this->addAttributeGroups($attributeSetEntity, $attributeSetConfig['groups']); | ||
$this->addAttributeGroupAssociations($attributeSetEntity, $attributeSetConfig['groups']); | ||
} | ||
} | ||
|
||
/** | ||
* @param AttributeSetInterface $attributeSetEntity | ||
* @param array $attributeGroupData | ||
*/ | ||
protected function addAttributeGroups(AttributeSetInterface $attributeSetEntity, array $attributeGroupData) | ||
{ | ||
/* if ($attributeSetEntity->getDefaultGroupId()) { | ||
$this->eavSetup->removeAttributeGroup( | ||
Product::ENTITY, | ||
$attributeSetEntity->getId(), | ||
$attributeSetEntity->getDefaultGroupId() | ||
); | ||
}*/ | ||
|
||
foreach ($attributeGroupData as $group) { | ||
$attributeSetName = $attributeSetEntity->getAttributeSetName(); | ||
$this->eavSetup->addAttributeGroup(Product::ENTITY, $attributeSetName, $group['name']); | ||
} | ||
} | ||
|
||
/** | ||
* @param AttributeSetInterface $attributeSetEntity | ||
* @param array $attributeGroupData | ||
*/ | ||
protected function addAttributeGroupAssociations( | ||
AttributeSetInterface $attributeSetEntity, | ||
array $attributeGroupData | ||
) { | ||
foreach ($attributeGroupData as $group) { | ||
foreach ($group['attributes'] as $attributeCode) { | ||
$attributeData = $this->eavSetup->getAttribute(Product::ENTITY, $attributeCode); | ||
|
||
if (count($attributeData) === 0) { | ||
throw new ComponentException("Attribute '{$attributeCode}' does not exist."); | ||
} | ||
|
||
$this->eavSetup->addAttributeToGroup( | ||
Product::ENTITY, | ||
$attributeSetEntity->getId(), | ||
$group['name'], | ||
$attributeCode | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $attributeSetName | ||
* @return string | ||
*/ | ||
protected function getAttributeSetId($attributeSetName) | ||
{ | ||
$attributeSetData = $this->eavSetup->getAttributeSet(Product::ENTITY, $attributeSetName); | ||
if (array_key_exists('attribute_set_id', $attributeSetData)) { | ||
return $attributeSetData['attribute_set_id']; | ||
} | ||
|
||
throw new ComponentException('Could not find attribute set name.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
<?php | ||
|
||
namespace CtiDigital\Configurator\Model\Component; | ||
|
||
use CtiDigital\Configurator\Model\Exception\ComponentException; | ||
use CtiDigital\Configurator\Model\LoggingInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Eav\Setup\EavSetup; | ||
use Magento\Framework\ObjectManagerInterface; | ||
|
||
/** | ||
* Class Attributes | ||
* @package CtiDigital\Configurator\Model\Component | ||
* @SuppressWarnings(PHPMD.LongVariable) | ||
*/ | ||
class Attributes extends YamlComponentAbstract | ||
{ | ||
|
||
protected $alias = 'attributes'; | ||
protected $name = 'Attributes'; | ||
protected $description = 'Component to create/maintain attributes.'; | ||
|
||
/** | ||
* @var EavSetup | ||
*/ | ||
protected $eavSetup; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $cachedAttributeConfig; | ||
|
||
/** | ||
* @var Product\Attribute\Repository | ||
*/ | ||
protected $productAttributeRepository; | ||
|
||
public function __construct( | ||
LoggingInterface $log, | ||
ObjectManagerInterface $objectManager, | ||
EavSetup $eavSetup, | ||
Product\Attribute\Repository $repository | ||
) { | ||
parent::__construct($log, $objectManager); | ||
$this->eavSetup = $eavSetup; | ||
$this->productAttributeRepository = $repository; | ||
} | ||
|
||
/** | ||
* @param array $attributeConfigurationData | ||
*/ | ||
protected function processData($attributeConfigurationData = null) | ||
{ | ||
try { | ||
foreach ($attributeConfigurationData['attributes'] as $attributeCode => $attributeConfiguration) { | ||
$this->processAttribute($attributeCode, $attributeConfiguration); | ||
} | ||
} catch (ComponentException $e) { | ||
$this->log->logError($e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* @param $attributeCode | ||
* @param $attributeConfig | ||
*/ | ||
protected function processAttribute($attributeCode, array $attributeConfig) | ||
{ | ||
$updateAttribute = true; | ||
$attributeExists = false; | ||
$attributeArray = $this->eavSetup->getAttribute(Product::ENTITY, $attributeCode); | ||
if ($attributeArray && $attributeArray['attribute_id']) { | ||
$attributeExists = true; | ||
$this->log->logComment(sprintf('Attribute %s exists. Checking for updates.', $attributeCode)); | ||
$updateAttribute = $this->checkForAttributeUpdates($attributeCode, $attributeArray, $attributeConfig); | ||
|
||
if (isset($attributeConfig['option'])) { | ||
$newAttributeOptions = $this->manageAttributeOptions($attributeCode, $attributeConfig['option']); | ||
$attributeConfig['option']['values'] = $newAttributeOptions; | ||
} | ||
} | ||
|
||
if ($updateAttribute) { | ||
|
||
$attributeConfig['user_defined'] = 1; | ||
|
||
if (isset($attributeConfig['product_types'])) { | ||
$attributeConfig['apply_to'] = implode(',', $attributeConfig['product_types']); | ||
} | ||
|
||
$this->eavSetup->addAttribute( | ||
Product::ENTITY, | ||
$attributeCode, | ||
$attributeConfig | ||
); | ||
|
||
if ($attributeExists) { | ||
$this->log->logInfo(sprintf('Attribute %s updated.', $attributeCode)); | ||
return; | ||
} | ||
|
||
$this->log->logInfo(sprintf('Attribute %s created.', $attributeCode)); | ||
} | ||
} | ||
|
||
protected function checkForAttributeUpdates($attributeCode, $attributeArray, $attributeConfig) | ||
{ | ||
$requiresUpdate = false; | ||
$nest = 1; | ||
foreach ($attributeConfig as $name => $value) { | ||
|
||
if ($name == "product_types") { | ||
$value = implode(',', $value); | ||
} | ||
|
||
$name = $this->mapAttributeConfig($name); | ||
|
||
if ($name == 'option') { | ||
continue; | ||
} | ||
|
||
if (!isset($attributeArray[$name])) { | ||
$this->log->logError(sprintf( | ||
'Attribute %s type %s does not exist or is not mapped', | ||
$attributeCode, | ||
$name | ||
), $nest); | ||
continue; | ||
} | ||
|
||
if ($attributeArray[$name] != $value) { | ||
$this->log->logInfo(sprintf( | ||
'Update required for %s as %s is "%s" but should be "%s"', | ||
$attributeCode, | ||
$name, | ||
$attributeArray[$name], | ||
$value | ||
), $nest); | ||
|
||
$requiresUpdate = true; | ||
|
||
continue; | ||
} | ||
|
||
$this->log->logComment(sprintf( | ||
'No Update required for %s as %s is still "%s"', | ||
$attributeCode, | ||
$name, | ||
$value | ||
), $nest); | ||
} | ||
|
||
return $requiresUpdate; | ||
} | ||
|
||
protected function mapAttributeConfig($name) | ||
{ | ||
if ($name == 'label') { | ||
$name = 'frontend_label'; | ||
} | ||
|
||
if ($name == 'type') { | ||
$name = 'backend_type'; | ||
} | ||
|
||
if ($name == 'input') { | ||
$name = 'frontend_input'; | ||
} | ||
|
||
if ($name == 'product_types') { | ||
$name = 'apply_to'; | ||
} | ||
return $name; | ||
} | ||
|
||
private function manageAttributeOptions($attributeCode, $option) | ||
{ | ||
$attributeOptions = $this->productAttributeRepository->get($attributeCode)->getOptions(); | ||
|
||
// Loop through existing attributes options | ||
$existingAttributeOptions = array(); | ||
foreach ($attributeOptions as $attributeOption) { | ||
$value = $attributeOption->getLabel(); | ||
$existingAttributeOptions[] = $value; | ||
} | ||
|
||
$optionsToAdd = array_diff($option['values'], $existingAttributeOptions); | ||
//$optionsToRemove = array_diff($existingAttributeOptions, $option['values']); | ||
|
||
return $optionsToAdd; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
attribute_sets: | ||
- | ||
name: Shirts | ||
inherit: Default | ||
groups: | ||
- | ||
name: General | ||
attributes: | ||
- colour | ||
- color | ||
- | ||
name: Prices | ||
attributes: | ||
- rrp | ||
- test_attr | ||
- | ||
name: Example Attribute Set 2 | ||
inherit: Default | ||
groups: | ||
- | ||
name: Prices | ||
attributes: | ||
- rrp | ||
- | ||
name: Matthew Attribute Set | ||
inherit: Default | ||
groups: | ||
- | ||
name: Prices | ||
attributes: | ||
- rrp | ||
- | ||
name: Matthew Attribute Set 2 | ||
groups: | ||
- | ||
name: Prices | ||
attributes: | ||
- rrp | ||
|
Oops, something went wrong.