Skip to content

Commit

Permalink
Merge pull request #81 from ctidigital/customer-attributes
Browse files Browse the repository at this point in the history
Customer attributes
  • Loading branch information
paulpartington-cti authored Mar 6, 2018
2 parents 1e7b740 + a40960e commit 15e5554
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Component/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class Attributes extends YamlComponentAbstract
'used_for_promo_rules' => 'is_used_for_promo_rules'
];

/**
* @var array
*/
protected $skipCheck = [
'option',
'used_in_forms'
];

/**
* @var string
*/
Expand Down Expand Up @@ -147,7 +155,7 @@ protected function checkForAttributeUpdates($attributeCode, $attributeArray, $at

$name = $this->mapAttributeConfig($name);

if ($name == 'option') {
if (in_array($name, $this->skipCheck)) {
continue;
}
if (!array_key_exists($name, $attributeArray)) {
Expand Down
76 changes: 75 additions & 1 deletion Component/CustomerAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
use CtiDigital\Configurator\Exception\ComponentException;
use Magento\Customer\Model\Customer;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Eav\Model\AttributeRepository;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Model\ResourceModel\Attribute;

/**
* Class CustomerAttributes
Expand All @@ -16,6 +20,9 @@
*/
class CustomerAttributes extends Attributes
{
const DEFAULT_ATTRIBUTE_SET_ID = 1;
const DEFAULT_ATTRIBUTE_GROUP_ID = 1;

protected $alias = 'customer_attributes';
protected $name = 'Customer Attributes';
protected $description = 'Component to create/maintain customer attributes.';
Expand All @@ -30,13 +37,39 @@ class CustomerAttributes extends Attributes
'position' => 'sort_order'
];

/**
* @var CustomerSetupFactory
*/
protected $customerSetup;

/**
* @var Attribute
*/
protected $attributeResource;

/**
* @var array
*/
protected $defaultForms = [
'values' => [
'customer_account_create',
'customer_account_edit',
'adminhtml_checkout',
'adminhtml_customer'
]
];

public function __construct(
LoggerInterface $log,
ObjectManagerInterface $objectManager,
EavSetup $eavSetup,
AttributeRepository $attributeRepository
AttributeRepository $attributeRepository,
CustomerSetupFactory $customerSetupFactory,
Attribute $attributeResource
) {
$this->attributeConfigMap = array_merge($this->attributeConfigMap, $this->customerConfigMap);
$this->customerSetup = $customerSetupFactory;
$this->attributeResource = $attributeResource;
parent::__construct($log, $objectManager, $eavSetup, $attributeRepository);
}

Expand All @@ -48,9 +81,50 @@ protected function processData($attributeConfigurationData = null)
try {
foreach ($attributeConfigurationData['customer_attributes'] as $attributeCode => $attributeConfiguration) {
$this->processAttribute($attributeCode, $attributeConfiguration);
$this->addAdditionalValues($attributeCode, $attributeConfiguration);
}
} catch (ComponentException $e) {
$this->log->logError($e->getMessage());
}
}

/**
* Adds necessary additional values to the attribute. Without these, values can't be saved
* to the attribute and it won't appear in any forms.
*
* @param $attributeCode
* @param $attributeConfiguration
*/
protected function addAdditionalValues($attributeCode, $attributeConfiguration)
{
if (!isset($attributeConfiguration['used_in_forms']) ||
!isset($attributeConfiguration['used_in_forms']['values'])) {
$attributeConfiguration['used_in_forms'] = $this->defaultForms;
}

/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetup->create();
try {
$attribute = $customerSetup->getEavConfig()
->getAttribute($this->entityTypeId, $attributeCode)
->addData([
'attribute_set_id' => self::DEFAULT_ATTRIBUTE_SET_ID,
'attribute_group_id' => self::DEFAULT_ATTRIBUTE_GROUP_ID,
'used_in_forms' => $attributeConfiguration['used_in_forms']['values']
]);
$this->attributeResource->save($attribute);
} catch (LocalizedException $e) {
$this->log->logError(sprintf(
'Error applying additional values to %s: %s',
$attributeCode,
$e->getMessage()
));
} catch (\Exception $e) {
$this->log->logError(sprintf(
'Error saving additional values for %s: %s',
$attributeCode,
$e->getMessage()
));
}
}
}

0 comments on commit 15e5554

Please sign in to comment.