1313namespace BitBag \SyliusElasticsearchPlugin \Form \Type \ChoiceMapper ;
1414
1515use BitBag \SyliusElasticsearchPlugin \Formatter \StringFormatterInterface ;
16+ use Sylius \Component \Locale \Context \LocaleContextInterface ;
1617use Sylius \Component \Product \Model \ProductAttributeInterface ;
1718use Sylius \Component \Product \Model \ProductAttributeValueInterface ;
1819use Sylius \Component \Product \Repository \ProductAttributeValueRepositoryInterface ;
@@ -24,20 +25,29 @@ final class ProductAttributesMapper implements ProductAttributesMapperInterface
2425 */
2526 private $ productAttributeValueRepository ;
2627
28+ /**
29+ * @var LocaleContextInterface
30+ */
31+ private $ localeContext ;
32+
2733 /**
2834 * @var StringFormatterInterface
2935 */
3036 private $ stringFormatter ;
3137
3238 /**
3339 * @param ProductAttributeValueRepositoryInterface $productAttributeValueRepository
40+ * @param LocaleContextInterface $localeContext
3441 * @param StringFormatterInterface $stringFormatter
3542 */
3643 public function __construct (
3744 ProductAttributeValueRepositoryInterface $ productAttributeValueRepository ,
45+ LocaleContextInterface $ localeContext ,
3846 StringFormatterInterface $ stringFormatter
39- ) {
47+ )
48+ {
4049 $ this ->productAttributeValueRepository = $ productAttributeValueRepository ;
50+ $ this ->localeContext = $ localeContext ;
4151 $ this ->stringFormatter = $ stringFormatter ;
4252 }
4353
@@ -50,13 +60,18 @@ public function mapToChoices(ProductAttributeInterface $productAttribute): array
5060 $ choices = [];
5161 array_walk ($ attributeValues , function (ProductAttributeValueInterface $ productAttributeValue ) use (&$ choices ) {
5262 $ value = $ productAttributeValue ->getValue ();
53- if (is_array ($ value )) {
63+ $ configuration = $ productAttributeValue ->getAttribute ()->getConfiguration ();
64+ if (is_array ($ value )
65+ && isset ($ configuration ['choices ' ])
66+ && is_array ($ configuration ['choices ' ])) {
5467 foreach ($ value as $ singleValue ) {
55- $ choices [$ singleValue ] = $ this ->stringFormatter ->formatToLowercaseWithoutSpaces ($ singleValue );
68+ $ choice = $ this ->stringFormatter ->formatToLowercaseWithoutSpaces ($ singleValue );
69+ $ label = $ configuration ['choices ' ][$ singleValue ][$ this ->localeContext ->getLocaleCode ()];
70+ $ choices [$ label ] = $ choice ;
5671 }
5772 } else {
58- $ choiceValue = is_string ($ value ) ? $ this ->stringFormatter ->formatToLowercaseWithoutSpaces ($ value ) : $ value ;
59- $ choices [$ value ] = $ choiceValue ;
73+ $ choice = is_string ($ value ) ? $ this ->stringFormatter ->formatToLowercaseWithoutSpaces ($ value ) : $ value ;
74+ $ choices [$ value ] = $ choice ;
6075 }
6176 });
6277
0 commit comments