Skip to content

Commit b7452e9

Browse files
authored
ENGCOM-8173: Fix inability to place PayPal guest order when Automatic Assignment to Customer Group is enabled #25405
2 parents aa45503 + b90d176 commit b7452e9

File tree

3 files changed

+154
-78
lines changed

3 files changed

+154
-78
lines changed

app/code/Magento/Quote/Observer/Frontend/Quote/Address/CollectTotalsObserver.php

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
*/
66
namespace Magento\Quote\Observer\Frontend\Quote\Address;
77

8+
use Magento\Customer\Api\AddressRepositoryInterface;
9+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
10+
use Magento\Customer\Api\GroupManagementInterface;
11+
use Magento\Customer\Helper\Address;
12+
use Magento\Customer\Model\Session;
13+
use Magento\Customer\Model\Vat;
14+
use Magento\Framework\Event\Observer;
815
use Magento\Framework\Event\ObserverInterface;
16+
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
17+
use Magento\Quote\Model\Quote;
918

1019
/**
1120
* Handle customer VAT number on collect_totals_before event of quote address.
@@ -15,22 +24,22 @@
1524
class CollectTotalsObserver implements ObserverInterface
1625
{
1726
/**
18-
* @var \Magento\Customer\Api\AddressRepositoryInterface
27+
* @var AddressRepositoryInterface
1928
*/
2029
private $addressRepository;
2130

2231
/**
23-
* @var \Magento\Customer\Model\Session
32+
* @var Session
2433
*/
2534
private $customerSession;
2635

2736
/**
28-
* @var \Magento\Customer\Helper\Address
37+
* @var Address
2938
*/
3039
protected $customerAddressHelper;
3140

3241
/**
33-
* @var \Magento\Customer\Model\Vat
42+
* @var Vat
3443
*/
3544
protected $customerVat;
3645

@@ -40,36 +49,36 @@ class CollectTotalsObserver implements ObserverInterface
4049
protected $vatValidator;
4150

4251
/**
43-
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
52+
* @var CustomerInterfaceFactory
4453
*/
4554
protected $customerDataFactory;
4655

4756
/**
4857
* Group Management
4958
*
50-
* @var \Magento\Customer\Api\GroupManagementInterface
59+
* @var GroupManagementInterface
5160
*/
5261
protected $groupManagement;
5362

