15
15
use Magento \Framework \Model \AbstractModel ;
16
16
use Magento \Framework \ObjectManager \ResetAfterRequestInterface ;
17
17
use Magento \Framework \Serialize \SerializerInterface ;
18
+ use Magento \Store \Model \StoreManagerInterface ;
18
19
19
20
/**
20
21
* EAV config model.
@@ -71,11 +72,8 @@ class Config implements ResetAfterRequestInterface
71
72
/**
72
73
* Initialized attributes
73
74
*
74
- * array ($entityTypeCode =>
75
- * ($attributeCode => $object)
76
- * )
77
- *
78
- * @var AbstractAttribute[][]
75
+ * [int $website][string $entityTypeCode][string $code] = AbstractAttribute $attribute
76
+ * @var array<int, array<string, array<string, AbstractAttribute>>>
79
77
*/
80
78
private $ attributes ;
81
79
@@ -123,6 +121,11 @@ class Config implements ResetAfterRequestInterface
123
121
*/
124
122
protected $ _universalFactory ;
125
123
124
+ /**
125
+ * @var StoreManagerInterface
126
+ */
127
+ protected $ _storeManager ;
128
+
126
129
/**
127
130
* @var AbstractAttribute[]
128
131
*/
@@ -168,6 +171,7 @@ class Config implements ResetAfterRequestInterface
168
171
* @param SerializerInterface|null $serializer
169
172
* @param ScopeConfigInterface|null $scopeConfig
170
173
* @param array $attributesForPreload
174
+ * @param StoreManagerInterface|null $storeManager
171
175
* @codeCoverageIgnore
172
176
*/
173
177
public function __construct (
@@ -178,7 +182,8 @@ public function __construct(
178
182
\Magento \Framework \Validator \UniversalFactory $ universalFactory ,
179
183
SerializerInterface $ serializer = null ,
180
184
ScopeConfigInterface $ scopeConfig = null ,
181
- $ attributesForPreload = []
185
+ $ attributesForPreload = [],
186
+ ?StoreManagerInterface $ storeManager = null ,
182
187
) {
183
188
$ this ->_cache = $ cache ;
184
189
$ this ->_entityTypeFactory = $ entityTypeFactory ;
@@ -188,6 +193,7 @@ public function __construct(
188
193
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (SerializerInterface::class);
189
194
$ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
190
195
$ this ->attributesForPreload = $ attributesForPreload ;
196
+ $ this ->_storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
191
197
}
192
198
193
199
/**
@@ -243,7 +249,7 @@ protected function _load($id)
243
249
*/
244
250
private function loadAttributes ($ entityTypeCode )
245
251
{
246
- return $ this ->attributes [$ entityTypeCode ] ?? [];
252
+ return $ this ->attributes [$ this -> getWebsiteId ()][ $ entityTypeCode ] ?? [];
247
253
}
248
254
249
255
/**
@@ -269,7 +275,7 @@ protected function _save($obj, $id)
269
275
*/
270
276
private function saveAttribute (AbstractAttribute $ attribute , $ entityTypeCode , $ attributeCode )
271
277
{
272
- $ this ->attributes [$ entityTypeCode ][$ attributeCode ] = $ attribute ;
278
+ $ this ->attributes [$ this -> getWebsiteId ()][ $ entityTypeCode ][$ attributeCode ] = $ attribute ;
273
279
}
274
280
275
281
/**
@@ -476,7 +482,7 @@ protected function _initAttributes($entityType)
476
482
477
483
$ entityTypeCode = $ entityType ->getEntityTypeCode ();
478
484
$ attributes = $ this ->_universalFactory ->create ($ entityType ->getEntityAttributeCollection ());
479
- $ websiteId = $ attributes instanceof Collection ? $ this ->getWebsiteId ($ attributes ) : 0 ;
485
+ $ websiteId = $ attributes instanceof Collection ? $ this ->getWebsiteIdFromAttributeCollection ($ attributes ) : 0 ;
480
486
$ cacheKey = self ::ATTRIBUTES_CACHE_ID . '- ' . $ entityTypeCode . '- ' . $ websiteId ;
481
487
482
488
if ($ this ->isCacheEnabled () && $ this ->initAttributesFromCache ($ entityType , $ cacheKey )) {
@@ -537,6 +543,7 @@ public function getAttributes($entityType)
537
543
*/
538
544
public function getAttribute ($ entityType , $ code )
539
545
{
546
+ $ websiteId = $ this ->getWebsiteId ();
540
547
if ($ code instanceof \Magento \Eav \Model \Entity \Attribute \AttributeInterface) {
541
548
return $ code ;
542
549
}
@@ -548,19 +555,19 @@ public function getAttribute($entityType, $code)
548
555
$ code = $ this ->_getAttributeReference ($ code , $ entityTypeCode ) ?: $ code ;
549
556
}
550
557
551
- if (isset ($ this ->attributes [$ entityTypeCode ][$ code ])) {
558
+ if (isset ($ this ->attributes [$ websiteId ][ $ entityTypeCode ][$ code ])) {
552
559
\Magento \Framework \Profiler::stop ('EAV: ' . __METHOD__ );
553
- return $ this ->attributes [$ entityTypeCode ][$ code ];
560
+ return $ this ->attributes [$ websiteId ][ $ entityTypeCode ][$ code ];
554
561
}
555
562
556
563
if (array_key_exists ($ entityTypeCode , $ this ->attributesForPreload )
557
564
&& array_key_exists ($ code , $ this ->attributesForPreload [$ entityTypeCode ])
558
565
) {
559
566
$ this ->initSystemAttributes ($ entityType , $ this ->attributesForPreload [$ entityTypeCode ]);
560
567
}
561
- if (isset ($ this ->attributes [$ entityTypeCode ][$ code ])) {
568
+ if (isset ($ this ->attributes [$ websiteId ][ $ entityTypeCode ][$ code ])) {
562
569
\Magento \Framework \Profiler::stop ('EAV: ' . __METHOD__ );
563
- return $ this ->attributes [$ entityTypeCode ][$ code ];
570
+ return $ this ->attributes [$ websiteId ][ $ entityTypeCode ][$ code ];
564
571
}
565
572
566
573
if ($ this ->scopeConfig ->getValue (self ::XML_PATH_CACHE_USER_DEFINED_ATTRIBUTES )) {
@@ -590,7 +597,8 @@ private function initSystemAttributes($entityType, $systemAttributes)
590
597
return ;
591
598
}
592
599
$ attributeCollection = $ this ->_universalFactory ->create ($ entityType ->getEntityAttributeCollection ());
593
- $ websiteId = $ attributeCollection instanceof Collection ? $ this ->getWebsiteId ($ attributeCollection ) : 0 ;
600
+ $ websiteId = $ attributeCollection instanceof Collection
601
+ ? $ this ->getWebsiteIdFromAttributeCollection ($ attributeCollection ) : 0 ;
594
602
$ cacheKey = self ::ATTRIBUTES_CACHE_ID . '- ' . $ entityTypeCode . '- ' . $ websiteId . '-preload ' ;
595
603
if ($ this ->isCacheEnabled () && ($ attributes = $ this ->_cache ->load ($ cacheKey ))) {
596
604
$ attributes = $ this ->serializer ->unserialize ($ attributes );
@@ -628,7 +636,7 @@ private function initSystemAttributes($entityType, $systemAttributes)
628
636
$ cacheKey ,
629
637
[
630
638
\Magento \Eav \Model \Cache \Type::CACHE_TAG ,
631
- \Magento \Eav \Model \Entity \Attribute::CACHE_TAG
639
+ \Magento \Eav \Model \Entity \Attribute::CACHE_TAG ,
632
640
]
633
641
);
634
642
}
@@ -973,14 +981,25 @@ private function initAttributesFromCache(Type $entityType, string $cacheKey)
973
981
}
974
982
975
983
/**
976
- * Returns website id.
984
+ * Returns website id from attribute collection .
977
985
*
978
986
* @param Collection $attributeCollection
979
987
* @return int
980
988
*/
981
- private function getWebsiteId (Collection $ attributeCollection ): int
989
+ private function getWebsiteIdFromAttributeCollection (Collection $ attributeCollection ): int
990
+ {
991
+ return (int )$ attributeCollection ->getWebsite ()?->getId();
992
+ }
993
+
994
+ /**
995
+ * Return current website scope instance
996
+ *
997
+ * @return int website id
998
+ */
999
+ public function getWebsiteId () : int
982
1000
{
983
- return $ attributeCollection ->getWebsite () ? (int )$ attributeCollection ->getWebsite ()->getId () : 0 ;
1001
+ $ websiteId = $ this ->_storeManager ->getStore ()?->getWebsiteId();
1002
+ return (int )$ websiteId ;
984
1003
}
985
1004
986
1005
/**
0 commit comments