Skip to content

Commit 5ebf71a

Browse files
committed
ACP2E-1456: display bundle options taking into account cart tax configurations
1 parent 5d71a16 commit 5ebf71a

File tree

3 files changed

+38
-122
lines changed

3 files changed

+38
-122
lines changed

app/code/Magento/Bundle/Helper/Catalog/Product/Configuration.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
use Magento\Bundle\Model\Product\Price;
99
use Magento\Bundle\Model\Product\Type;
10+
use Magento\Bundle\Pricing\Price\TaxPrice;
1011
use Magento\Catalog\Api\Data\ProductInterface;
11-
use Magento\Catalog\Helper\Product\Configuration\ConfigurationInterface;
1212
use Magento\Catalog\Helper\Product\Configuration as ProductConfiguration;
13+
use Magento\Catalog\Helper\Product\Configuration\ConfigurationInterface;
1314
use Magento\Catalog\Model\Product;
1415
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
1516
use Magento\Framework\App\Helper\AbstractHelper;
@@ -54,7 +55,7 @@ class Configuration extends AbstractHelper implements ConfigurationInterface
5455
private $serializer;
5556

5657
/**
57-
* @var Tax
58+
* @var TaxPrice
5859
*/
5960
private $taxHelper;
6061

@@ -64,22 +65,22 @@ class Configuration extends AbstractHelper implements ConfigurationInterface
6465
* @param Data $pricingHelper
6566
* @param Escaper $escaper
6667
* @param Json|null $serializer
67-
* @param Tax|null $taxHelper
68+
* @param TaxPrice|null $taxHelper
6869
*/
6970
public function __construct(
7071
Context $context,
7172
ProductConfiguration $productConfiguration,
7273
Data $pricingHelper,
7374
Escaper $escaper,
7475
Json $serializer = null,
75-
Tax $taxHelper = null
76+
TaxPrice $taxHelper = null
7677
) {
7778
$this->productConfiguration = $productConfiguration;
7879
$this->pricingHelper = $pricingHelper;
7980
$this->escaper = $escaper;
8081
$this->serializer = $serializer ?: ObjectManager::getInstance()
8182
->get(Json::class);
82-
$this->taxHelper = $taxHelper ?? ObjectManager::getInstance()->get(Tax::class);
83+
$this->taxHelper = $taxHelper ?? ObjectManager::getInstance()->get(TaxPrice::class);
8384
parent::__construct($context);
8485
}
8586

app/code/Magento/Bundle/Helper/Catalog/Product/Tax.php renamed to app/code/Magento/Bundle/Pricing/Price/TaxPrice.php

Lines changed: 26 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,13 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\Bundle\Helper\Catalog\Product;
8+
namespace Magento\Bundle\Pricing\Price;
99

10-
use Magento\Catalog\Api\CategoryRepositoryInterface;
11-
use Magento\Catalog\Api\ProductRepositoryInterface;
1210
use Magento\Catalog\Model\Product;
13-
use Magento\Customer\Api\Data\AddressInterface;
14-
use Magento\Customer\Api\Data\AddressInterfaceFactory;
15-
use Magento\Customer\Api\Data\RegionInterfaceFactory;
1611
use Magento\Customer\Api\GroupRepositoryInterface;
17-
use Magento\Customer\Model\Address\AbstractAddress;
1812
use Magento\Customer\Model\Session as CustomerSession;
19-
use Magento\Framework\App\Helper\AbstractHelper;
20-
use Magento\Framework\App\Helper\Context;
2113
use Magento\Framework\Exception\LocalizedException;
22-
use Magento\Framework\Pricing\PriceCurrencyInterface;
14+
use Magento\Framework\Exception\NoSuchEntityException;
2315
use Magento\Store\Api\Data\StoreInterface;
2416
use Magento\Store\Model\Store;
2517
use Magento\Store\Model\StoreManagerInterface;
@@ -30,96 +22,70 @@
3022
use Magento\Tax\Api\TaxCalculationInterface;
3123
use Magento\Tax\Model\Config;
3224

