Skip to content

Commit

Permalink
Use Akeneo value if label is not defined for a locale (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Jul 13, 2022
1 parent 257fc6c commit 2682d0e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
50 changes: 49 additions & 1 deletion spec/ValueHandler/ProductOptionValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function it_implements_value_handler_interface(): void
$this->shouldHaveType(ValueHandlerInterface::class);
}

public function it_supports_product_variant_as_subject(ProductVariantInterface $productVariant)
public function it_supports_product_variant_as_subject(ProductVariantInterface $productVariant): void
{
$this->supports($productVariant, self::OPTION_CODE, [])->shouldReturn(true);
}
Expand Down Expand Up @@ -355,6 +355,54 @@ public function it_skips_locale_not_defined_on_sylius(
$productOptionValueTranslationFactory->createNew()->shouldHaveBeenCalledOnce();
}

public function it_use_akeneo_value_if_label_is_null_for_a_locale(
ProductVariantInterface $productVariant,
ProductOptionValueInterface $productOptionValue,
ProductOptionValueTranslationInterface $englishProductOptionValueTranslation,
ProductOptionValueTranslationInterface $italianProductOptionValueTranslation,
ProductOptionInterface $productOption,
RepositoryInterface $productOptionValueRepository,
ApiClientInterface $apiClient,
FactoryInterface $productOptionValueTranslationFactory
): void {
$value = [
[
'scope' => null,
'locale' => null,
'data' => self::VALUE_CODE,
],
];
$productOptionValueRepository->findOneBy(['code' => self::OPTION_CODE . '_' . self::VALUE_CODE])->willReturn(null);
$productVariant->hasOptionValue($productOptionValue)->willReturn(false);
$apiClient
->findAttributeOption(self::OPTION_CODE, self::VALUE_CODE)
->willReturn(
[
'code' => self::VALUE_CODE,
'attribute' => self::OPTION_CODE,
'sort_order' => 4,
'labels' => [
'en_US' => null,
'it_IT' => self::IT_LABEL,
'de_DE' => 'German Label'
],
]
)
;
$this->handle($productVariant, self::OPTION_CODE, $value);

$productOptionValue->setCode('option-code_value-code')->shouldHaveBeenCalled();
$productOptionValue->setOption($productOption)->shouldHaveBeenCalled();
$productOption->addValue($productOptionValue)->shouldHaveBeenCalled();
$englishProductOptionValueTranslation->setValue(self::VALUE_CODE)->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setLocale('it_IT')->shouldHaveBeenCalled();
$italianProductOptionValueTranslation->setValue(self::IT_LABEL)->shouldHaveBeenCalled();
$productOptionValue->addTranslation($englishProductOptionValueTranslation)->shouldHaveBeenCalled();
$productOptionValue->addTranslation($italianProductOptionValueTranslation)->shouldHaveBeenCalled();
$productVariant->addOptionValue($productOptionValue)->shouldHaveBeenCalled();
$productOptionValueTranslationFactory->createNew()->shouldHaveBeenCalledOnce();
}

public function it_supports_product_option_metrical_value(
ProductVariantInterface $productVariant,
ProductOptionValueInterface $productOptionValue,
Expand Down
6 changes: 5 additions & 1 deletion src/ValueHandler/ProductOptionValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ private function handleSelectOption(ProductOptionInterface $productOption, strin
)
);
}
/**
* @var string $localeCode
* @var ?string $label
*/
foreach ($akeneoAttributeOption['labels'] as $localeCode => $label) {
if ($this->translationLocaleProvider !== null &&
!in_array($localeCode, $this->translationLocaleProvider->getDefinedLocalesCodes(), true)) {
Expand All @@ -200,7 +204,7 @@ private function handleSelectOption(ProductOptionInterface $productOption, strin
$optionValueTranslation = $this->productOptionValueTranslationFactory->createNew();
$optionValueTranslation->setLocale($localeCode);
}
$optionValueTranslation->setValue($label);
$optionValueTranslation->setValue($label ?? $akeneoValue);
if (!$optionValue->hasTranslation($optionValueTranslation)) {
$optionValue->addTranslation($optionValueTranslation);
}
Expand Down

0 comments on commit 2682d0e

Please sign in to comment.