Skip to content

Commit 9bd5def

Browse files
committed
#26121: special price & tier price are coming in base currency
Refactor TierPriceBuilder
1 parent 4c5086a commit 9bd5def

File tree

3 files changed

+97
-58
lines changed

3 files changed

+97
-58
lines changed

app/code/Magento/Catalog/Model/Product/Price/TierPriceBuilder.php

Lines changed: 92 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use Magento\Catalog\Api\Data\ProductTierPriceInterface;
1111
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
1212
use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
13-
use Magento\Catalog\Model\Product\Price\Validation\TierPriceValidator;
13+
use Magento\Catalog\Api\Data\ProductInterface;
1414
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\Pricing\PriceCurrencyInterface;
1516
use Magento\Store\Model\ScopeInterface;
1617
use Magento\Store\Model\StoreManagerInterface;
18+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1719

1820
/**
1921
* Builds ProductTierPriceInterface objects
@@ -23,12 +25,12 @@ class TierPriceBuilder
2325
/**
2426
* @var int
2527
*/
26-
private $websiteId = 0;
28+
private $websiteId;
2729

2830
/**
2931
* @var ProductTierPriceInterfaceFactory
3032
*/
31-
protected $tierPriceFactory;
33+
private $tierPriceFactory;
3234

3335
/**
3436
* @var ProductTierPriceExtensionFactory
@@ -45,31 +47,80 @@ class TierPriceBuilder
4547
*/
4648
private $storeManager;
4749

50+
/**
51+
* @var PriceCurrencyInterface
52+
*/
53+
private $priceCurrency;
54+
4855
/**
4956
* @param ProductTierPriceInterfaceFactory $tierPriceFactory
5057
* @param ProductTierPriceExtensionFactory $tierPriceExtensionFactory
5158
* @param ScopeConfigInterface $config
5259
* @param StoreManagerInterface $storeManager
60+
* @param PriceCurrencyInterface $priceCurrency
5361
*/
5462
public function __construct(
5563
ProductTierPriceInterfaceFactory $tierPriceFactory,
5664
ProductTierPriceExtensionFactory $tierPriceExtensionFactory,
5765
ScopeConfigInterface $config,
58-
StoreManagerInterface $storeManager
66+
StoreManagerInterface $storeManager,
67+
PriceCurrencyInterface $priceCurrency
5968
) {
6069
$this->tierPriceFactory = $tierPriceFactory;
6170
$this->tierPriceExtensionFactory = $tierPriceExtensionFactory;
6271
$this->config = $config;
6372
$this->storeManager = $storeManager;
73+
$this->priceCurrency = $priceCurrency;
74+
75+
$this->setWebsiteId();
76+
}
77+
78+
/**
79+
* Gets list of product tier prices
80+
*
81+
* @param ProductInterface $product
82+
* @return ProductTierPriceInterface[]
83+
*/
84+
public function getTierPrices($product)
85+
{
86+
/** @var array $tierPricesRaw */
87+
$tierPricesRaw = $this->loadData($product);
88+
89+
return $this->buildTierPriceObjects($tierPricesRaw);
90+
}
91+
92+
/**
93+
* Get tier data for a product
94+
*
95+
* @param ProductInterface $product
96+
* @return array
97+
*/
98+
private function loadData(ProductInterface $product): array
99+
{
100+
$tierData = $product->getData(ProductAttributeInterface::CODE_TIER_PRICE);
101+
102+
if ($tierData === null) {
103+
$attribute = $product->getResource()->getAttribute(ProductAttributeInterface::CODE_TIER_PRICE);
104+
if ($attribute) {
105+
$attribute->getBackend()->afterLoad($product);
106+
$tierData = $product->getData(ProductAttributeInterface::CODE_TIER_PRICE);
107+
}
108+
}
109+
110+
if ($tierData === null || !is_array($tierData)) {
111+
return [];
112+
}
113+
114+
return $tierData;
64115
}
65116

