Skip to content

Commit e4be19b

Browse files
fix tier price show only including price
1 parent 53d7226 commit e4be19b

File tree

4 files changed

+124
-58
lines changed

4 files changed

+124
-58
lines changed

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

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,27 @@
99
use Magento\Catalog\Model\Product;
1010
use Magento\Customer\Api\GroupManagementInterface;
1111
use Magento\Customer\Model\Session;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\App\ObjectManager;
1214
use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
1315
use Magento\Framework\Pricing\Amount\AmountInterface;
1416
use Magento\Framework\Pricing\Price\AbstractPrice;
1517
use Magento\Framework\Pricing\Price\BasePriceProviderInterface;
18+
use Magento\Framework\Pricing\PriceCurrencyInterface;
1619
use Magento\Framework\Pricing\PriceInfoInterface;
1720
use Magento\Customer\Model\Group\RetrieverInterface as CustomerGroupRetrieverInterface;
21+
use Magento\Tax\Model\Config;
1822

1923
/**
2024
* @api
2125
* @since 100.0.2
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2228
*/
2329
class TierPrice extends AbstractPrice implements TierPriceInterface, BasePriceProviderInterface
2430
{
31+
private const XML_PATH_TAX_DISPLAY_TYPE = 'tax/display/type';
32+
2533
/**
2634
* Price type tier
2735
*/
@@ -61,42 +69,44 @@ class TierPrice extends AbstractPrice implements TierPriceInterface, BasePricePr
6169
* @var CustomerGroupRetrieverInterface
6270
*/
6371
private $customerGroupRetriever;
72+
6473
/**
65-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
74+
* @var ScopeConfigInterface
6675
*/
6776
private $scopeConfig;
6877

6978
/**
7079
* @param Product $saleableItem
7180
* @param float $quantity
7281
* @param CalculatorInterface $calculator
73-
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
82+
* @param PriceCurrencyInterface $priceCurrency
7483
* @param Session $customerSession
7584
* @param GroupManagementInterface $groupManagement
7685
* @param CustomerGroupRetrieverInterface|null $customerGroupRetriever
86+
* @param ScopeConfigInterface|null $scopeConfig
7787
*/
7888
public function __construct(
7989
Product $saleableItem,
8090
$quantity,
8191
CalculatorInterface $calculator,
82-
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
92+
PriceCurrencyInterface $priceCurrency,
8393
Session $customerSession,
8494
GroupManagementInterface $groupManagement,
8595
CustomerGroupRetrieverInterface $customerGroupRetriever = null,
86-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
96+
?ScopeConfigInterface $scopeConfig = null
8797
) {
8898
$quantity = (float)$quantity ? $quantity : 1;
8999
parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
90100
$this->customerSession = $customerSession;
91101
$this->groupManagement = $groupManagement;
92102
$this->customerGroupRetriever = $customerGroupRetriever
93-
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomerGroupRetrieverInterface::class);
103+
?? ObjectManager::getInstance()->get(CustomerGroupRetrieverInterface::class);
94104
if ($saleableItem->hasCustomerGroupId()) {
95105
$this->customerGroup = (int) $saleableItem->getCustomerGroupId();
96106
} else {
97107
$this->customerGroup = (int) $this->customerGroupRetriever->getCustomerGroupId();
98108
}
99-
$this->scopeConfig = $scopeConfig;
109+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
100110
}
101111

102112
/**
@@ -142,6 +152,8 @@ protected function isFirstPriceBetter($firstPrice, $secondPrice)
142152
}
143153

144154
/**
155+
* Returns tier price count
156+
*
145157
* @return int
146158
*/
147159
public function getTierPriceCount()
@@ -150,6 +162,8 @@ public function getTierPriceCount()
150162
}
151163

152164
/**
165+
* Returns tier price list
166+
*
153167
* @return array
154168
*/
155169
public function getTierPriceList()
@@ -161,18 +175,32 @@ public function getTierPriceList()
161175
$this->priceList,
162176
function (&$priceData) {
163177
/* convert string value to float */
164-
$priceData['price_qty'] = $priceData['price_qty'] * 1;
165-
$priceData['price'] = $this->applyAdjustment($priceData['price']);
166-
if ($this->scopeConfig->getValue('tax/display/type') == 3) {
167-
$priceData['excl_tax_price'] = $this->applyAdjustment($priceData['excl_tax_price'], true);
178+
$priceData['price_qty'] *= 1;
179+
if ($this->getConfigTaxDisplayType() === Config::DISPLAY_TYPE_BOTH) {
180+
$exclTaxPrice = $this->calculator->getAmount($priceData['price'], $this->product, true);
181+
$priceData['excl_tax_price'] = $exclTaxPrice;
168182
}
183+
$priceData['price'] = $this->applyAdjustment($priceData['price']);
169184
}
170185
);
171186
}
187+
172188
return $this->priceList;
173189
}
174190

