Skip to content

Commit 4c5086a

Browse files
committed
#26121: special price & tier price are coming in base currency
Refactor to pass new optional parameter to @api class constructor
1 parent 75f71a4 commit 4c5086a

File tree

6 files changed

+168
-108
lines changed

6 files changed

+168
-108
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Model\Product\Price;
9+
10+
use Magento\Catalog\Api\Data\ProductTierPriceInterface;
11+
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
12+
use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
13+
use Magento\Catalog\Model\Product\Price\Validation\TierPriceValidator;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Store\Model\ScopeInterface;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
18+
/**
19+
* Builds ProductTierPriceInterface objects
20+
*/
21+
class TierPriceBuilder
22+
{
23+
/**
24+
* @var int
25+
*/
26+
private $websiteId = 0;
27+
28+
/**
29+
* @var ProductTierPriceInterfaceFactory
30+
*/
31+
protected $tierPriceFactory;
32+
33+
/**
34+
* @var ProductTierPriceExtensionFactory
35+
*/
36+
private $tierPriceExtensionFactory;
37+
38+
/**
39+
* @var ScopeConfigInterface
40+
*/
41+
private $config;
42+
43+
/**
44+
* @var StoreManagerInterface
45+
*/
46+
private $storeManager;
47+
48+
/**
49+
* @param ProductTierPriceInterfaceFactory $tierPriceFactory
50+
* @param ProductTierPriceExtensionFactory $tierPriceExtensionFactory
51+
* @param ScopeConfigInterface $config
52+
* @param StoreManagerInterface $storeManager
53+
*/
54+
public function __construct(
55+
ProductTierPriceInterfaceFactory $tierPriceFactory,
56+
ProductTierPriceExtensionFactory $tierPriceExtensionFactory,
57+
ScopeConfigInterface $config,
58+
StoreManagerInterface $storeManager
59+
) {
60+
$this->tierPriceFactory = $tierPriceFactory;
61+
$this->tierPriceExtensionFactory = $tierPriceExtensionFactory;
62+
$this->config = $config;
63+
$this->storeManager = $storeManager;
64+
}
65+
66+
/**
67+
* Transform the raw tier prices of the product into array of ProductTierPriceInterface objects
68+
*
69+
* @param array $tierPricesRaw
70+
* @return ProductTierPriceInterface[]
71+
*/
72+
public function buildTierPriceObjects(array $tierPricesRaw): array
73+
{
74+
$prices = [];
75+
76+
foreach ($tierPricesRaw as $tierPriceRaw) {
77+
$prices[] = $this->createTierPriceObjectFromRawData($tierPriceRaw);
78+
}
79+
80+
return $prices;
81+
}
82+
83+
/**
84+
* Transform the raw tier price data into ProductTierPriceInterface object
85+
*
86+
* @param array $tierPriceRaw
87+
* @return ProductTierPriceInterface
88+
*/
89+
private function createTierPriceObjectFromRawData(array $tierPriceRaw): ProductTierPriceInterface
90+
{
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+
94+
/** @var ProductTierPriceInterface $tierPrice */
95+
$tierPrice = $this->tierPriceFactory->create()
96+
->setExtensionAttributes($this->tierPriceExtensionFactory->create());
97+
98+
$tierPrice->setCustomerGroupId(
99+
isset($tierPriceRaw['cust_group']) ? $tierPriceRaw['cust_group'] : ''
100+
);
101+
$tierPrice->setValue(
102+
isset($tierPriceRaw['website_price']) ? $tierPriceRaw['website_price'] : $tierPriceRaw['price']
103+
);
104+
$tierPrice->setQty(
105+
isset($tierPriceRaw['price_qty']) ? $tierPriceRaw['price_qty'] : ''
106+
);
107+
$tierPrice->getExtensionAttributes()->setWebsiteId(
108+
isset($tierPriceRaw['website_id']) ? (int)$tierPriceRaw['website_id'] : $this->websiteId
109+
);
110+
if (isset($tierPriceRaw['percentage_value'])) {
111+
$tierPrice->getExtensionAttributes()->setPercentageValue($tierPriceRaw['percentage_value']);
112+
}
113+
114+
return $tierPrice;
115+
}
116+
117+
/**
118+
* Find and set the website id, based on the catalog price scope setting
119+
*/
120+
private function setWebsiteForPriceScope()
121+
{
122+
if ($this->websiteId != 0) {
123+
return;
124+
}
125+
126+
$websiteId = 0;
127+
$value = $this->config->getValue('catalog/price/scope', ScopeInterface::SCOPE_WEBSITE);
128+
if ($value != 0) {
129+
$websiteId = $this->storeManager->getWebsite()->getId();
130+
}
131+
132+
$this->websiteId = (int)$websiteId;
133+
}
134+
}

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

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Catalog\Model\Product\Type;
99

