|
6 | 6 | namespace Magento\Catalog\Model\Product\Attribute\Backend; |
7 | 7 |
|
8 | 8 | use \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; |
| 9 | +use Magento\Catalog\Model\Attribute\ScopeOverriddenValue; |
9 | 10 |
|
10 | 11 | /** |
11 | | - * Catalog product price attribute backend model |
| 12 | + * Backend model for set of EAV attributes with 'frontend_input' equals 'price'. |
12 | 13 | * |
13 | 14 | * @author Magento Core Team <core@magentocommerce.com> |
14 | 15 | */ |
@@ -48,26 +49,33 @@ class Price extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend |
48 | 49 | protected $localeFormat; |
49 | 50 |
|
50 | 51 | /** |
51 | | - * Construct |
52 | | - * |
| 52 | + * @var \Magento\Catalog\Model\Attribute\ScopeOverriddenValue |
| 53 | + */ |
| 54 | + private $scopeOverriddenValue; |
| 55 | + |
| 56 | + /** |
53 | 57 | * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory |
54 | 58 | * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
55 | 59 | * @param \Magento\Catalog\Helper\Data $catalogData |
56 | 60 | * @param \Magento\Framework\App\Config\ScopeConfigInterface $config |
57 | 61 | * @param \Magento\Framework\Locale\FormatInterface $localeFormat |
| 62 | + * @param ScopeOverriddenValue|null $scopeOverriddenValue |
58 | 63 | */ |
59 | 64 | public function __construct( |
60 | 65 | \Magento\Directory\Model\CurrencyFactory $currencyFactory, |
61 | 66 | \Magento\Store\Model\StoreManagerInterface $storeManager, |
62 | 67 | \Magento\Catalog\Helper\Data $catalogData, |
63 | 68 | \Magento\Framework\App\Config\ScopeConfigInterface $config, |
64 | | - \Magento\Framework\Locale\FormatInterface $localeFormat |
| 69 | + \Magento\Framework\Locale\FormatInterface $localeFormat, |
| 70 | + ScopeOverriddenValue $scopeOverriddenValue = null |
65 | 71 | ) { |
66 | 72 | $this->_currencyFactory = $currencyFactory; |
67 | 73 | $this->_storeManager = $storeManager; |
68 | 74 | $this->_helper = $catalogData; |
69 | 75 | $this->_config = $config; |
70 | 76 | $this->localeFormat = $localeFormat; |
| 77 | + $this->scopeOverriddenValue = $scopeOverriddenValue |
| 78 | + ?: \Magento\Framework\App\ObjectManager::getInstance()->get(ScopeOverriddenValue::class); |
71 | 79 | } |
72 | 80 |
|
73 | 81 | /** |
@@ -102,50 +110,52 @@ public function setScope($attribute) |
102 | 110 | } |
103 | 111 |
|
104 | 112 | /** |
105 | | - * After Save Attribute manipulation |
| 113 | + * After Save Price Attribute manipulation |
| 114 | + * Processes product price attributes if price scoped to website and updates data when: |
| 115 | + * * Price changed for non-default store view - will update price for all stores assigned to current website. |
| 116 | + * * Price will be changed according to store currency even if price changed in product with default store id. |
| 117 | + * * In a case when price was removed for non-default store (use default option checked) the default store price |
| 118 | + * * will be used instead |
106 | 119 | * |
107 | 120 | * @param \Magento\Catalog\Model\Product $object |
108 | 121 | * @return $this |
109 | | - * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
110 | 122 | */ |
111 | 123 | public function afterSave($object) |
112 | 124 | { |
113 | | - $value = $object->getData($this->getAttribute()->getAttributeCode()); |
114 | | - /** |
115 | | - * Orig value is only for existing objects |
116 | | - */ |
117 | | - $oridData = $object->getOrigData(); |
118 | | - $origValueExist = $oridData && array_key_exists($this->getAttribute()->getAttributeCode(), $oridData); |
119 | | - if ($object->getStoreId() != 0 || !$value || $origValueExist) { |
120 | | - return $this; |
121 | | - } |
122 | | - |
123 | | - if ($this->getAttribute()->getIsGlobal() == ScopedAttributeInterface::SCOPE_WEBSITE) { |
124 | | - $baseCurrency = $this->_config->getValue( |
125 | | - \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, |
126 | | - 'default' |
127 | | - ); |
128 | | - |
129 | | - $storeIds = $object->getStoreIds(); |
130 | | - if (is_array($storeIds)) { |
131 | | - foreach ($storeIds as $storeId) { |
132 | | - $storeCurrency = $this->_storeManager->getStore($storeId)->getBaseCurrencyCode(); |
133 | | - if ($storeCurrency == $baseCurrency) { |
134 | | - continue; |
135 | | - } |
136 | | - $rate = $this->_currencyFactory->create()->load($baseCurrency)->getRate($storeCurrency); |
137 | | - if (!$rate) { |
138 | | - $rate = 1; |
139 | | - } |
140 | | - $newValue = $value * $rate; |
141 | | - $object->addAttributeUpdate($this->getAttribute()->getAttributeCode(), $newValue, $storeId); |
| 125 | + /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ |
| 126 | + $attribute = $this->getAttribute(); |
| 127 | + $attributeCode = $attribute->getAttributeCode(); |
| 128 | + $value = $object->getData($attributeCode); |
| 129 | + if ((float)$value > 0) { |
| 130 | + if ($attribute->isScopeWebsite() && $object->getStoreId() != \Magento\Store\Model\Store::DEFAULT_STORE_ID) { |
| 131 | + if ($this->isUseDefault($object)) { |
| 132 | + $value = null; |
| 133 | + } |
| 134 | + foreach ((array)$object->getWebsiteStoreIds() as $storeId) { |
| 135 | + $object->addAttributeUpdate($attributeCode, $value, $storeId); |
142 | 136 | } |
143 | 137 | } |
144 | 138 | } |
145 | 139 |
|
146 | 140 | return $this; |
147 | 141 | } |
148 | 142 |
|
| 143 | + /** |
| 144 | + * Check whether product uses default attribute's value in selected scope |
| 145 | + * @param \Magento\Catalog\Model\Product $object |
| 146 | + * @return bool |
| 147 | + */ |
| 148 | + private function isUseDefault($object) |
| 149 | + { |
| 150 | + $overridden = $this->scopeOverriddenValue->containsValue( |
| 151 | + \Magento\Catalog\Api\Data\ProductInterface::class, |
| 152 | + $object, |
| 153 | + $this->getAttribute()->getAttributeCode(), |
| 154 | + $object->getStoreId() |
| 155 | + ); |
| 156 | + return !$overridden; |
| 157 | + } |
| 158 | + |
149 | 159 | /** |
150 | 160 | * Validate |
151 | 161 | * |
|
0 commit comments