Skip to content

Commit 6d41441

Browse files
committed
Merge branch 'ACP2E-2905-2' of https://github.com/adobe-commerce-tier-4/magento2ce into 04-29-24-Tier4-Bugfix-Delivery
2 parents 440162d + deb133a commit 6d41441

File tree

3 files changed

+134
-57
lines changed

3 files changed

+134
-57
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Backend/GroupPrice/AbstractGroupPrice.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ public function preparePriceData(array $priceData, $productTypeId, $websiteId)
296296
} elseif ($v['website_id'] == 0 && !isset($data[$key])) {
297297
$data[$key] = $v;
298298
$data[$key]['website_id'] = $websiteId;
299-
if ($this->_isPriceFixed($price)) {
299+
if ($this->_isPriceFixed($price) &&
300+
$this->isPercentageValue($data[$key])
301+
) {
300302
$data[$key]['price'] = $v['price'] * $rates[$websiteId]['rate'];
301303
$data[$key]['website_price'] = $v['price'] * $rates[$websiteId]['rate'];
302304
}
@@ -466,4 +468,15 @@ private function getMetadataPool()
466468
}
467469
return $this->metadataPool;
468470
}
471+
472+
/**
473+
* Check if data consists of percentage value
474+
*
475+
* @param array $data
476+
* @return bool
477+
*/
478+
private function isPercentageValue(array $data): bool
479+
{
480+
return (array_key_exists('percentage_value', $data) && $data['percentage_value'] === null);
481+
}
469482
}

app/code/Magento/Checkout/Model/Plugin/RecollectQuoteOnCustomerGroupChange.php

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
namespace Magento\Checkout\Model\Plugin;
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Model\Config\Share;
1112
use Magento\Customer\Model\Customer;
1213
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\App\ObjectManager;
1316
use Magento\Framework\Exception\LocalizedException;
1417
use Magento\Framework\Exception\NoSuchEntityException;
1518
use Magento\Framework\Model\AbstractModel;
1619
use Magento\Quote\Api\CartRepositoryInterface;
1720
use Magento\Quote\Model\Quote;
21+
use Magento\Store\Model\StoreManagerInterface;
1822

1923
/**
2024
* Recollect quote when customer group updated through API
@@ -31,18 +35,36 @@ class RecollectQuoteOnCustomerGroupChange
3135
*/
3236
private $customerRepository;
3337

38+
/**
39+
* @var ScopeConfigInterface
40+
*/
41+
private $scopeConfig;
42+
43+
/**
44+
* @var StoreManagerInterface $storeManager
45+
*/
46+
private $storeManager;
47+
3448
/**
3549
* Initialize Constructor
3650
*
3751
* @param CartRepositoryInterface $cartRepository
3852
* @param CustomerRepositoryInterface $customerRepository
53+
* @param ScopeConfigInterface|null $scopeConfig
54+
* @param StoreManagerInterface|null $storeManager
3955
*/
4056
public function __construct(
4157
CartRepositoryInterface $cartRepository,
42-
CustomerRepositoryInterface $customerRepository
58+
CustomerRepositoryInterface $customerRepository,
59+
?ScopeConfigInterface $scopeConfig = null,
60+
?StoreManagerInterface $storeManager = null
4361
) {
4462
$this->cartRepository = $cartRepository;
4563
$this->customerRepository = $customerRepository;
64+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()
65+
->get(ScopeConfigInterface::class);
66+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
67+
->get(StoreManagerInterface::class);
4668
}
4769

