Skip to content

Commit

Permalink
Fixed bug, when exception occurred on order with coupons cancel, made…
Browse files Browse the repository at this point in the history
… by guest

after creating of customer account.
  • Loading branch information
Winfle committed Nov 27, 2018
1 parent 97b98cc commit cede04f
Show file tree
Hide file tree
Showing 5 changed files with 398 additions and 7 deletions.
58 changes: 58 additions & 0 deletions app/code/Magento/Sales/Model/Order/CustomerAssignment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Model\Order;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Event\ManagerInterface;

class CustomerAssignment
{
/**
* @var ManagerInterface
*/
private $eventManager;

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* CustomerAssignment constructor.
*
* @param ManagerInterface $eventManager
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(
ManagerInterface $eventManager,
OrderRepositoryInterface $orderRepository
) {
$this->eventManager = $eventManager;
$this->orderRepository = $orderRepository;
}

/**
* @param OrderInterface $order
* @param CustomerInterface $customer
*/
public function execute(OrderInterface $order, CustomerInterface $customer)/*: void*/
{
$order->setCustomerId($customer->getId());
$order->setCustomerIsGuest(false);
$this->orderRepository->save($order);

$this->eventManager->dispatch(
'sales_order_customer_assign_after', [
'order' => $order,
'customer' => $customer
]
);
}
}
23 changes: 16 additions & 7 deletions app/code/Magento/Sales/Observer/AssignOrderToCustomerObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\CustomerAssignment;

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

/**
* @var CustomerAssignment
*/
private $customerAssignmentService;

/**
* AssignOrderToCustomerObserver constructor.
*
* @param OrderRepositoryInterface $orderRepository
* @param CustomerAssignment $customerAssignmentService
*/
public function __construct(OrderRepositoryInterface $orderRepository)
{
public function __construct(
OrderRepositoryInterface $orderRepository,
CustomerAssignment $customerAssignmentService
) {
$this->orderRepository = $orderRepository;
$this->customerAssignmentService = $customerAssignmentService;
}

/**
Expand All @@ -44,11 +56,8 @@ public function execute(Observer $observer)
if (array_key_exists('__sales_assign_order_id', $delegateData)) {
$orderId = $delegateData['__sales_assign_order_id'];
$order = $this->orderRepository->get($orderId);
if (!$order->getCustomerId()) {
//if customer ID wasn't already assigned then assigning.
$order->setCustomerId($customer->getId());
$order->setCustomerIsGuest(0);
$this->orderRepository->save($order);
if (!$order->getCustomerId() && $customer->getId()) {
$this->customerAssignmentService->execute($order, $customer);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\SalesRule\Observer;

use Magento\Framework\Event\Observer;
use Magento\SalesRule\Model\Coupon\UpdateCouponUsages;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Framework\Event\ObserverInterface;

class AssignCouponDataAfterOrderCustomerAssignObserver implements ObserverInterface
{
const EVENT_KEY_CUSTOMER = 'customer';

const EVENT_KEY_ORDER = 'order';

/**
* @var UpdateCouponUsages
*/
private $updateCouponUsages;

/**
* AssignCouponDataAfterOrderCustomerAssign constructor.
*
* @param UpdateCouponUsages $updateCouponUsages
*/
public function __construct(
UpdateCouponUsages $updateCouponUsages
) {
$this->updateCouponUsages = $updateCouponUsages;
}

/**
* @inheritDoc
*/
public function execute(Observer $observer)
{
$event = $observer->getEvent();
/** @var OrderInterface $order */
$order = $event->getData(self::EVENT_KEY_ORDER);

if ($order->getCustomerId()) {
$this->updateCouponUsages->execute($order, true);
}
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/SalesRule/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
<event name="magento_salesrule_api_data_ruleinterface_load_after">
<observer name="legacy_model_load" instance="Magento\Framework\EntityManager\Observer\AfterEntityLoad" />
</event>
<event name="sales_order_customer_assign_after">
<observer name="sales_order_assign_customer_after" instance="Magento\SalesRule\Observer\AssignCouponDataAfterOrderCustomerAssignObserver" />
</event>
</config>
Loading

0 comments on commit cede04f

Please sign in to comment.