From 14c56caab07130ee5496a357abdb25d8f2f5e1ea Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Thu, 8 Sep 2022 22:21:27 +0200 Subject: [PATCH] Set default value for required attributes that are localizable/scopable (#22) --- Helper/Import/Product.php | 78 +++++++++++++++++++++++++++++++++++++++ etc/adminhtml/system.xml | 6 +++ etc/config.xml | 1 + etc/di.xml | 2 + 4 files changed, 87 insertions(+) create mode 100644 Helper/Import/Product.php diff --git a/Helper/Import/Product.php b/Helper/Import/Product.php new file mode 100644 index 0000000..80bfead --- /dev/null +++ b/Helper/Import/Product.php @@ -0,0 +1,78 @@ + 'value' + $mappedResult = parent::getColumnsFromResult($result, $keys); + + if (!$this->scopeConfig->getValue('akeneo_connector/justbetter/defaultstorevalues')) { + return $mappedResult; + } + + $requiredAttributes = $this->getRequiredAttributes(); + + foreach ($requiredAttributes as $requiredAttribute) { + + if ( + !array_key_exists($requiredAttribute, $result['values']) || + count($result['values'][$requiredAttribute]) == 0 || + !$this->isScopableOrLocalizable($requiredAttribute, $mappedResult) + ) { + continue; + } + + $mappedResult[$requiredAttribute] = $this->getFirstValue($result['values'][$requiredAttribute]); + } + + return $mappedResult; + } + + protected function getFirstValue(array $values): string + { + $array = array_reverse($values); + return array_pop($array)['data'] ?? ''; + } + + /** Check if an attribute is scopeable or localizable based on the column result name, ex. name-nl_NL-ecommerce */ + protected function isScopableOrLocalizable(string $attributeCode, array $columnResult): bool + { + $columns = array_keys($columnResult); + + foreach ($columns as $column) { + + if ($column === $attributeCode) { + return false; + } + + if (substr($column, 0, strlen($attributeCode)) === $attributeCode) { + return true; + } + } + + return false; + } + + /** Get a list of Magento's required product attributes */ + protected function getRequiredAttributes(): array + { + $eavAttributeTable = $this->connection->getTableName('eav_attribute'); + $eavEntityTypeTable = $this->connection->getTableName('eav_entity_type'); + + $select = $this->connection->select() + ->from("$eavAttributeTable AS attr") + ->join("$eavEntityTypeTable AS type", + "attr.entity_type_id = type.entity_type_id AND type.entity_type_code = 'catalog_product'") + ->where('is_required = 1'); + + $requiredAttributes = $this->connection->fetchAll($select); + + return array_map(fn (array $attribute) => $attribute['attribute_code'], $requiredAttributes); + } +} \ No newline at end of file diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 29fa23d..029d470 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -117,6 +117,12 @@ JustBetter\AkeneoBundle\Data\FamilyOptions When a family is selected here all of the products will get the status not visible individually + + + Magento\Config\Model\Config\Source\Yesno + Set the value on the default store for required attributes that are scopable or localizable. (Default no) + diff --git a/etc/config.xml b/etc/config.xml index 0cbf097..0e17b38 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -13,6 +13,7 @@ 0 0 ecommerce + 0 0 diff --git a/etc/di.xml b/etc/di.xml index afc15f5..414c5bc 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -40,4 +40,6 @@ + +