|
14 | 14 | use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
|
15 | 15 | use Magento\Framework\App\Config\Element;
|
16 | 16 | use Magento\Framework\App\ResourceConnection\Config;
|
| 17 | +use Magento\Framework\DataObject; |
17 | 18 | use Magento\Framework\Exception\LocalizedException;
|
18 | 19 | use Magento\Framework\Model\AbstractModel;
|
19 | 20 | use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
|
@@ -204,6 +205,13 @@ abstract class AbstractEntity extends AbstractResource implements EntityInterfac
|
204 | 205 | */
|
205 | 206 | protected $objectRelationProcessor;
|
206 | 207 |
|
| 208 | + /** |
| 209 | + * Attributes stored by scope (store id and attribute set id). |
| 210 | + * |
| 211 | + * @var array |
| 212 | + */ |
| 213 | + private $attributesByScope; |
| 214 | + |
207 | 215 | /**
|
208 | 216 | * @param Context $context
|
209 | 217 | * @param array $data
|
@@ -447,6 +455,20 @@ public function getAttribute($attribute)
|
447 | 455 | return $attribute;
|
448 | 456 | }
|
449 | 457 |
|
| 458 | + /** |
| 459 | + * Adding attribute to entity by scope. |
| 460 | + * |
| 461 | + * @param AbstractAttribute $attribute |
| 462 | + * @param string $suffix |
| 463 | + * @return $this |
| 464 | + */ |
| 465 | + public function addAttributeByScope(AbstractAttribute $attribute, $suffix) |
| 466 | + { |
| 467 | + $attributeCode = $attribute->getAttributeCode(); |
| 468 | + $this->attributesByScope[$suffix][$attributeCode] = $attribute; |
| 469 | + return $this->addAttribute($attribute); |
| 470 | + } |
| 471 | + |
450 | 472 | /**
|
451 | 473 | * Adding attribute to entity
|
452 | 474 | *
|
@@ -572,6 +594,31 @@ protected function _isApplicableAttribute($object, $attribute)
|
572 | 594 | return true;
|
573 | 595 | }
|
574 | 596 |
|
| 597 | + /** |
| 598 | + * Get attributes by scope |
| 599 | + * |
| 600 | + * @return array |
| 601 | + */ |
| 602 | + private function getAttributesByScope($suffix) |
| 603 | + { |
| 604 | + return !empty($this->attributesByScope[$suffix]) |
| 605 | + ? $this->attributesByScope[$suffix] |
| 606 | + : $this->getAttributesByCode(); |
| 607 | + } |
| 608 | + |
| 609 | + /** |
| 610 | + * Get attributes cache suffix. |
| 611 | + * |
| 612 | + * @param DataObject $object |
| 613 | + * @return string |
| 614 | + */ |
| 615 | + private function getAttributesCacheSuffix(DataObject $object) |
| 616 | + { |
| 617 | + $attributeSetId = $object->getAttributeSetId() ?: 0; |
| 618 | + $storeId = $object->getStoreId() ?: 0; |
| 619 | + return $storeId . '-' . $attributeSetId; |
| 620 | + } |
| 621 | + |
575 | 622 | /**
|
576 | 623 | * Walk through the attributes and run method with optional arguments
|
577 | 624 | *
|
@@ -607,7 +654,8 @@ public function walkAttributes($partMethod, array $args = [], $collectExceptionM
|
607 | 654 | break;
|
608 | 655 | }
|
609 | 656 | $results = [];
|
610 |
| - foreach ($this->getAttributesByCode() as $attrCode => $attribute) { |
| 657 | + $suffix = $this->getAttributesCacheSuffix($args[0]); |
| 658 | + foreach ($this->getAttributesByScope($suffix) as $attrCode => $attribute) { |
611 | 659 | if (isset($args[0]) && is_object($args[0]) && !$this->_isApplicableAttribute($args[0], $attribute)) {
|
612 | 660 | continue;
|
613 | 661 | }
|
|
0 commit comments