1818use Magento \Elasticsearch \Model \Adapter \FieldMapper \Product \FieldProviderInterface ;
1919use Magento \Framework \Api \SearchCriteriaBuilder ;
2020use Magento \Catalog \Model \ResourceModel \Category \Collection ;
21+ use Magento \Framework \App \ObjectManager ;
22+ use Magento \Store \Model \StoreManagerInterface ;
2123
2224/**
2325 * Provide dynamic fields for product.
@@ -65,6 +67,11 @@ class DynamicField implements FieldProviderInterface
6567 */
6668 private $ fieldNameResolver ;
6769
70+ /**
71+ * @var StoreManagerInterface
72+ */
73+ private $ storeManager ;
74+
6875 /**
6976 * @param FieldTypeConverterInterface $fieldTypeConverter
7077 * @param IndexTypeConverterInterface $indexTypeConverter
@@ -73,6 +80,7 @@ class DynamicField implements FieldProviderInterface
7380 * @param FieldNameResolver $fieldNameResolver
7481 * @param AttributeProvider $attributeAdapterProvider
7582 * @param Collection $categoryCollection
83+ * @param StoreManagerInterface|null $storeManager
7684 */
7785 public function __construct (
7886 FieldTypeConverterInterface $ fieldTypeConverter ,
@@ -81,7 +89,8 @@ public function __construct(
8189 SearchCriteriaBuilder $ searchCriteriaBuilder ,
8290 FieldNameResolver $ fieldNameResolver ,
8391 AttributeProvider $ attributeAdapterProvider ,
84- Collection $ categoryCollection
92+ Collection $ categoryCollection ,
93+ ?StoreManagerInterface $ storeManager = null
8594 ) {
8695 $ this ->groupRepository = $ groupRepository ;
8796 $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
@@ -90,6 +99,7 @@ public function __construct(
9099 $ this ->fieldNameResolver = $ fieldNameResolver ;
91100 $ this ->attributeAdapterProvider = $ attributeAdapterProvider ;
92101 $ this ->categoryCollection = $ categoryCollection ;
102+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
93103 }
94104
95105 /**
@@ -123,7 +133,17 @@ public function getFields(array $context = []): array
123133 $ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
124134 $ groups = $ this ->groupRepository ->getList ($ searchCriteria )->getItems ();
125135 $ priceAttribute = $ this ->attributeAdapterProvider ->getByAttributeCode ('price ' );
126- $ ctx = isset ($ context ['websiteId ' ]) ? ['websiteId ' => $ context ['websiteId ' ]] : [];
136+ /**
137+ * For backword compatibility, we use 'websiteId' if the 'storeId' parameter is missing,
138+ * although the 'websiteId' may contain the store ID instead of website ID
139+ * @see \Magento\Elasticsearch\Model\Adapter\Elasticsearch:494
140+ */
141+ $ ctx = [];
142+ if (isset ($ context ['storeId ' ])) {
143+ $ ctx ['websiteId ' ] = $ this ->storeManager ->getStore ($ context ['storeId ' ])->getWebsiteId ();
144+ } elseif (isset ($ context ['websiteId ' ])) {
145+ $ ctx ['websiteId ' ] = $ context ['websiteId ' ];
146+ }
127147 foreach ($ groups as $ group ) {
128148 $ ctx ['customerGroupId ' ] = $ group ->getId ();
129149 $ groupPriceKey = $ this ->fieldNameResolver ->getFieldName (
0 commit comments