Skip to content

Commit f3e2166

Browse files
authored
Merge pull request #7254 from magento-l3/PR-2-2021-25-11
PR-2-2021-25-11
2 parents 62d8061 + 71512f3 commit f3e2166

File tree

33 files changed

+982
-74
lines changed

33 files changed

+982
-74
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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;
9+
10+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
use Magento\Eav\Model\ReservedAttributeCheckerInterface;
12+
13+
/**
14+
* Adapter for \Magento\Catalog\Model\Product\ReservedAttributeList
15+
*
16+
* Is created to implement proper interface and to use api class ReservedAttributeList
17+
* while keeping it backward compatible
18+
* @see \Magento\Catalog\Model\Product\ReservedAttributeList
19+
*/
20+
class ReservedAttributeCheckerAdapter implements ReservedAttributeCheckerInterface
21+
{
22+
/**
23+
* @var ReservedAttributeList
24+
*/
25+
private $reservedAttributeList;
26+
27+
/**
28+
* @param ReservedAttributeList $reservedAttributeList
29+
*/
30+
public function __construct(
31+
ReservedAttributeList $reservedAttributeList
32+
) {
33+
$this->reservedAttributeList = $reservedAttributeList;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function isReservedAttribute(AbstractAttribute $attribute): bool
40+
{
41+
return $this->reservedAttributeList->isReservedAttribute($attribute);
42+
}
43+
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<preference for="Magento\Catalog\Model\Indexer\Product\Price\UpdateIndexInterface" type="Magento\Catalog\Model\Indexer\Product\Price\InvalidateIndex" />
7474
<preference for="Magento\Catalog\Model\Product\Gallery\ImagesConfigFactoryInterface" type="Magento\Catalog\Model\Product\Gallery\ImagesConfigFactory" />
7575
<preference for="Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface" type="Magento\Catalog\Model\Product\Configuration\Item\ItemResolverComposite" />
76-
<preference for="Magento\Catalog\Api\Data\MassActionInterface" type="\Magento\Catalog\Model\MassAction" />
76+
<preference for="Magento\Catalog\Api\Data\MassActionInterface" type="Magento\Catalog\Model\MassAction" />
7777
<preference for="Magento\Catalog\Model\ProductLink\Data\ListCriteriaInterface" type="Magento\Catalog\Model\ProductLink\Data\ListCriteria" />
7878
<preference for="Magento\Catalog\Api\CategoryListDeleteBySkuInterface" type="Magento\Catalog\Model\CategoryLinkRepository"/>
7979
<type name="Magento\Customer\Model\ResourceModel\Visitor">
@@ -1333,4 +1333,13 @@
13331333
<argument name="imageResizeScheduler" xsi:type="object">Magento\MediaStorage\Service\ImageResizeScheduler\Proxy</argument>
13341334
</arguments>
13351335
</type>
1336+
<type name="Magento\Eav\Model\ReservedAttributeChecker">
1337+
<arguments>
1338+
<argument name="validators" xsi:type="array">
1339+
<item name="catalog_product" xsi:type="array">
1340+
<item name="product_reserved_attribute_codes" xsi:type="object">\Magento\Catalog\Model\Product\ReservedAttributeCheckerAdapter</item>
1341+
</item>
1342+
</argument>
1343+
</arguments>
1344+
</type>
13361345
</config>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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\Checkout\Model\Plugin;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Model\Customer;
12+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\Model\AbstractModel;
16+
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Quote\Model\Quote;
18+
19+
/**
20+
* Recollect quote when customer group updated through API
21+
*/
22+
class RecollectQuoteOnCustomerGroupChange
23+
{
24+
/**
25+
* @var CartRepositoryInterface
26+
*/
27+
private $cartRepository;
28+
29+
/**
30+
* @var CustomerRepositoryInterface
31+
*/
32+
private $customerRepository;
33+
34+
/**
35+
* Initialize Constructor
36+
*
37+
* @param CartRepositoryInterface $cartRepository
38+
* @param CustomerRepositoryInterface $customerRepository
39+
*/
40+
public function __construct(
41+
CartRepositoryInterface $cartRepository,
42+
CustomerRepositoryInterface $customerRepository
43+
) {
44+
$this->cartRepository = $cartRepository;
45+
$this->customerRepository = $customerRepository;
46+
}
47+
48+
/**
49+
* Plugin around create customer that triggers to update and recollect all customer cart
50+
*
51+
* @param CustomerResource $subject
52+
* @param callable $proceed
53+
* @param AbstractModel $customer
54+
* @return CustomerResource
55+
*
56+
* @throws LocalizedException
57+
* @throws NoSuchEntityException
58+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
59+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
60+
*/
61+
public function aroundSave(
62+
CustomerResource $subject,
63+
callable $proceed,
64+
AbstractModel $customer
65+
): CustomerResource {
66+
$customerId = $customer->getId() ?: $customer->getEntityId();
67+
/** @var Customer $customer */
68+
if ($customerId && empty($customer->getTaxvat())) {
69+
try {
70+
$prevCustomerData = $this->customerRepository->getById($customerId);
71+
$previousCustomerData = $prevCustomerData->__toArray();
72+
} catch (NoSuchEntityException $e) {
73+
$previousCustomerData = [];
74+
}
75+
}
76+
77+
$result = $proceed($customer);
78+
79+
if (!empty($previousCustomerData)
80+
&& $previousCustomerData['group_id'] !== null
81+
&& $previousCustomerData['group_id'] != $customer->getGroupId()
82+
&& empty($previousCustomerData['taxvat'])
83+
) {
84+
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);
90+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
91+
} catch (NoSuchEntityException $e) {
92+
//no active cart for customer
93+
}
94+
}
95+
96+
return $result;
97+
}
98+
}