66117
/**
67-
* Transform the raw tier prices of the product into array of ProductTierPriceInterface objects
118+
* Transform the raw tier data into array of ProductTierPriceInterface objects
68119
*
69120
* @param array $tierPricesRaw
70121
* @return ProductTierPriceInterface[]
71122
*/
72-
public function buildTierPriceObjects(array $tierPricesRaw): array
123+
private function buildTierPriceObjects(array $tierPricesRaw): array
73124
{
74125
$prices = [];
75126

@@ -88,9 +139,6 @@ public function buildTierPriceObjects(array $tierPricesRaw): array
88139
*/
89140
private function createTierPriceObjectFromRawData(array $tierPriceRaw): ProductTierPriceInterface
90141
{
91-
//Find and set the website id that would be used as a fallback if the raw data does not bear it itself
92-
$this->setWebsiteForPriceScope();
93-
94142
/** @var ProductTierPriceInterface $tierPrice */
95143
$tierPrice = $this->tierPriceFactory->create()
96144
->setExtensionAttributes($this->tierPriceExtensionFactory->create());
@@ -99,36 +147,63 @@ private function createTierPriceObjectFromRawData(array $tierPriceRaw): ProductT
99147
isset($tierPriceRaw['cust_group']) ? $tierPriceRaw['cust_group'] : ''
100148
);
101149
$tierPrice->setValue(
102-
isset($tierPriceRaw['website_price']) ? $tierPriceRaw['website_price'] : $tierPriceRaw['price']
150+
$this->getPriceValue($tierPriceRaw)
103151
);
104152
$tierPrice->setQty(
105153
isset($tierPriceRaw['price_qty']) ? $tierPriceRaw['price_qty'] : ''
106154
);
107155
$tierPrice->getExtensionAttributes()->setWebsiteId(
108-
isset($tierPriceRaw['website_id']) ? (int)$tierPriceRaw['website_id'] : $this->websiteId
156+
isset($tierPriceRaw['website_id']) ? $tierPriceRaw['website_id'] : $this->websiteId
109157
);
110158
if (isset($tierPriceRaw['percentage_value'])) {
111-
$tierPrice->getExtensionAttributes()->setPercentageValue($tierPriceRaw['percentage_value']);
159+
$tierPrice->getExtensionAttributes()->setPercentageValue(
160+
$tierPriceRaw['percentage_value']
161+
);
112162
}
113163

114164
return $tierPrice;
115165
}
116166

