|
| 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 | +} |
0 commit comments