Skip to content

Commit

Permalink
Set default value for required attributes that are localizable/scopab…
Browse files Browse the repository at this point in the history
…le (#22)
  • Loading branch information
VincentBean authored Sep 8, 2022
1 parent f472571 commit 14c56ca
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Helper/Import/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace JustBetter\AkeneoBundle\Helper\Import;

use Akeneo\Connector\Helper\Import\Product as BaseProduct;

class Product extends BaseProduct
{
/** Overwritten to set values for store view 0 when attributes are scopable/localizable and required */
protected function getColumnsFromResult(array $result, array $keys = []): array
{
// This returns the result for the temp table DB columns. ex: 'name-nl_NL-ecommerce' => '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);
}
}
6 changes: 6 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@
<source_model>JustBetter\AkeneoBundle\Data\FamilyOptions</source_model>
<comment>When a family is selected here all of the products will get the status not visible individually</comment>
</field>
<field id="defaultstorevalues" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Set default value for required attributes</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Set the value on the default store for required attributes that are scopable or localizable. (Default no)</comment>
</field>
<group id="slack" translate="label" type="text" sortOrder="11" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Slack Akeneo import notifications</label>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<settaxclass>0</settaxclass>
<enablemetricunits>0</enablemetricunits>
<metric_conversion_channel>ecommerce</metric_conversion_channel>
<defaultstorevalues>0</defaultstorevalues>
<slack>
<enable>0</enable>
<token></token>
Expand Down
2 changes: 2 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@
</argument>
</arguments>
</type>

<preference for="Akeneo\Connector\Helper\Import\Product" type="JustBetter\AkeneoBundle\Helper\Import\Product"/>
</config>

0 comments on commit 14c56ca

Please sign in to comment.