Skip to content

Commit cede04f

Browse files
committed
Fixed bug, when exception occurred on order with coupons cancel, made by guest
after creating of customer account.
1 parent 97b98cc commit cede04f

File tree

5 files changed

+398
-7
lines changed

5 files changed

+398
-7
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Sales\Model\Order;
9+
10+
use Magento\Sales\Api\Data\OrderInterface;
11+
use Magento\Sales\Api\OrderRepositoryInterface;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Framework\Event\ManagerInterface;
14+
15+
class CustomerAssignment
16+
{
17+
/**
18+
* @var ManagerInterface
19+
*/
20+
private $eventManager;
21+
22+
/**
23+
* @var OrderRepositoryInterface
24+
*/
25+
private $orderRepository;
26+
27+
/**
28+
* CustomerAssignment constructor.
29+
*
30+
* @param ManagerInterface $eventManager
31+
* @param OrderRepositoryInterface $orderRepository
32+
*/
33+
public function __construct(
34+
ManagerInterface $eventManager,
35+
OrderRepositoryInterface $orderRepository
36+
) {
37+
$this->eventManager = $eventManager;
38+
$this->orderRepository = $orderRepository;
39+
}
40+
41+
/**
42+
* @param OrderInterface $order
43+
* @param CustomerInterface $customer
44+
*/
45+
public function execute(OrderInterface $order, CustomerInterface $customer)/*: void*/
46+
{
47+
$order->setCustomerId($customer->getId());
48+
$order->setCustomerIsGuest(false);
49+
$this->orderRepository->save($order);
50+
51+
$this->eventManager->dispatch(
52+
'sales_order_customer_assign_after', [
53+
'order' => $order,
54+
'customer' => $customer
55+
]
56+
);
57+
}
58+
}

app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Event\Observer;
1313
use Magento\Framework\Event\ObserverInterface;
1414
use Magento\Sales\Api\OrderRepositoryInterface;
15+
use Magento\Sales\Model\Order\CustomerAssignment;
1516

1617
/**
1718
* Assign order to customer created after issuing guest order.
@@ -24,11 +25,22 @@ class AssignOrderToCustomerObserver implements ObserverInterface
2425
private $orderRepository;
2526

2627
/**
28+
* @var CustomerAssignment
29+
*/
30+
private $customerAssignmentService;
31+
32+
/**
33+
* AssignOrderToCustomerObserver constructor.
34+
*
2735
* @param OrderRepositoryInterface $orderRepository
36+
* @param CustomerAssignment $customerAssignmentService
2837
*/
29-
public function __construct(OrderRepositoryInterface $orderRepository)
30-
{
38+
public function __construct(
39+
OrderRepositoryInterface $orderRepository,
40+
CustomerAssignment $customerAssignmentService
41+
) {
3142
$this->orderRepository = $orderRepository;
43+
$this->customerAssignmentService = $customerAssignmentService;
3244
}
3345

3446
/**
@@ -44,11 +56,8 @@ public function execute(Observer $observer)
4456
if (array_key_exists('__sales_assign_order_id', $delegateData)) {
4557
$orderId = $delegateData['__sales_assign_order_id'];
4658
$order = $this->orderRepository->get($orderId);
47-
if (!$order->getCustomerId()) {
48-
//if customer ID wasn't already assigned then assigning.
49-
$order->setCustomerId($customer->getId());
50-
$order->setCustomerIsGuest(0);
51-
$this->orderRepository->save($order);
59+
if (!$order->getCustomerId() && $customer->getId()) {
60+
$this->customerAssignmentService->execute($order, $customer);
5261
}
5362
}
5463
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\SalesRule\Observer;
11+
12+
use Magento\Framework\Event\Observer;
13+
use Magento\SalesRule\Model\Coupon\UpdateCouponUsages;
14+
use Magento\Sales\Api\Data\OrderInterface;
15+
use Magento\Framework\Event\ObserverInterface;
16+
17+
class AssignCouponDataAfterOrderCustomerAssignObserver implements ObserverInterface
18+
{
19+
const EVENT_KEY_CUSTOMER = 'customer';
20+
21+
const EVENT_KEY_ORDER = 'order';
22+
23+
/**
24+
* @var UpdateCouponUsages
25+
*/
26+
private $updateCouponUsages;
27+
28+
/**
29+
* AssignCouponDataAfterOrderCustomerAssign constructor.
30+
*
31+
* @param UpdateCouponUsages $updateCouponUsages
32+
*/
33+
public function __construct(
34+
UpdateCouponUsages $updateCouponUsages
35+
) {
36+
$this->updateCouponUsages = $updateCouponUsages;
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function execute(Observer $observer)
43+
{
44+
$event = $observer->getEvent();
45+
/** @var OrderInterface $order */
46+
$order = $event->getData(self::EVENT_KEY_ORDER);
47+
48+
if ($order->getCustomerId()) {
49+
$this->updateCouponUsages->execute($order, true);
50+
}
51+
}
52+
}

app/code/Magento/SalesRule/etc/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@
2424
<event name="magento_salesrule_api_data_ruleinterface_load_after">
2525
<observer name="legacy_model_load" instance="Magento\Framework\EntityManager\Observer\AfterEntityLoad" />
2626
</event>
27+
<event name="sales_order_customer_assign_after">
28+
<observer name="sales_order_assign_customer_after" instance="Magento\SalesRule\Observer\AssignCouponDataAfterOrderCustomerAssignObserver" />
29+
</event>
2730
</config>

0 commit comments

Comments
 (0)