1010
use Magento\Catalog\Model\Product;
11+
use Magento\Catalog\Model\Product\Price\TierPriceBuilder;
1112
use Magento\Customer\Api\GroupManagementInterface;
1213
use Magento\Framework\Pricing\PriceCurrencyInterface;
1314
use Magento\Store\Model\Store;
@@ -93,6 +94,11 @@ class Price
9394
*/
9495
private $tierPriceExtensionFactory;
9596

97+
/**
98+
* @var TierPriceBuilder
99+
*/
100+
private $tierPriceBuilder;
101+
96102
/**
97103
* Constructor
98104
*
@@ -103,9 +109,10 @@ class Price
103109
* @param \Magento\Framework\Event\ManagerInterface $eventManager
104110
* @param PriceCurrencyInterface $priceCurrency
105111
* @param GroupManagementInterface $groupManagement
106-
* @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory
112+
* @param \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory @deprecated obsolete dependency
107113
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
108-
* @param ProductTierPriceExtensionFactory|null $tierPriceExtensionFactory
114+
* @param ProductTierPriceExtensionFactory|null $tierPriceExtensionFactory @deprecated obsolete dependency
115+
* @param TierPriceBuilder $tierPriceBuilder
109116
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
110117
*/
111118
public function __construct(
@@ -118,7 +125,8 @@ public function __construct(
118125
GroupManagementInterface $groupManagement,
119126
\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory,
120127
\Magento\Framework\App\Config\ScopeConfigInterface $config,
121-
ProductTierPriceExtensionFactory $tierPriceExtensionFactory = null
128+
ProductTierPriceExtensionFactory $tierPriceExtensionFactory = null,
129+
?TierPriceBuilder $tierPriceBuilder = null
122130
) {
123131
$this->_ruleFactory = $ruleFactory;
124132
$this->_storeManager = $storeManager;
@@ -131,6 +139,8 @@ public function __construct(
131139
$this->config = $config;
132140
$this->tierPriceExtensionFactory = $tierPriceExtensionFactory ?: ObjectManager::getInstance()
133141
->get(ProductTierPriceExtensionFactory::class);
142+
$this->tierPriceBuilder = $tierPriceBuilder ?: ObjectManager::getInstance()
143+
->get(TierPriceBuilder::class);
134144
}
135145

136146
/**
@@ -371,49 +381,7 @@ public function getTierPrices($product)
371381
{
372382
$tierPricesRaw = $this->getExistingPrices($product, 'tier_price');
373383

374-
return $this->buildProductTierPriceInterfaceObjects($tierPricesRaw);
375-
}
376-
377-
/**
378-
* Return ProductTierPriceInterface[] given raw tier prices array
379-
*
380-
* @param array $tierPricesRaw
381-
* @return \Magento\Catalog\Api\Data\ProductTierPriceInterface[]
382-
*/
383-
public function transformTierPrices($tierPricesRaw)
384-
{
385-
return $this->buildProductTierPriceInterfaceObjects($tierPricesRaw);
386-
}
387-
388-
/**
389-
* Return ProductTierPriceInterface[] given raw tier prices array
390-
*
391-
* @param array $tierPricesRaw
392-
* @return \Magento\Catalog\Api\Data\ProductTierPriceInterface[]
393-
*/
394-
private function buildProductTierPriceInterfaceObjects($tierPricesRaw)
395-
{
396-
$prices = [];
397-
foreach ($tierPricesRaw as $price) {
398-
/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice */
399-
$tierPrice = $this->tierPriceFactory->create()
400-
->setExtensionAttributes($this->tierPriceExtensionFactory->create());
401-
$tierPrice->setCustomerGroupId($price['cust_group']);
402-
if (array_key_exists('website_price', $price)) {
403-
$value = $price['website_price'];
404-
} else {
405-
$value = $price['price'];
406-
}
407-
$tierPrice->setValue($value);
408-
$tierPrice->setQty($price['price_qty']);
409-
if (isset($price['percentage_value'])) {
410-
$tierPrice->getExtensionAttributes()->setPercentageValue($price['percentage_value']);
411-
}
412-
$websiteId = isset($price['website_id']) ? $price['website_id'] : $this->getWebsiteForPriceScope();
413-
$tierPrice->getExtensionAttributes()->setWebsiteId($websiteId);
414-
$prices[] = $tierPrice;
415-
}
416-
return $prices;
384+
return $this->tierPriceBuilder->buildTierPriceObjects($tierPricesRaw);
417385
}
418386

419387
/**

app/code/Magento/Catalog/Pricing/Price/TierPrice.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TierPrice extends AbstractPrice implements TierPriceInterface, BasePricePr
7070
* @param Session $customerSession
7171
* @param GroupManagementInterface $groupManagement
7272
* @param CustomerGroupRetrieverInterface|null $customerGroupRetriever
73+
* @param int|null $customerGroup
7374
*/
7475
public function __construct(
7576
Product $saleableItem,
@@ -78,15 +79,18 @@ public function __construct(
7879
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
7980
Session $customerSession,
8081
GroupManagementInterface $groupManagement,
81-
CustomerGroupRetrieverInterface $customerGroupRetriever = null
82+
CustomerGroupRetrieverInterface $customerGroupRetriever = null,
83+
$customerGroup = null
8284
) {
8385
$quantity = (float)$quantity ? $quantity : 1;
8486
parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
8587
$this->customerSession = $customerSession;
8688
$this->groupManagement = $groupManagement;
8789
$this->customerGroupRetriever = $customerGroupRetriever
8890
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomerGroupRetrieverInterface::class);
89-
if ($saleableItem->hasCustomerGroupId()) {
91+
if ($customerGroup) {
92+
$this->customerGroup = $customerGroup;
93+
} elseif ($saleableItem->hasCustomerGroupId()) {
9094
$this->customerGroup = (int) $saleableItem->getCustomerGroupId();
9195
} else {
9296
$this->customerGroup = (int) $this->customerGroupRetriever->getCustomerGroupId();

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
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\Model\Product\Type\Price;
17-
use Magento\CatalogCustomerGraphQl\Pricing\Price\TierPriceFactory;
16+
use Magento\Catalog\Pricing\Price\TierPriceFactory;
17+
use Magento\Catalog\Model\Product\Price\TierPriceBuilder;
1818

1919
/**
2020
* Get product tier price information
@@ -57,37 +57,37 @@ class Tiers
5757
private $products = [];
5858

5959
/**
60-
* @var Price
60+
* @var TierPriceFactory
6161
*/
62-
private $price;
62+
private $tierPriceFactory;
6363

6464
/**
65-
* @var TierPriceFactory
65+
* @var TierPriceBuilder
6666
*/
67-
private $tierPriceFactory;
67+
private $tierPriceBuilder;
6868

6969
/**
7070
* @param CollectionFactory $collectionFactory
7171
* @param ProductResource $productResource
7272
* @param PriceProviderPool $priceProviderPool
7373
* @param int $customerGroupId
74-
* @param Price $price
7574
* @param TierPriceFactory $tierPriceFactory
75+
* @param TierPriceBuilder $tierPriceBuilder
7676
*/
7777
public function __construct(
7878
CollectionFactory $collectionFactory,
7979
ProductResource $productResource,
8080
PriceProviderPool $priceProviderPool,
8181
$customerGroupId,
82-
Price $price,
83-
TierPriceFactory $tierPriceFactory
82+
TierPriceFactory $tierPriceFactory,
83+
TierPriceBuilder $tierPriceBuilder
8484
) {
8585
$this->collectionFactory = $collectionFactory;
8686
$this->productResource = $productResource;
8787
$this->priceProviderPool = $priceProviderPool;
8888
$this->customerGroupId = $customerGroupId;
89-
$this->price = $price;
9089
$this->tierPriceFactory = $tierPriceFactory;
90+
$this->tierPriceBuilder = $tierPriceBuilder;
9191
}
9292

9393
/**
@@ -121,15 +121,15 @@ public function getProductTierPrices($productId): ?array
121121
[
122122
'saleableItem' => $this->products[$productId],
123123
'quantity' => 1,
124-
'customerGroupId' => $this->customerGroupId
124+
'customerGroup' => $this->customerGroupId
125125
]
126126
);
127127

128128
/** @var array $tierPricesRaw */
129129
$tierPricesRaw = $tierPrice->getTierPriceList();
130130

131131
/** @var ProductTierPriceInterface[] $tierPrices */
132-
$tierPrices = $this->price->transformTierPrices($tierPricesRaw);
132+
$tierPrices = $this->tierPriceBuilder->buildTierPriceObjects($tierPricesRaw);
133133

134134
return $tierPrices;
135135
}

app/code/Magento/CatalogCustomerGraphQl/Pricing/Price/TierPrice.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)