4870
/**
@@ -82,11 +104,19 @@ public function aroundSave(
82104
&& empty($previousCustomerData['taxvat'])
83105
) {
84106
try {
85-
/** @var Quote $quote */
86-
$quote = $this->cartRepository->getActiveForCustomer($customer->getId());
87-
$quote->setCustomerGroupId($customer->getGroupId());
88-
$quote->collectTotals();
89-
$this->cartRepository->save($quote);
107+
$customerAccountShareScope = $this->scopeConfig->getValue(
108+
Share::XML_PATH_CUSTOMER_ACCOUNT_SHARE,
109+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
110+
);
111+
if ($customerAccountShareScope === Share::SHARE_WEBSITE) {
112+
/** @var Quote $quote */
113+
$quote = $this->cartRepository->getActiveForCustomer($customer->getId());
114+
$quote->setCustomerGroupId($customer->getGroupId());
115+
$quote->collectTotals();
116+
$this->cartRepository->save($quote);
117+
} else {
118+
$this->collectTotalsForCustomerGlobalScope($customer);
119+
}
90120
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
91121
} catch (NoSuchEntityException $e) {
92122
//no active cart for customer
@@ -95,4 +125,25 @@ public function aroundSave(
95125

96126
return $result;
97127
}
128+
129+
/**
130+
* Re-collect totals for customer share account global scope
131+
*
132+
* @param Customer|AbstractModel $customer
133+
* @return void
134+
* @throws NoSuchEntityException
135+
*/
136+
private function collectTotalsForCustomerGlobalScope(Customer|AbstractModel $customer): void
137+
{
138+
$allStores = $this->storeManager->getStores();
139+
foreach ($allStores as $store) {
140+
/** @var Quote $quote */
141+
$quote = $this->cartRepository->getActiveForCustomer($customer->getId(), [$store->getId()]);
142+
if ($quote) {
143+
$quote->setCustomerGroupId($customer->getGroupId());
144+
$quote->collectTotals();
145+
$this->cartRepository->save($quote);
146+
}
147+
}
148+
}
98149
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Attribute/Backend/TierpriceTest.php

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,64 @@
88
namespace Magento\Catalog\Model\Product\Attribute\Backend;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\Catalog\Model\Product\Type;
14+
use Magento\Catalog\Model\ProductRepository;
15+
use Magento\Framework\DataObject;
16+
use Magento\Framework\EntityManager\MetadataPool;
17+
use Magento\Framework\Exception\CouldNotSaveException;
18+
use Magento\Framework\Exception\InputException;
19+
use Magento\Framework\Exception\LocalizedException;
20+
use Magento\Framework\Exception\NoSuchEntityException;
21+
use Magento\Framework\Exception\StateException;
22+
use Magento\TestFramework\Helper\Bootstrap;
23+
use PHPUnit\Framework\TestCase;
1124

1225
/**
1326
* Test class for \Magento\Catalog\Model\Product\Attribute\Backend\Tierprice.
1427
*
1528
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
1629
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1730
*/
18-
class TierpriceTest extends \PHPUnit\Framework\TestCase
31+
class TierpriceTest extends TestCase
1932
{
2033
/**
21-
* @var \Magento\Framework\EntityManager\MetadataPool
34+
* @var MetadataPool
2235
*/
2336
protected $metadataPool;
2437

2538
/**
26-
* @var \Magento\Catalog\Model\ProductRepository
39+
* @var ProductRepository
2740
*/
2841
protected $productRepository;
2942

3043
/**
31-
* @var \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory
44+
* @var ProductTierPriceInterfaceFactory
3245
*/
3346
private $tierPriceFactory;
3447

3548
/**
36-
* @var \Magento\Catalog\Model\Product\Attribute\Backend\Tierprice
49+
* @var Tierprice
3750
*/
3851
protected $_model;
3952

4053
protected function setUp(): void
4154
{
42-
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
43-
\Magento\Catalog\Model\Product\Attribute\Backend\Tierprice::class
55+
$this->_model = Bootstrap::getObjectManager()->create(
56+
Tierprice::class
4457
);
45-
$this->productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
46-
\Magento\Catalog\Model\ProductRepository::class
58+
$this->productRepository = Bootstrap::getObjectManager()->create(
59+
ProductRepository::class
4760
);
48-
$this->metadataPool = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
49-
\Magento\Framework\EntityManager\MetadataPool::class
61+
$this->metadataPool = Bootstrap::getObjectManager()->create(
62+
MetadataPool::class
5063
);
51-
$this->tierPriceFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
52-
->create(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class);
64+
$this->tierPriceFactory = Bootstrap::getObjectManager()
65+
->create(ProductTierPriceInterfaceFactory::class);
5366

5467
$this->_model->setAttribute(
55-
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
68+
Bootstrap::getObjectManager()->get(
5669
\Magento\Eav\Model\Config::class
5770
)->getAttribute(
5871
'catalog_product',
@@ -63,7 +76,7 @@ protected function setUp(): void
6376

6477
public function testValidate()
6578
{
66-
$product = new \Magento\Framework\DataObject();
79+
$product = new DataObject();
6780
$product->setTierPrice(
6881
[
6982
['website_id' => 0, 'cust_group' => 1, 'price_qty' => 2, 'price' => 8],
@@ -81,9 +94,9 @@ public function testValidate()
8194
*/
8295
public function testValidateDuplicate(array $tierPricesData)
8396
{
84-
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
97+
$this->expectException(LocalizedException::class);
8598

86-
$product = new \Magento\Framework\DataObject();
99+
$product = new DataObject();
87100
$product->setTierPrice($tierPricesData);
88101

89102
$this->_model->validate($product);
@@ -116,9 +129,9 @@ public function validateDuplicateDataProvider(): array
116129
*/
117130
public function testValidateDuplicateWebsite()
118131
{
119-
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
132+
$this->expectException(LocalizedException::class);
120133

121-
$product = new \Magento\Framework\DataObject();
134+
$product = new DataObject();
122135
$product->setTierPrice(
123136
[
124137
['website_id' => 0, 'cust_group' => 1, 'price_qty' => 2.2, 'price' => 8],
@@ -134,9 +147,9 @@ public function testValidateDuplicateWebsite()
134147
*/
135148
public function testValidatePercentage()
136149
{
137-
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
150+
$this->expectException(LocalizedException::class);
138151

139-
$product = new \Magento\Framework\DataObject();
152+
$product = new DataObject();
140153
$product->setTierPrice(
141154
[
142155
['website_id' => 0, 'cust_group' => 1, 'price_qty' => 2, 'percentage_value' => 101],
@@ -149,15 +162,15 @@ public function testValidatePercentage()
149162
public function testPreparePriceData()
150163
{
151164
$data = [
152-
['website_id' => 0, 'cust_group' => 1, 'price_qty' => 2, 'price' => 8],
153-
['website_id' => 0, 'cust_group' => 1, 'price_qty' => 5, 'price' => 5],
154-
['website_id' => 1, 'cust_group' => 1, 'price_qty' => 5, 'price' => 5],
155-
['website_id' => 1, 'cust_group' => 1, 'price_qty' => 5.3, 'price' => 4],
156-
['website_id' => 1, 'cust_group' => 1, 'price_qty' => 5.4, 'price' => 3],
157-
['website_id' => 1, 'cust_group' => 1, 'price_qty' => '5.40', 'price' => 2],
165+
['website_id' => 0, 'cust_group' => 1, 'price_qty' => 2, 'price' => 8, 'percentage_value' => 10],
166+
['website_id' => 0, 'cust_group' => 1, 'price_qty' => 5, 'price' => 5, 'percentage_value' => null],
167+
['website_id' => 1, 'cust_group' => 1, 'price_qty' => 5, 'price' => 5, 'percentage_value' => 40],
168+
['website_id' => 1, 'cust_group' => 1, 'price_qty' => 5.3, 'price' => 4, 'percentage_value' => 10],
169+
['website_id' => 1, 'cust_group' => 1, 'price_qty' => 5.4, 'price' => 3, 'percentage_value' => 50],
170+
['website_id' => 1, 'cust_group' => 1, 'price_qty' => '5.40', 'price' => 2, 'percentage_value' => null],
158171
];
159172

160-
$newData = $this->_model->preparePriceData($data, \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE, 1);
173+
$newData = $this->_model->preparePriceData($data, Type::TYPE_SIMPLE, 1);
161174
$this->assertCount(4, $newData);
162175
$this->assertArrayHasKey('1-2', $newData);
163176
$this->assertArrayHasKey('1-5', $newData);
@@ -167,9 +180,9 @@ public function testPreparePriceData()
167180

168181
public function testAfterLoad()
169182
{
170-
/** @var $product \Magento\Catalog\Model\Product */
171-
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
172-
\Magento\Catalog\Model\Product::class
183+
/** @var $product Product */
184+
$product = Bootstrap::getObjectManager()->create(
185+
Product::class
173186
);
174187
$fixtureProduct = $this->productRepository->get('simple');
175188
$product->setId($fixtureProduct->getId());
@@ -185,14 +198,14 @@ public function testAfterLoad()
185198
* @dataProvider saveExistingProductDataProvider
186199
* @param array $tierPricesData
187200
* @param int $tierPriceCount
188-
* @throws \Magento\Framework\Exception\CouldNotSaveException
189-
* @throws \Magento\Framework\Exception\InputException
190-
* @throws \Magento\Framework\Exception\NoSuchEntityException
191-
* @throws \Magento\Framework\Exception\StateException
201+
* @throws CouldNotSaveException
202+
* @throws InputException
203+
* @throws NoSuchEntityException
204+
* @throws StateException
192205
*/
193206
public function testSaveExistingProduct(array $tierPricesData, int $tierPriceCount): void
194207
{
195-
/** @var $product \Magento\Catalog\Model\Product */
208+
/** @var $product Product */
196209
$product = $this->productRepository->get('simple', true);
197210
$tierPrices = [];
198211
foreach ($tierPricesData as $tierPrice) {
@@ -222,7 +235,7 @@ public function saveExistingProductDataProvider(): array
222235
'website_id' => 0,
223236
'customer_group_id' => 0,
224237
'qty' => 10,
225-
'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
238+
'extension_attributes' => new DataObject(['percentage_value' => 50])
226239
],
227240
],
228241
5,
@@ -237,7 +250,7 @@ public function saveExistingProductDataProvider(): array
237250
'website_id' => 0,
238251
'customer_group_id' => 0,
239252
'qty' => 10,
240-
'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 10])
253+
'extension_attributes' => new DataObject(['percentage_value' => 10])
241254
],
242255
],
243256
5,
@@ -251,7 +264,7 @@ public function saveExistingProductDataProvider(): array
251264
'website_id' => 0,
252265
'customer_group_id' => 0,
253266
'qty' => 10,
254-
'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
267+
'extension_attributes' => new DataObject(['percentage_value' => 50])
255268
],
256269
],
257270
4,
@@ -266,13 +279,13 @@ public function saveExistingProductDataProvider(): array
266279
'website_id' => 0,
267280
'customer_group_id' => 32000,
268281
'qty' => 20,
269-
'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 90])
282+
'extension_attributes' => new DataObject(['percentage_value' => 90])
270283
],
271284
[
272285
'website_id' => 0,
273286
'customer_group_id' => 0,
274287
'qty' => 10,
275-
'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
288+
'extension_attributes' => new DataObject(['percentage_value' => 50])
276289
],
277290
],
278291
6,
@@ -285,18 +298,18 @@ public function saveExistingProductDataProvider(): array
285298
* @dataProvider saveNewProductDataProvider
286299
* @param array $tierPricesData
287300
* @param int $tierPriceCount
288-
* @throws \Magento\Framework\Exception\CouldNotSaveException
289-
* @throws \Magento\Framework\Exception\InputException
290-
* @throws \Magento\Framework\Exception\LocalizedException
291-
* @throws \Magento\Framework\Exception\StateException
301+
* @throws CouldNotSaveException
302+
* @throws InputException
303+
* @throws LocalizedException
304+
* @throws StateException
292305
*/
293306
public function testSaveNewProduct(array $tierPricesData, int $tierPriceCount): void
294307
{
295-
/** @var $product \Magento\Catalog\Model\Product */
296-
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
297-
->create(\Magento\Catalog\Model\Product::class);
308+
/** @var $product Product */
309+
$product = Bootstrap::getObjectManager()
310+
->create(Product::class);
298311
$product->isObjectNew(true);
299-
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
312+
$product->setTypeId(Type::TYPE_SIMPLE)
300313
->setAttributeSetId(4)
301314
->setName('Simple Product New')
302315
->setSku('simple product new')
@@ -330,7 +343,7 @@ public function saveNewProductDataProvider(): array
330343
'website_id' => 0,
331344
'customer_group_id' => 0,
332345
'qty' => 10,
333-
'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
346+
'extension_attributes' => new DataObject(['percentage_value' => 50])
334347
],
335348
],
336349
6,

0 commit comments

Comments
 (0)