33-
class Tax extends AbstractHelper
25+
/**
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
28+
*/
29+
class TaxPrice
3430
{
3531
/**
3632
* @var StoreManagerInterface
3733
*/
38-
protected $storeManager;
34+
private $storeManager;
3935

4036
/**
4137
* @var TaxClassKeyInterfaceFactory
4238
*/
43-
protected $taxClassKeyFactory;
39+
private $taxClassKeyFactory;
4440

4541
/**
4642
* @var Config
4743
*/
48-
protected $taxConfig;
44+
private $taxConfig;
4945

5046
/**
5147
* @var QuoteDetailsInterfaceFactory
5248
*/
53-
protected $quoteDetailsFactory;
49+
private $quoteDetailsFactory;
5450

5551
/**
5652
* @var QuoteDetailsItemInterfaceFactory
5753
*/
58-
protected $quoteDetailsItemFactory;
54+
private $quoteDetailsItemFactory;
5955

6056
/**
6157
* @var CustomerSession
6258
*/
63-
protected $customerSession;
59+
private $customerSession;
6460

6561
/**
6662
* @var TaxCalculationInterface
6763
*/
68-
protected $taxCalculationService;
69-
70-
/**
71-
* @var PriceCurrencyInterface
72-
*/
73-
protected $priceCurrency;
74-
75-
/**
76-
* @var ProductRepositoryInterface
77-
*/
78-
protected $productRepository;
79-
80-
/**
81-
* @var CategoryRepositoryInterface
82-
*/
83-
protected $categoryRepository;
64+
private $taxCalculationService;
8465

8566
/**
8667
* @var GroupRepositoryInterface
8768
*/
88-
protected $customerGroupRepository;
69+
private $customerGroupRepository;
8970

9071
/**
91-
* @var AddressInterfaceFactory
92-
*/
93-
protected $addressFactory;
94-
95-
/**
96-
* @var RegionInterfaceFactory
97-
*/
98-
protected $regionFactory;
99-
100-
/**
101-
* @param Context $context
10272
* @param StoreManagerInterface $storeManager
10373
* @param TaxClassKeyInterfaceFactory $taxClassKeyFactory
10474
* @param Config $taxConfig
10575
* @param QuoteDetailsInterfaceFactory $quoteDetailsFactory
10676
* @param QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory
10777
* @param TaxCalculationInterface $taxCalculationService
10878
* @param CustomerSession $customerSession
109-
* @param PriceCurrencyInterface $priceCurrency
11079
* @param GroupRepositoryInterface $customerGroupRepository
111-
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
11280
*/
11381
public function __construct(
114-
Context $context,
11582
StoreManagerInterface $storeManager,
11683
TaxClassKeyInterfaceFactory $taxClassKeyFactory,
11784
Config $taxConfig,
11885
QuoteDetailsInterfaceFactory $quoteDetailsFactory,
11986
QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory,
12087
TaxCalculationInterface $taxCalculationService,
12188
CustomerSession $customerSession,
122-
PriceCurrencyInterface $priceCurrency,
12389
GroupRepositoryInterface $customerGroupRepository
12490
) {
12591
$this->storeManager = $storeManager;
@@ -129,9 +95,7 @@ public function __construct(
12995
$this->quoteDetailsItemFactory = $quoteDetailsItemFactory;
13096
$this->taxCalculationService = $taxCalculationService;
13197
$this->customerSession = $customerSession;
132-
$this->priceCurrency = $priceCurrency;
13398
$this->customerGroupRepository = $customerGroupRepository;
134-
parent::__construct($context);
13599
}
136100

137101
/**
@@ -140,53 +104,36 @@ public function __construct(
140104
* @param Product $product
141105
* @param float $price
142106
* @param bool|null $includingTax
143-
* @param AbstractAddress|null $shippingAddress
144-
* @param AbstractAddress|null $billingAddress
145107
* @param int|null $ctc
146108
* @param Store|bool|int|string|null $store
147109
* @param bool|null $priceIncludesTax
148-
* @param bool $roundPrice
149110
* @return float
150111
* @throws LocalizedException
151-
* @throws \Magento\Framework\Exception\NoSuchEntityException
112+
* @throws NoSuchEntityException
152113
*/
153114
public function getTaxPrice(
154115
Product $product,
155116
float $price,
156117
bool $includingTax = null,
157-
AbstractAddress $shippingAddress = null,
158-
AbstractAddress $billingAddress = null,
159118
int $ctc = null,
160119
Store|bool|int|string $store = null,
161-
bool $priceIncludesTax = null,
162-
bool $roundPrice = true
120+
bool $priceIncludesTax = null
163121
): float {
164122
if (!$price) {
165123
return $price;
166124
}
167125

168126
$store = $this->storeManager->getStore($store);
127+
$storeId = $store?->getId();
128+
$taxClassKey = $this->taxClassKeyFactory->create();
129+
$customerTaxClassKey = $this->taxClassKeyFactory->create();
130+
$item = $this->quoteDetailsItemFactory->create();
131+
$quoteDetails = $this->quoteDetailsFactory->create();
132+
169133
if ($priceIncludesTax === null) {
170134
$priceIncludesTax = $this->taxConfig->priceIncludesTax($store);
171135
}
172136

173-
$shippingAddressDataObject = null;
174-
if ($shippingAddress === null) {
175-
$shippingAddressDataObject =
176-
$this->convertDefaultTaxAddress($this->customerSession->getDefaultTaxShippingAddress());
177-
} elseif ($shippingAddress instanceof AbstractAddress) {
178-
$shippingAddressDataObject = $shippingAddress->getDataModel();
179-
}
180-
181-
$billingAddressDataObject = null;
182-
if ($billingAddress === null) {
183-
$billingAddressDataObject =
184-
$this->convertDefaultTaxAddress($this->customerSession->getDefaultTaxBillingAddress());
185-
} elseif ($billingAddress instanceof AbstractAddress) {
186-
$billingAddressDataObject = $billingAddress->getDataModel();
187-
}
188-
189-
$taxClassKey = $this->taxClassKeyFactory->create();
190137
$taxClassKey->setType(TaxClassKeyInterface::TYPE_ID)
191138
->setValue($product->getTaxClassId());
192139

@@ -195,11 +142,9 @@ public function getTaxPrice(
195142
->getTaxClassId();
196143
}
197144

198-
$customerTaxClassKey = $this->taxClassKeyFactory->create();
199145
$customerTaxClassKey->setType(TaxClassKeyInterface::TYPE_ID)
200146
->setValue($ctc);
201147

202-
$item = $this->quoteDetailsItemFactory->create();
203148
$item->setQuantity(1)
204149
->setCode($product->getSku())
205150
->setShortDescription($product->getShortDescription())
@@ -208,16 +153,12 @@ public function getTaxPrice(
208153
->setType('product')
209154
->setUnitPrice($price);
210155

211-
$quoteDetails = $this->quoteDetailsFactory->create();
212-
$quoteDetails->setShippingAddress($shippingAddressDataObject)
213-
->setBillingAddress($billingAddressDataObject)
156+
$quoteDetails
214157
->setCustomerTaxClassKey($customerTaxClassKey)
215158
->setItems([$item])
216159
->setCustomerId($this->customerSession->getCustomerId());
217160

218-
$storeId = null;
219-
$storeId = $store?->getId();
220-
$taxDetails = $this->taxCalculationService->calculateTax($quoteDetails, $storeId, $roundPrice);
161+
$taxDetails = $this->taxCalculationService->calculateTax($quoteDetails, $storeId);
221162
$items = $taxDetails->getItems();
222163
$taxDetailsItem = array_shift($items);
223164

@@ -233,11 +174,7 @@ public function getTaxPrice(
233174
$taxDetailsItem->getPrice() : $taxDetailsItem->getPriceInclTax();
234175
}
235176

236-
if ($roundPrice) {
237-
return $this->priceCurrency->round($price);
238-
} else {
239-
return $price;
240-
}
177+
return $price;
241178
}
242179

243180
/**
@@ -250,26 +187,4 @@ public function displayCartPricesBoth(StoreInterface $store = null): bool
250187
{
251188
return $this->taxConfig->displayCartPricesBoth($store);
252189
}
253-
254-
/**
255-
* Convert tax address array to address data object with country id and postcode
256-
*
257-
* @param array|null $taxAddress
258-
* @return AddressInterface|null
259-
*/
260-
private function convertDefaultTaxAddress(array $taxAddress = null)
261-
{
262-
if (empty($taxAddress)) {
263-
return null;
264-
}
265-
/** @var AddressInterface $addressDataObject */
266-
$addressDataObject = $this->addressFactory->create()
267-
->setCountryId($taxAddress['country_id'])
268-
->setPostcode($taxAddress['postcode']);
269-
270-
if (isset($taxAddress['region_id'])) {
271-
$addressDataObject->setRegion($this->regionFactory->create()->setRegionId($taxAddress['region_id']));
272-
}
273-
return $addressDataObject;
274-
}
275190
}

app/code/Magento/Bundle/Test/Unit/Helper/Catalog/Product/ConfigurationTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace Magento\Bundle\Test\Unit\Helper\Catalog\Product;
99

10-
use Magento\Bundle\Helper\Catalog\Product\Tax;
1110
use Magento\Bundle\Model\Product\Price;
1211
use Magento\Bundle\Model\Product\Type;
1312
use Magento\Bundle\Model\ResourceModel\Option\Collection;
13+
use Magento\Bundle\Pricing\Price\TaxPrice;
1414
use Magento\Catalog\Helper\Product\Configuration;
1515
use Magento\Catalog\Model\Product;
1616
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
@@ -59,7 +59,7 @@ class ConfigurationTest extends TestCase
5959
private $serializer;
6060

6161
/**
62-
* @var Tax|MockObject
62+
* @var TaxPrice|MockObject
6363
*/
6464
private $taxHelper;
6565

@@ -78,7 +78,7 @@ protected function setUp(): void
7878
$this->serializer = $this->getMockBuilder(Json::class)
7979
->onlyMethods(['unserialize'])
8080
->getMockForAbstractClass();
81-
$this->taxHelper = $this->createPartialMock(Tax::class, ['displayCartPricesBoth', 'getTaxPrice']);
81+
$this->taxHelper = $this->createPartialMock(TaxPrice::class, ['displayCartPricesBoth', 'getTaxPrice']);
8282

8383
$this->serializer->expects($this->any())
8484
->method('unserialize')
@@ -272,13 +272,13 @@ public function testGetOptions(): void
272272
$this->taxHelper->expects($this->any())
273273
->method('getTaxPrice')
274274
->with($product, 15)
275-
->willReturn(15);
275+
->willReturn(15.00);
276276
$this->taxHelper->expects($this->any())
277277
->method('displayCartPricesBoth')
278278
->willReturn(false);
279-
$this->pricingHelper->expects($this->once())->method('currency')->with(15)
279+
$this->pricingHelper->expects($this->once())->method('currency')->with(15.00)
280280
->willReturn('<span class="price">$15.00</span>');
281-
$priceModel->expects($this->once())->method('getSelectionFinalTotalPrice')->willReturn(15);
281+
$priceModel->expects($this->once())->method('getSelectionFinalTotalPrice')->willReturn(15.00);
282282
$selectionQty->expects($this->any())->method('getValue')->willReturn(1);
283283
$bundleOption->expects($this->any())->method('getSelections')->willReturn([$product]);
284284
$bundleOption->expects($this->once())->method('getTitle')->willReturn('title');

0 commit comments

Comments
 (0)