117167
/**
118-
* Find and set the website id, based on the catalog price scope setting
168+
* Get price value
169+
*
170+
* @param array $tierPriceRaw
171+
* @return float
119172
*/
120-
private function setWebsiteForPriceScope()
173+
private function getPriceValue(array $tierPriceRaw): float
121174
{
122-
if ($this->websiteId != 0) {
123-
return;
175+
$valueInDefaultCurrency = $this->extractPriceValue($tierPriceRaw);
176+
$valueInStoreCurrency = $this->priceCurrency->convertAndRound($valueInDefaultCurrency);
177+
178+
return $valueInStoreCurrency;
179+
}
180+
181+
/**
182+
* Extract float price value from raw data
183+
*
184+
* @param array $tierPriceRaw
185+
* @return float
186+
*/
187+
private function extractPriceValue(array $tierPriceRaw): float
188+
{
189+
if (isset($tierPriceRaw['website_price'])) {
190+
return (float)$tierPriceRaw['website_price'];
124191
}
125192

193+
return (float)$tierPriceRaw['price'];
194+
}
195+
196+
/**
197+
* Find and set the website id
198+
*/
199+
private function setWebsiteId()
200+
{
126201
$websiteId = 0;
127202
$value = $this->config->getValue('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE);
128203
if ($value != 0) {
129204
$websiteId = $this->storeManager->getWebsite()->getId();
130205
}
131206

132-
$this->websiteId = (int)$websiteId;
207+
$this->websiteId = $websiteId;
133208
}
134209
}

app/code/Magento/Catalog/Model/Product/Type/Price.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ class Price
109109
* @param \Magento\Framework\Event\ManagerInterface $eventManager
110110
* @param PriceCurrencyInterface $priceCurrency
111111
* @param GroupManagementInterface $groupManagement
112-
* @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory @deprecated obsolete dependency
112+
* @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory @deprecated
113113
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
114-
* @param ProductTierPriceExtensionFactory|null $tierPriceExtensionFactory @deprecated obsolete dependency
114+
* @param ProductTierPriceExtensionFactory|null $tierPriceExtensionFactory @deprecated
115115
* @param TierPriceBuilder $tierPriceBuilder
116116
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
117117
*/
@@ -379,9 +379,7 @@ protected function getAllCustomerGroupsId()
379379
*/
380380
public function getTierPrices($product)
381381
{
382-
$tierPricesRaw = $this->getExistingPrices($product, 'tier_price');
383-
384-
return $this->tierPriceBuilder->buildTierPriceObjects($tierPricesRaw);
382+
return $this->tierPriceBuilder->getTierPrices($product);
385383
}
386384

387385
/**

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/Product/Price/Tiers.php

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
use Magento\Customer\Model\GroupManagement;
1414
use Magento\Catalog\Api\Data\ProductTierPriceInterface;
1515
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool;
16-
use Magento\Catalog\Pricing\Price\TierPriceFactory;
17-
use Magento\Catalog\Model\Product\Price\TierPriceBuilder;
1816

1917
/**
2018
* Get product tier price information
@@ -56,38 +54,22 @@ class Tiers
5654
*/
5755
private $products = [];
5856

59-
/**
60-
* @var TierPriceFactory
61-
*/
62-
private $tierPriceFactory;
63-
64-
/**
65-
* @var TierPriceBuilder
66-
*/
67-
private $tierPriceBuilder;
68-
6957
/**
7058
* @param CollectionFactory $collectionFactory
7159
* @param ProductResource $productResource
7260
* @param PriceProviderPool $priceProviderPool
7361
* @param int $customerGroupId
74-
* @param TierPriceFactory $tierPriceFactory
75-
* @param TierPriceBuilder $tierPriceBuilder
7662
*/
7763
public function __construct(
7864
CollectionFactory $collectionFactory,
7965
ProductResource $productResource,
8066
PriceProviderPool $priceProviderPool,
81-
$customerGroupId,
82-
TierPriceFactory $tierPriceFactory,
83-
TierPriceBuilder $tierPriceBuilder
67+
$customerGroupId
8468
) {
8569
$this->collectionFactory = $collectionFactory;
8670
$this->productResource = $productResource;
8771
$this->priceProviderPool = $priceProviderPool;
8872
$this->customerGroupId = $customerGroupId;
89-
$this->tierPriceFactory = $tierPriceFactory;
90-
$this->tierPriceBuilder = $tierPriceBuilder;
9173
}
9274

9375
/**
@@ -115,23 +97,7 @@ public function getProductTierPrices($productId): ?array
11597
if (empty($this->products[$productId])) {
11698
return null;
11799
}
118-
119-
/** @var TierPrice $tierPrice */
120-
$tierPrice = $this->tierPriceFactory->create(
121-
[
122-
'saleableItem' => $this->products[$productId],
123-
'quantity' => 1,
124-
'customerGroup' => $this->customerGroupId
125-
]
126-
);
127-
128-
/** @var array $tierPricesRaw */
129-
$tierPricesRaw = $tierPrice->getTierPriceList();
130-
131-
/** @var ProductTierPriceInterface[] $tierPrices */
132-
$tierPrices = $this->tierPriceBuilder->buildTierPriceObjects($tierPricesRaw);
133-
134-
return $tierPrices;
100+
return $this->products[$productId]->getTierPrices();
135101
}
136102

137103
/**

0 commit comments

Comments
 (0)