Skip to content

Commit cd52ad3

Browse files
committed
Merge branch 'graphql-api-enhancements' of github.com:magento-lynx/magento2ce into graphql-api-enhancements
2 parents 30c8c72 + b9cbec3 commit cd52ad3

File tree

7 files changed

+420
-1
lines changed

7 files changed

+420
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\CustomerGraphQl\Model;
18+
19+
use Magento\Framework\Api\SearchCriteriaBuilder;
20+
use Magento\Sales\Api\OrderRepositoryInterface;
21+
use Magento\Sales\Api\Data\OrderSearchResultInterface;
22+
23+
class GetGuestOrdersByEmail
24+
{
25+
/**
26+
* @param OrderRepositoryInterface $orderRepository
27+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
28+
*/
29+
public function __construct(
30+
private readonly OrderRepositoryInterface $orderRepository,
31+
private SearchCriteriaBuilder $searchCriteriaBuilder
32+
) {
33+
}
34+
35+
/**
36+
* Retrieve customer orders collection
37+
*
38+
* @param string $email
39+
* @return OrderSearchResultInterface
40+
*/
41+
public function execute(string $email): OrderSearchResultInterface
42+
{
43+
$this->searchCriteriaBuilder->addFilter(
44+
'customer_email',
45+
$email,
46+
'eq'
47+
)->addFilter(
48+
'customer_is_guest',
49+
1,
50+
'eq'
51+
);
52+
return $this->orderRepository->getList($this->searchCriteriaBuilder->create());
53+
}
54+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\CustomerGraphQl\Plugin\Model;
18+
19+
use Magento\Customer\Model\AccountManagement;
20+
use Magento\Customer\Api\Data\CustomerInterface;
21+
use Magento\Sales\Model\Order\CustomerAssignment;
22+
use Magento\CustomerGraphQl\Model\GetGuestOrdersByEmail;
23+
24+
class MergeGuestOrder
25+
{
26+
/**
27+
* @param GetGuestOrdersByEmail $getGuestOrdersByEmail
28+
* @param CustomerAssignment $customerAssignment
29+
*/
30+
public function __construct(
31+
private readonly GetGuestOrdersByEmail $getGuestOrdersByEmail,
32+
private readonly CustomerAssignment $customerAssignment
33+
) {
34+
}
35+
36+
/**
37+
* Merge guest customer order after signup
38+
*
39+
* @param AccountManagement $subject
40+
* @param CustomerInterface $customer
41+
* @return CustomerInterface
42+
*/
43+
public function afterCreateAccount(AccountManagement $subject, CustomerInterface $customer)
44+
{
45+
$searchResult = $this->getGuestOrdersByEmail->execute($customer->getEmail());
46+
foreach ($searchResult->getItems() as $order) {
47+
$this->customerAssignment->execute($order, $customer);
48+
}
49+
return $customer;
50+
}
51+
}

app/code/Magento/CustomerGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"magento/module-directory": "*",
1717
"magento/module-tax": "*",
1818
"magento/module-graph-ql-cache": "*",
19-
"magento/module-graph-ql-resolver-cache": "*"
19+
"magento/module-graph-ql-resolver-cache": "*",
20+
"magento/module-sales": "*"
2021
},
2122
"license": [
2223
"OSL-3.0",

app/code/Magento/CustomerGraphQl/etc/graphql/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,8 @@
209209
</argument>
210210
</arguments>
211211
</type>
212+
<type name="Magento\Customer\Model\AccountManagement">
213+
<plugin name="merge_order_after_customer_signup"
214+
type="Magento\CustomerGraphQl\Plugin\Model\MergeGuestOrder" />
215+
</type>
212216
</config>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\QuoteGraphQl\Plugin\Model;
18+
19+
use Magento\Quote\Model\QuoteManagement;
20+
use Magento\Sales\Api\OrderRepositoryInterface;
21+
use Magento\Store\Model\StoreManagerInterface;
22+
use Magento\Sales\Model\Order\CustomerAssignment;
23+
use Magento\Customer\Api\CustomerRepositoryInterface;
24+
use Magento\Framework\Exception\NoSuchEntityException;
25+
26+
class MergeGuestOrder
27+
{
28+
/**
29+
* @param OrderRepositoryInterface $orderRepository
30+
* @param StoreManagerInterface $storeManager
31+
* @param CustomerAssignment $customerAssignment
32+
* @param CustomerRepositoryInterface $customerRepository
33+
*/
34+
public function __construct(
35+
private readonly OrderRepositoryInterface $orderRepository,
36+
private readonly StoreManagerInterface $storeManager,
37+
private readonly CustomerAssignment $customerAssignment,
38+
private readonly CustomerRepositoryInterface $customerRepository
39+
) {
40+
}
41+
42+
/**
43+
* Merge guest order in customer after place order
44+
*
45+
* @param QuoteManagement $subject
46+
* @param int $orderId
47+
* @return int
48+
* @throws \Magento\Framework\Exception\LocalizedException
49+
* @throws \Magento\Framework\Exception\NoSuchEntityException
50+
*/
51+
public function afterPlaceOrder(QuoteManagement $subject, int $orderId)
52+
{
53+
if ($orderId) {
54+
$order = $this->orderRepository->get($orderId);
55+
if ($order->getCustomerIsGuest() && $order->getCustomerEmail()) {
56+
try {
57+
$websiteID = $this->storeManager->getStore()->getWebsiteId();
58+
$customer = $this->customerRepository->get($order->getCustomerEmail(), $websiteID);
59+
if ($customer->getId()) {
60+
$this->customerAssignment->execute($order, $customer);
61+
}
62+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
63+
} catch (NoSuchEntityException $e) {
64+
// Do not remove this handle as it used to check that customer
65+
// with this email not registered in the system
66+
}
67+
}
68+
}
69+
return $orderId;
70+
}
71+
}

app/code/Magento/QuoteGraphQl/etc/graphql/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,8 @@
7575
</argument>
7676
</arguments>
7777
</type>
78+
<type name="Magento\Quote\Model\QuoteManagement">
79+
<plugin name="merge_guest_orders_with_customer_after_place"
80+
type="Magento\QuoteGraphQl\Plugin\Model\MergeGuestOrder" />
81+
</type>
7882
</config>

0 commit comments

Comments
 (0)