Skip to content

Commit f4a5a13

Browse files
committed
Merge remote-tracking branch 'adobe-commerce-tier-4/ACP2E-3255' into Tier4-Kings-PR-10-14-2024
2 parents 681a15c + 2a2fac9 commit f4a5a13

File tree

2 files changed

+162
-9
lines changed

2 files changed

+162
-9
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,14 @@ protected function _prepareCustomerQuote($quote)
694694
$hasDefaultBilling = true;
695695
}
696696
}
697-
//save here new customer address
698-
$shippingAddress->setCustomerId($quote->getCustomerId());
699-
$this->addressRepository->save($shippingAddress);
700-
$quote->addCustomerAddress($shippingAddress);
697+
if (!$shippingAddress->getId()) {
698+
//save here new customer address
699+
$shippingAddress->setCustomerId($quote->getCustomerId());
700+
$this->addressRepository->save($shippingAddress);
701+
$quote->addCustomerAddress($shippingAddress);
702+
$this->addressesToSync[] = $shippingAddress->getId();
703+
}
701704
$shipping->setCustomerAddressData($shippingAddress);
702-
$this->addressesToSync[] = $shippingAddress->getId();
703705
$shipping->setCustomerAddressId($shippingAddress->getId());
704706
}
705707
}
@@ -727,11 +729,13 @@ protected function _prepareCustomerQuote($quote)
727729
}
728730
$billingAddress->setIsDefaultBilling(true);
729731
}
730-
$billingAddress->setCustomerId($quote->getCustomerId());
731-
$this->addressRepository->save($billingAddress);
732-
$quote->addCustomerAddress($billingAddress);
732+
if (!$billingAddress->getId()) {
733+
$billingAddress->setCustomerId($quote->getCustomerId());
734+
$this->addressRepository->save($billingAddress);
735+
$quote->addCustomerAddress($billingAddress);
736+
$this->addressesToSync[] = $billingAddress->getId();
737+
}
733738
$billing->setCustomerAddressData($billingAddress);
734-
$this->addressesToSync[] = $billingAddress->getId();
735739
$billing->setCustomerAddressId($billingAddress->getId());
736740

737741
// Admin order: `Same As Billing Address`- when new billing address saved in address book
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote\Customer;
9+
10+
use Exception;
11+
use Magento\Customer\Test\Fixture\Customer;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Integration\Api\CustomerTokenServiceInterface;
15+
use Magento\Framework\Exception\AuthenticationException;
16+
use Magento\Quote\Model\Quote;
17+
use Magento\Quote\Model\ResourceModel\Quote\Collection;
18+
use Magento\TestFramework\Fixture\Config as ConfigFixture;
19+
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
20+
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
21+
use Magento\Store\Test\Fixture\Store as StoreFixture;
22+
use Magento\Store\Model\ScopeInterface;
23+
use Magento\TestFramework\Fixture\DataFixture;
24+
use Magento\TestFramework\Fixture\DataFixtureStorage;
25+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
26+
use Magento\TestFramework\Helper\Bootstrap;
27+
use Magento\TestFramework\TestCase\GraphQlAbstract;
28+
29+
/**
30+
* Test for create customer cart
31+
*/
32+
class CreateCustomerCart extends GraphQlAbstract
33+
{
34+
/**
35+
* @var ObjectManagerInterface
36+
*/
37+
private ObjectManagerInterface $objectManager;
38+
39+
/**
40+
* @var DataFixtureStorage
41+
*/
42+
private DataFixtureStorage $fixtures;
43+
44+
/**
45+
* @var CustomerTokenServiceInterface
46+
*/
47+
private CustomerTokenServiceInterface $customerTokenService;
48+
49+
/**
50+
* Setup for create customer cart
51+
*
52+
* @return void
53+
* @throws LocalizedException
54+
*/
55+
protected function setUp(): void
56+
{
57+
$this->objectManager = Bootstrap::getObjectManager();
58+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
59+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
60+
}
61+
62+
/**
63+
* @inheritdoc
64+
*/
65+
protected function tearDown(): void
66+
{
67+
/** @var Quote $quote */
68+
$quoteCollection = $this->objectManager->create(Collection::class);
69+
foreach ($quoteCollection as $quote) {
70+
$quote->delete();
71+
}
72+
parent::tearDown();
73+
}
74+
75+
/**
76+
* Test to create empty cart for customer with billing and shipping address
77+
*
78+
* @return void
79+
* @throws AuthenticationException
80+
* @throws Exception
81+
*/
82+
#[
83+
ConfigFixture('customer/account_share/scope', 0),
84+
DataFixture(WebsiteFixture::class, ['code' => 'website2'], as: 'website2'),
85+
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'group2'),
86+
DataFixture(StoreFixture::class, ['website_id' => '$website2.id$'], 'store2'),
87+
ConfigFixture('general/country/allow', 'US', ScopeInterface::SCOPE_WEBSITE, 'base'),
88+
ConfigFixture('general/country/allow', 'NL', ScopeInterface::SCOPE_WEBSITE, 'website2'),
89+
DataFixture(
90+
Customer::class,
91+
[
92+
'email' => 'john@doe.com',
93+
'password' => 'test@123',
94+
'addresses' => [
95+
[
96+
'country_id' => 'US',
97+
'region_id' => 32,
98+
'city' => 'Boston',
99+
'street' => ['10 Milk Street'],
100+
'postcode' => '02108',
101+
'telephone' => '1234567890',
102+
'default_billing' => true,
103+
'default_shipping' => true,
104+
],
105+
],
106+
],
107+
'customer'
108+
),
109+
]
110+
public function testToCreateEmptyCartForCustomerWithDefaultAddress()
111+
{
112+
$store = $this->fixtures->get('store2');
113+
$customerCartQuery = $this->getCustomerCartQuery();
114+
$headerMap = $this->getHeaderMap();
115+
$headerMap['Store'] = $store->getCode();
116+
$response = $this->graphQlMutation($customerCartQuery, [], '', $headerMap);
117+
self::assertArrayHasKey('customerCart', $response);
118+
self::assertArrayHasKey('id', $response['customerCart']);
119+
self::assertNotEmpty($response['customerCart']['id']);
120+
}
121+
122+
/**
123+
* Query customer cart
124+
*
125+
* @return string
126+
*/
127+
private function getCustomerCartQuery(): string
128+
{
129+
return <<<QUERY
130+
{
131+
customerCart {
132+
id
133+
}
134+
}
135+
QUERY;
136+
}
137+
138+
/**
139+
* Get Authentication header
140+
*
141+
* @return array
142+
* @throws AuthenticationException
143+
*/
144+
private function getHeaderMap(): array
145+
{
146+
$customerToken = $this->customerTokenService->createCustomerAccessToken('john@doe.com', 'test@123');
147+
return ['Authorization' => 'Bearer ' . $customerToken];
148+
}
149+
}

0 commit comments

Comments
 (0)