Skip to content

Commit

Permalink
Import from Akeneo only product options with right type (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Nov 22, 2023
1 parent 4884d3f commit a3187cc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/AttributeOptions/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ final class Importer implements ImporterInterface

private const BOOLEAN_TYPE = 'pim_catalog_boolean';

private const METRIC_TYPE = 'pim_catalog_metric';

/**
* @param RepositoryInterface<ProductAttributeInterface> $attributeRepository
* @param ?FactoryInterface<ProductOptionValueTranslationInterface> $productOptionValueTranslationFactory
Expand Down Expand Up @@ -199,6 +201,9 @@ private function filterSyliusOptionCodes(ResourceCursorInterface $akeneoAttribut
$akeneoAttributeCodes = [];
/** @var AkeneoAttribute $akeneoAttribute */
foreach ($akeneoAttributes as $akeneoAttribute) {
if (!in_array($akeneoAttribute['type'], [self::SIMPLESELECT_TYPE, self::MULTISELECT_TYPE, self::BOOLEAN_TYPE, self::METRIC_TYPE], true)) {
continue;
}
$akeneoAttributeCodes[] = $akeneoAttribute['code'];
}
$syliusOptions = $productOptionRepository->findByCodes($akeneoAttributeCodes);
Expand Down Expand Up @@ -256,14 +261,7 @@ private function importOptionValues(array $akeneoAttribute, ProductOptionInterfa
if ($akeneoAttribute['type'] === self::BOOLEAN_TYPE) {
foreach ([true, false] as $booleanValue) {
$optionValueCode = $this->getSyliusProductOptionValueCode($attributeCode, (string) $booleanValue);
$productOptionValue = null;
foreach ($option->getValues() as $value) {
if ($value->getCode() === $optionValueCode) {
$productOptionValue = $value;

break;
}
}
$productOptionValue = $this->getProductOptionValueFromOption($option, $optionValueCode);
if ($productOptionValue === null) {
$productOptionValue = $this->createNewProductOptionValue($optionValueCode, $option);
}
Expand All @@ -276,14 +274,7 @@ private function importOptionValues(array $akeneoAttribute, ProductOptionInterfa

foreach ($attributeOptions as $attributeOption) {
$optionValueCode = $this->getSyliusProductOptionValueCode($attributeCode, $attributeOption['code']);
$optionValue = null;
foreach ($option->getValues() as $value) {
if ($value->getCode() === $optionValueCode) {
$optionValue = $value;

break;
}
}
$optionValue = $this->getProductOptionValueFromOption($option, $optionValueCode);
if ($optionValue === null) {
$optionValue = $this->createNewProductOptionValue($optionValueCode, $option);
}
Expand Down Expand Up @@ -380,4 +371,20 @@ private function getTranslator(): TranslatorInterface

return $translator;
}

private function getProductOptionValueFromOption(
ProductOptionInterface $option,
string $optionValueCode,
): ?ProductOptionValueInterface {
$productOptionValue = null;
foreach ($option->getValues() as $value) {
if ($value->getCode() === $optionValueCode) {
$productOptionValue = $value;

break;
}
}

return $productOptionValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ Sylius\Component\Product\Model\ProductOption:
code: 'length'
sellable:
code: 'sellable'
product_option_of_wrong_type:
code: 'product_option_of_wrong_type'
4 changes: 4 additions & 0 deletions tests/Integration/AttributeOptions/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ public function it_updates_boolean_attribute_options_from_akeneo_to_sylius_optio
*/
public function it_returns_all_boolean_metric_simple_select_and_multiselect_attributes_identifiers_that_are_also_sylius_select_attributes_or_sylius_product_options(): void
{
InMemoryAttributeApi::addResource(Attribute::create('product_option_of_wrong_type', [
'type' => AttributeType::TEXT,
]));

$identifiers = $this->importer->getIdentifiersModifiedSince(new DateTime());

$this->assertEquals(['material', 'size', 'length', 'sellable'], $identifiers);
Expand Down

0 comments on commit a3187cc

Please sign in to comment.