175191
/**
192+
* Returns config tax display type
193+
*
194+
* @return int
195+
*/
196+
private function getConfigTaxDisplayType(): int
197+
{
198+
return (int) $this->scopeConfig->getValue(self::XML_PATH_TAX_DISPLAY_TYPE);
199+
}
200+
201+
/**
202+
* Filters tier prices
203+
*
176204
* @param array $priceList
177205
* @return array
178206
*/
@@ -213,6 +241,8 @@ protected function filterTierPrices(array $priceList)
213241
}
214242

215243
/**
244+
* Returns base price
245+
*
216246
* @return float
217247
*/
218248
protected function getBasePrice()
@@ -222,27 +252,24 @@ protected function getBasePrice()
222252
}
223253

224254
/**
225-
* Calculates savings percentage according to the given tier price amount
226-
* and related product price amount.
255+
* Calculates savings percentage according to the given tier price amount and related product price amount.
227256
*
228257
* @param AmountInterface $amount
229-
*
230258
* @return float
231259
*/
232260
public function getSavePercent(AmountInterface $amount)
233261
{
234-
$productPriceAmount = $this->priceInfo->getPrice(
235-
FinalPrice::PRICE_CODE
236-
)->getAmount();
262+
$productPriceAmount = $this->priceInfo->getPrice(FinalPrice::PRICE_CODE)
263+
->getAmount();
237264

238-
return round(
239-
100 - ((100 / $productPriceAmount->getValue()) * $amount->getValue())
240-
);
265+
return round(100 - ((100 / $productPriceAmount->getValue()) * $amount->getValue()));
241266
}
242267

243268
/**
269+
* Apply adjustment to price
270+
*
244271
* @param float|string $price
245-
* @return \Magento\Framework\Pricing\Amount\AmountInterface
272+
* @return AmountInterface
246273
*/
247274
protected function applyAdjustment($price)
248275
{
@@ -323,6 +350,8 @@ protected function getStoredTierPrices()
323350
}
324351

325352
/**
353+
* Return is percentage discount
354+
*
326355
* @return bool
327356
*/
328357
public function isPercentageDiscount()

app/code/Magento/ConfigurableProduct/Block/Product/View/Type/Configurable.php

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace Magento\ConfigurableProduct\Block\Product\View\Type;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1112
use Magento\ConfigurableProduct\Model\ConfigurableAttributeData;
1213
use Magento\Customer\Helper\Session\CurrentCustomer;
@@ -287,49 +288,65 @@ protected function getOptionPrices()
287288
{
288289
$prices = [];
289290
foreach ($this->getAllowProducts() as $product) {
290-
$tierPrices = [];
291291
$priceInfo = $product->getPriceInfo();
292-
$tierPriceModel = $priceInfo->getPrice('tier_price');
293-
$tierPricesList = $tierPriceModel->getTierPriceList();
294-
foreach ($tierPricesList as $tierPrice) {
295-
$tierPrices[] = [
296-
'qty' => $this->localeFormat->getNumber($tierPrice['price_qty']),
297-
'price' => $this->localeFormat->getNumber($tierPrice['price']->getValue()),
298-
'excl_tax_price' => (is_object($tierPrice['excl_tax_price'])) ? $this->localeFormat->getNumber($tierPrice['excl_tax_price']->getValue()) : $tierPrice['base_price'],
299-
'percentage' => $this->localeFormat->getNumber(
300-
$tierPriceModel->getSavePercent($tierPrice['price'])
301-
),
302-
];
303-
}
304292

305-
$prices[$product->getId()] =
306-
[
307-
'oldPrice' => [
308-
'amount' => $this->localeFormat->getNumber(
309-
$priceInfo->getPrice('regular_price')->getAmount()->getValue()
310-
),
311-
],
312-
'basePrice' => [
313-
'amount' => $this->localeFormat->getNumber(
314-
$priceInfo->getPrice('final_price')->getAmount()->getBaseAmount()
315-
),
316-
],
317-
'finalPrice' => [
318-
'amount' => $this->localeFormat->getNumber(
319-
$priceInfo->getPrice('final_price')->getAmount()->getValue()
320-
),
321-
],
322-
'tierPrices' => $tierPrices,
323-
'msrpPrice' => [
324-
'amount' => $this->localeFormat->getNumber(
325-
$product->getMsrp()
326-
),
327-
],
328-
];
293+
$prices[$product->getId()] = [
294+
'oldPrice' => [
295+
'amount' => $this->localeFormat->getNumber(
296+
$priceInfo->getPrice('regular_price')->getAmount()->getValue()
297+
),
298+
],
299+
'basePrice' => [
300+
'amount' => $this->localeFormat->getNumber(
301+
$priceInfo->getPrice('final_price')->getAmount()->getBaseAmount()
302+
),
303+
],
304+
'finalPrice' => [
305+
'amount' => $this->localeFormat->getNumber(
306+
$priceInfo->getPrice('final_price')->getAmount()->getValue()
307+
),
308+
],
309+
'tierPrices' => $this->getTierPricesByProduct($product),
310+
'msrpPrice' => [
311+
'amount' => $this->localeFormat->getNumber(
312+
$product->getMsrp()
313+
),
314+
],
315+
];
329316
}
317+
330318
return $prices;
331319
}
332320

