Skip to content

Commit 9053b10

Browse files
authored
Merge pull request #61 from riha112/2148
#2148 Linkage of order to customer via graphql
2 parents e2b66cf + 81859c3 commit 9053b10

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

src/Model/Resolver/LinkOrder.php

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace ScandiPWA\QuoteGraphQl\Model\Resolver;
4+
5+
use Magento\Framework\Exception\NoSuchEntityException;
6+
use Magento\Framework\GraphQl\Config\Element\Field;
7+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
8+
use Magento\Framework\GraphQl\Query\Resolver\Value;
9+
use Magento\Framework\GraphQl\Query\ResolverInterface;
10+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
11+
use Magento\Quote\Api\Data\PaymentMethodInterface;
12+
use Magento\Quote\Model\QuoteIdMask;
13+
use Magento\Checkout\Model\Session as CheckoutSession;
14+
use Magento\Sales\Model\Order;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Customer\Model\Customer;
17+
use Magento\Sales\Api\OrderRepositoryInterface;
18+
use Magento\Customer\Model\CustomerFactory;
19+
20+
/**
21+
* Class LinkOrder
22+
* @package ScandiPWA\QuoteGraphQl\Model\Resolver
23+
*/
24+
class LinkOrder implements ResolverInterface
25+
{
26+
/**
27+
* @var OrderRepositoryInterface
28+
*/
29+
protected $orderRepository;
30+
31+
/**
32+
* @var CheckoutSession
33+
*/
34+
protected $checkoutSession;
35+
36+
/**
37+
* @var StoreManagerInterface
38+
*/
39+
protected $storeManager;
40+
41+
/**
42+
* @var CustomerFactory
43+
*/
44+
protected $customerFactory;
45+
46+
/**
47+
* LinkOrder constructor.
48+
* @param OrderRepositoryInterface $orderRepository
49+
* @param CheckoutSession $checkoutSession
50+
* @param StoreManagerInterface $storeManager
51+
*/
52+
public function __construct(
53+
OrderRepositoryInterface $orderRepository,
54+
CheckoutSession $checkoutSession,
55+
StoreManagerInterface $storeManager,
56+
CustomerFactory $customerFactory
57+
) {
58+
$this->orderRepository = $orderRepository;
59+
$this->checkoutSession = $checkoutSession;
60+
$this->storeManager = $storeManager;
61+
$this->customerFactory = $customerFactory;
62+
}
63+
64+
/**
65+
* Get payment methods
66+
*
67+
* @param Field $field
68+
* @param ContextInterface $context
69+
* @param ResolveInfo $info
70+
* @param array|null $value
71+
* @param array|null $args
72+
* @return array|Value|mixed
73+
* @throws NoSuchEntityException
74+
*/
75+
public function resolve(
76+
Field $field,
77+
$context,
78+
ResolveInfo $info,
79+
array $value = null,
80+
array $args = null
81+
) {
82+
if (!isset($args['customer_email'])) {
83+
return false;
84+
}
85+
86+
$customerEmail = $args['customer_email'];
87+
$customer = $this->getCustomerByEmail($customerEmail);
88+
if (!$customer->getId()) {
89+
return false;
90+
}
91+
92+
// Loads last order from session
93+
$order = $this->checkoutSession->getLastRealOrder();
94+
95+
if (!$this->validateOrder($order, $customer)) {
96+
return false;
97+
}
98+
99+
$order->setCustomerId($customer->getId());
100+
$order->setCustomerIsGuest(0);
101+
$this->orderRepository->save($order);
102+
103+
return true;
104+
}
105+
106+
/**
107+
* @param Order $order
108+
* @param Customer $custumer
109+
* @return bool
110+
*/
111+
public function validateOrder($order, $custumer) : bool {
112+
// If order is un-siggned
113+
if (!$order->getId() || $order->getCustomerId()) {
114+
return false;
115+
}
116+
117+
// If order was placed in same store
118+
if ($order->getStoreId() !== $custumer->getStoreId()) {
119+
return false;
120+
}
121+
122+
return true;
123+
}
124+
125+
/**
126+
* @param $email
127+
* @return Customer
128+
* @throws NoSuchEntityException
129+
*/
130+
public function getCustomerByEmail($email)
131+
{
132+
$websiteID = $this->storeManager->getStore()->getWebsiteId();
133+
$customer = $this->customerFactory->create()->setWebsiteId($websiteID)->loadByEmail($email);
134+
135+
return $customer;
136+
}
137+
}

src/etc/schema.graphqls

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Mutation {
2727
removeCartItem(guestCartId: String, item_id: Int!): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\RemoveCartItem")
2828
applyCoupon(guestCartId: String, coupon_code: String!): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ApplyCoupon")
2929
removeCoupon(guestCartId: String): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\RemoveCoupon")
30+
linkOrder(customer_email: String!): Boolean @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\LinkOrder")
3031

3132
# Magento 2 overrides to stop sending logged in user cart IDs
3233
s_setPaymentMethodOnCart(input: S_SetPaymentMethodOnCartInput!): SetPaymentMethodOnCartOutput @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")

0 commit comments

Comments
 (0)