app/code/Magento/Checkout/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@
5353
type="Magento\Checkout\Model\CaptchaPaymentProcessingRateLimiter" />
5454
<preference for="Magento\Checkout\Api\PaymentSavingRateLimiterInterface"
5555
type="Magento\Checkout\Model\CaptchaPaymentSavingRateLimiter" />
56+
<type name="Magento\Customer\Model\ResourceModel\Customer">
57+
<plugin name="recollect_quote_on_customer_group_change" type="Magento\Checkout\Model\Plugin\RecollectQuoteOnCustomerGroupChange"/>
58+
</type>
5659
</config>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Eav\Model;
9+
10+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
use Magento\Framework\Exception\LocalizedException;
12+
13+
/**
14+
* Composite Reserved Attribute Checker
15+
*
16+
* Iterates through individual Reserved Attribute Checkers to check whether attribute is reserved by system
17+
*/
18+
class ReservedAttributeChecker implements ReservedAttributeCheckerInterface
19+
{
20+
/**
21+
* @var ReservedAttributeCheckerInterface[][]
22+
*/
23+
private $validators;
24+
25+
/**
26+
* @param array $validators
27+
*/
28+
public function __construct(
29+
array $validators = []
30+
) {
31+
$this->validators = $validators;
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function isReservedAttribute(AbstractAttribute $attribute): bool
38+
{
39+
$isReserved = false;
40+
$validators = $this->validators[$attribute->getEntityType()->getEntityTypeCode()] ?? [];
41+
foreach ($validators as $validator) {
42+
$isReserved = $validator->isReservedAttribute($attribute);
43+
if ($isReserved === true) {
44+
break;
45+
}
46+
}
47+
48+
return $isReserved;
49+
}
50+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Eav\Model;
9+
10+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
12+
/**
13+
* Checks whether attribute is reserved by system
14+
*/
15+
interface ReservedAttributeCheckerInterface
16+
{
17+
/**
18+
* Check whether attribute is reserved by system.
19+
*
20+
* Check that given user defined EAV attribute doesn't contain the attribute code
21+
* that matches to a getter field related to related model (e.g. product, category, customer...)
22+
*
23+
* @param AbstractAttribute $attribute
24+
* @return bool
25+
*/
26+
public function isReservedAttribute(AbstractAttribute $attribute): bool;
27+
}

0 commit comments

Comments
 (0)