From 16b6ba4e475b4e5e6d9969295a303d9e34baa17b Mon Sep 17 00:00:00 2001 From: Lorenzo Ruozzi Date: Wed, 29 Sep 2021 17:00:19 +0200 Subject: [PATCH] Use current attribute option value if it is not translatable --- .../Documentable/DocumentableProductTrait.php | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Model/Documentable/DocumentableProductTrait.php b/src/Model/Documentable/DocumentableProductTrait.php index 50365e00..9d1c1e29 100644 --- a/src/Model/Documentable/DocumentableProductTrait.php +++ b/src/Model/Documentable/DocumentableProductTrait.php @@ -16,6 +16,7 @@ use MonsieurBiz\SyliusSearchPlugin\generated\Model\Taxon as DocumentTaxon; use MonsieurBiz\SyliusSearchPlugin\Model\Document\Result; use MonsieurBiz\SyliusSearchPlugin\Model\Document\ResultInterface; +use Sylius\Component\Attribute\AttributeType\SelectAttributeType; use Sylius\Component\Attribute\Model\AttributeValueInterface; use Sylius\Component\Core\Model\Channel; use Sylius\Component\Core\Model\Image; @@ -171,17 +172,27 @@ protected function addTaxonsInDocument(ResultInterface $document, string $locale */ protected function addAttributesInDocument(ResultInterface $document, string $locale): ResultInterface { - /** @var AttributeValueInterface $attribute */ - foreach ($this->getAttributesByLocale($locale, $locale) as $attribute) { - $attributeValues = []; - if (isset($attribute->getAttribute()->getConfiguration()['choices'])) { - foreach ($attribute->getValue() as $value) { - $attributeValues[] = $attribute->getAttribute()->getConfiguration()['choices'][$value][$locale]; + /** @var AttributeValueInterface $attributeValue */ + foreach ($this->getAttributesByLocale($locale, $locale) as $attributeValue) { + $productAttributeValues = []; + $attribute = $attributeValue->getAttribute(); + if ($attribute === null) { + continue; + } + if ($attribute->getType() === SelectAttributeType::TYPE) { + // Add all the selected values in the current locale if it exists, otherwise use the current value + foreach ($attributeValue->getValue() as $value) { + if (isset($attribute->getConfiguration()['choices'][$value][$locale])) { + $productAttributeValues[] = $attribute->getConfiguration()['choices'][$value][$locale]; + + continue; + } + $productAttributeValues[] = $value; } } else { - $attributeValues[] = $attribute->getValue(); + $productAttributeValues[] = $attributeValue->getValue(); } - $document->addAttribute($attribute->getCode(), $attribute->getName(), $attributeValues, $attribute->getLocaleCode() ?? $locale, 1); + $document->addAttribute($attributeValue->getCode(), $attributeValue->getName(), $productAttributeValues, $attributeValue->getLocaleCode() ?? $locale, 1); } return $document;