321+
/**
322+
* Returns product's tier prices list
323+
*
324+
* @param ProductInterface $product
325+
* @return array
326+
*/
327+
private function getTierPricesByProduct(ProductInterface $product): array
328+
{
329+
$tierPrices = [];
330+
$tierPriceModel = $product->getPriceInfo()->getPrice('tier_price');
331+
foreach ($tierPriceModel->getTierPriceList() as $tierPrice) {
332+
$tierPriceData = [
333+
'qty' => $this->localeFormat->getNumber($tierPrice['price_qty']),
334+
'price' => $this->localeFormat->getNumber($tierPrice['price']->getValue()),
335+
'percentage' => $this->localeFormat->getNumber(
336+
$tierPriceModel->getSavePercent($tierPrice['price'])
337+
),
338+
];
339+
340+
if (isset($tierPrice['excl_tax_price'])) {
341+
$excludingTax = $tierPrice['excl_tax_price'];
342+
$tierPriceData['excl_tax_price'] = $this->localeFormat->getNumber($excludingTax->getBaseAmount());
343+
}
344+
$tierPrices[] = $tierPriceData;
345+
}
346+
347+
return $tierPrices;
348+
}
349+
333350
/**
334351
* Replace ',' on '.' for js
335352
*

app/code/Magento/ConfigurableProduct/view/base/templates/product/price/tier_price.phtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
+ '<span data-price-amount="' + priceUtils.formatPrice(item.price, currencyFormat) + '"'
1212
+ ' data-price-type=""' + ' class="price-wrapper ">'
1313
+ '<span class="price">' + priceUtils.formatPrice(item.price, currencyFormat) + '</span>'
14-
+ '<span class="price"> excl. tax ' + priceUtils.formatPrice(item.excl_tax_price, currencyFormat) + '</span>
1514
+ '</span>'
1615
+ '</span>'; %>
1716
<li class="item">
@@ -22,6 +21,11 @@
2221
<?= $block->escapeHtml(__('save')) ?><span
2322
class="percent tier-<%= key %>">&nbsp;<%= item.percentage %></span>%
2423
</strong>
24+
<% if (item['excl_tax_price']) { %>
25+
<span class="price-excluding-tax" data-label="<?= $block->escapeHtml(__('Excl. Tax')) ?>">
26+
<span class="price"><%= priceUtils.formatPrice(item['excl_tax_price'], currencyFormat) %></span>
27+
</span>
28+
<% } %>
2529
</li>
2630
<% }); %>
2731
</ul>

app/design/frontend/Magento/blank/web/css/source/_price.less

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,20 @@
7373

7474
.price-style-1();
7575
.price-style-3();
76+
77+
.prices-tier.items {
78+
.price-excluding-tax {
79+
display: block;
80+
81+
&:before {
82+
content: attr(data-label) ': ';
83+
.lib-font-size(11);
84+
}
85+
86+
.price {
87+
font-size: 1.2rem;
88+
font-weight: 700;
89+
}
90+
}
91+
}
7692
}

0 commit comments

Comments
 (0)