|
| 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\Quote\Plugin; |
| 9 | + |
| 10 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 11 | +use Magento\Customer\Model\ResourceModel\Customer as CustomerResource; |
| 12 | +use Magento\Customer\Model\Customer; |
| 13 | + |
| 14 | +/** |
| 15 | + * Recollect quote totals after change customer group |
| 16 | + */ |
| 17 | +class RecollectOnGroupChange |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var CartRepositoryInterface |
| 21 | + */ |
| 22 | + private $cartRepository; |
| 23 | + |
| 24 | + /** |
| 25 | + * @param CartRepositoryInterface $cartRepository |
| 26 | + */ |
| 27 | + public function __construct( |
| 28 | + CartRepositoryInterface $cartRepository |
| 29 | + ) { |
| 30 | + $this->cartRepository = $cartRepository; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Recollect totals if customer group change |
| 35 | + * |
| 36 | + * @param CustomerResource $subject |
| 37 | + * @param CustomerResource $result |
| 38 | + * @param Customer $customer |
| 39 | + * @return CustomerResource |
| 40 | + * |
| 41 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 42 | + */ |
| 43 | + public function afterSave(CustomerResource $subject, CustomerResource $result, Customer $customer) |
| 44 | + { |
| 45 | + if ($customer->getOrigData('group_id') !== null |
| 46 | + && $customer->getOrigData('group_id') != $customer->getGroupId() |
| 47 | + ) { |
| 48 | + try { |
| 49 | + /** @var \Magento\Quote\Model\Quote $quote */ |
| 50 | + $quote = $this->cartRepository->getActiveForCustomer($customer->getId()); |
| 51 | + $quote->setCustomerGroupId($customer->getGroupId()); |
| 52 | + $quote->collectTotals(); |
| 53 | + $this->cartRepository->save($quote); |
| 54 | + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { |
| 55 | + //no active cart for customer |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return $result; |
| 60 | + } |
| 61 | +} |
0 commit comments