5463
/**
5564
* Initialize dependencies.
5665
*
57-
* @param \Magento\Customer\Helper\Address $customerAddressHelper
58-
* @param \Magento\Customer\Model\Vat $customerVat
66+
* @param Address $customerAddressHelper
67+
* @param Vat $customerVat
5968
* @param VatValidator $vatValidator
60-
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory
61-
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
62-
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
63-
* @param \Magento\Customer\Model\Session $customerSession
69+
* @param CustomerInterfaceFactory $customerDataFactory
70+
* @param GroupManagementInterface $groupManagement
71+
* @param AddressRepositoryInterface $addressRepository
72+
* @param Session $customerSession
6473
*/
6574
public function __construct(
66-
\Magento\Customer\Helper\Address $customerAddressHelper,
67-
\Magento\Customer\Model\Vat $customerVat,
75+
Address $customerAddressHelper,
76+
Vat $customerVat,
6877
VatValidator $vatValidator,
69-
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
70-
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
71-
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
72-
\Magento\Customer\Model\Session $customerSession
78+
CustomerInterfaceFactory $customerDataFactory,
79+
GroupManagementInterface $groupManagement,
80+
AddressRepositoryInterface $addressRepository,
81+
Session $customerSession
7382
) {
7483
$this->customerVat = $customerVat;
7584
$this->customerAddressHelper = $customerAddressHelper;
@@ -83,25 +92,23 @@ public function __construct(
8392
/**
8493
* Handle customer VAT number if needed on collect_totals_before event of quote address
8594
*
86-
* @param \Magento\Framework\Event\Observer $observer
95+
* @param Observer $observer
8796
* @return void
8897
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
8998
*/
90-
public function execute(\Magento\Framework\Event\Observer $observer)
99+
public function execute(Observer $observer)
91100
{
92-
/** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
101+
/** @var ShippingAssignmentInterface $shippingAssignment */
93102
$shippingAssignment = $observer->getShippingAssignment();
94-
/** @var \Magento\Quote\Model\Quote $quote */
103+
/** @var Quote $quote */
95104
$quote = $observer->getQuote();
96-
/** @var \Magento\Quote\Model\Quote\Address $address */
105+
/** @var Quote\Address $address */
97106
$address = $shippingAssignment->getShipping()->getAddress();
98107

99108
$customer = $quote->getCustomer();
100109
$storeId = $customer->getStoreId();
101110

102-
if ($customer->getDisableAutoGroupChange()
103-
|| false == $this->vatValidator->isEnabled($address, $storeId)
104-
) {
111+
if ($customer->getDisableAutoGroupChange() || !$this->vatValidator->isEnabled($address, $storeId)) {
105112
return;
106113
}
107114
$customerCountryCode = $address->getCountryId();
@@ -135,6 +142,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
135142
$quote->setCustomerGroupId($groupId);
136143
$this->customerSession->setCustomerGroupId($groupId);
137144
$customer->setGroupId($groupId);
145+
$customer->setEmail($customer->getEmail() ?: $quote->getCustomerEmail());
138146
$quote->setCustomer($customer);
139147
}
140148
}

dev/tests/integration/testsuite/Magento/Paypal/Model/Express/CheckoutTest.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,36 @@
1717
use Magento\Quote\Model\Quote\Address;
1818
use Magento\Quote\Model\ResourceModel\Quote\Collection;
1919
use Magento\TestFramework\Helper\Bootstrap;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
2022

2123
/**
2224
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2325
*/
24-
class CheckoutTest extends \PHPUnit\Framework\TestCase
26+
class CheckoutTest extends TestCase
2527
{
2628
/**
2729
* @var ObjectManagerInterface
2830
*/
2931
private $objectManager;
3032

3133
/**
32-
* @var Info|\PHPUnit\Framework\MockObject\MockObject
34+
* @var Info|MockObject
3335
*/
3436
private $paypalInfo;
3537

3638
/**
37-
* @var Config|\PHPUnit\Framework\MockObject\MockObject
39+
* @var Config|MockObject
3840
*/
3941
private $paypalConfig;
4042

4143
/**
42-
* @var Factory|\PHPUnit\Framework\MockObject\MockObject
44+
* @var Factory|MockObject
4345
*/
4446
private $apiTypeFactory;
4547

4648
/**
47-
* @var Nvp|\PHPUnit\Framework\MockObject\MockObject
49+
* @var Nvp|MockObject
4850
*/
4951
private $api;
5052

@@ -215,6 +217,28 @@ public function testPlaceGuestQuote()
215217
$this->assertNotEmpty($order->getShippingAddress());
216218
}
217219

220+
/**
221+
* Place the order as guest when `Automatic Assignment to Customer Group` is enabled.
222+
*
223+
* @magentoDataFixture Magento/Paypal/_files/quote_express.php
224+
* @magentoConfigFixture current_store customer/create_account/auto_group_assign 1
225+
*
226+
* @return void
227+
*/
228+
public function testPlaceGuestQuoteAutomaticAssignmentEnabled(): void
229+
{
230+
$quote = $this->getFixtureQuote();
231+
$quote->setCheckoutMethod(Onepage::METHOD_GUEST);
232+
$quote->getShippingAddress()->setSameAsBilling(0);
233+
$quote->setReservedOrderId(null);
234+
235+
$checkout = $this->getCheckout($quote);
236+
$checkout->place('token');
237+
238+
$order = $checkout->getOrder();
239+
$this->assertNotEmpty($order->getRealOrderId());
240+
}
241+
218242
/**
219243
* @param Quote $quote
220244
* @return Checkout
@@ -721,11 +745,11 @@ private function getFixtureQuote(): Quote
721745
/**
722746
* Adds countryFactory to a mock.
723747
*
724-
* @param \PHPUnit\Framework\MockObject\MockObject $api
748+
* @param MockObject $api
725749
* @return void
726750
* @throws \ReflectionException
727751
*/
728-
private function addCountryFactory(\PHPUnit\Framework\MockObject\MockObject $api): void
752+
private function addCountryFactory(MockObject $api): void
729753
{
730754
$reflection = new \ReflectionClass($api);
731755
$property = $reflection->getProperty('_countryFactory');

0 commit comments

Comments
 (0)