18
18
use Magento \Elasticsearch \Model \Adapter \FieldMapper \Product \FieldProviderInterface ;
19
19
use Magento \Framework \Api \SearchCriteriaBuilder ;
20
20
use Magento \Catalog \Model \ResourceModel \Category \Collection ;
21
+ use Magento \Framework \App \ObjectManager ;
22
+ use Magento \Store \Model \StoreManagerInterface ;
21
23
22
24
/**
23
25
* Provide dynamic fields for product.
@@ -65,6 +67,11 @@ class DynamicField implements FieldProviderInterface
65
67
*/
66
68
private $ fieldNameResolver ;
67
69
70
+ /**
71
+ * @var StoreManagerInterface
72
+ */
73
+ private $ storeManager ;
74
+
68
75
/**
69
76
* @param FieldTypeConverterInterface $fieldTypeConverter
70
77
* @param IndexTypeConverterInterface $indexTypeConverter
@@ -73,6 +80,7 @@ class DynamicField implements FieldProviderInterface
73
80
* @param FieldNameResolver $fieldNameResolver
74
81
* @param AttributeProvider $attributeAdapterProvider
75
82
* @param Collection $categoryCollection
83
+ * @param StoreManagerInterface|null $storeManager
76
84
*/
77
85
public function __construct (
78
86
FieldTypeConverterInterface $ fieldTypeConverter ,
@@ -81,7 +89,8 @@ public function __construct(
81
89
SearchCriteriaBuilder $ searchCriteriaBuilder ,
82
90
FieldNameResolver $ fieldNameResolver ,
83
91
AttributeProvider $ attributeAdapterProvider ,
84
- Collection $ categoryCollection
92
+ Collection $ categoryCollection ,
93
+ ?StoreManagerInterface $ storeManager = null
85
94
) {
86
95
$ this ->groupRepository = $ groupRepository ;
87
96
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
@@ -90,6 +99,7 @@ public function __construct(
90
99
$ this ->fieldNameResolver = $ fieldNameResolver ;
91
100
$ this ->attributeAdapterProvider = $ attributeAdapterProvider ;
92
101
$ this ->categoryCollection = $ categoryCollection ;
102
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
93
103
}
94
104
95
105
/**
@@ -123,7 +133,17 @@ public function getFields(array $context = []): array
123
133
$ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
124
134
$ groups = $ this ->groupRepository ->getList ($ searchCriteria )->getItems ();
125
135
$ 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
+ }
127
147
foreach ($ groups as $ group ) {
128
148
$ ctx ['customerGroupId ' ] = $ group ->getId ();
129
149
$ groupPriceKey = $ this ->fieldNameResolver ->getFieldName (
0 commit comments