Skip to content

Commit

Permalink
Merge pull request #61 from riha112/2148
Browse files Browse the repository at this point in the history
#2148 Linkage of order to customer via graphql
  • Loading branch information
yeegor authored Mar 26, 2021
2 parents e2b66cf + 81859c3 commit 9053b10
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/Model/Resolver/LinkOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\Data\PaymentMethodInterface;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Sales\Model\Order;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Model\Customer;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Customer\Model\CustomerFactory;

/**
* Class LinkOrder
* @package ScandiPWA\QuoteGraphQl\Model\Resolver
*/
class LinkOrder implements ResolverInterface
{
/**
* @var OrderRepositoryInterface
*/
protected $orderRepository;

/**
* @var CheckoutSession
*/
protected $checkoutSession;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var CustomerFactory
*/
protected $customerFactory;

/**
* LinkOrder constructor.
* @param OrderRepositoryInterface $orderRepository
* @param CheckoutSession $checkoutSession
* @param StoreManagerInterface $storeManager
*/
public function __construct(
OrderRepositoryInterface $orderRepository,
CheckoutSession $checkoutSession,
StoreManagerInterface $storeManager,
CustomerFactory $customerFactory
) {
$this->orderRepository = $orderRepository;
$this->checkoutSession = $checkoutSession;
$this->storeManager = $storeManager;
$this->customerFactory = $customerFactory;
}

/**
* Get payment methods
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array|Value|mixed
* @throws NoSuchEntityException
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($args['customer_email'])) {
return false;
}

$customerEmail = $args['customer_email'];
$customer = $this->getCustomerByEmail($customerEmail);
if (!$customer->getId()) {
return false;
}

// Loads last order from session
$order = $this->checkoutSession->getLastRealOrder();

if (!$this->validateOrder($order, $customer)) {
return false;
}

$order->setCustomerId($customer->getId());
$order->setCustomerIsGuest(0);
$this->orderRepository->save($order);

return true;
}

/**
* @param Order $order
* @param Customer $custumer
* @return bool
*/
public function validateOrder($order, $custumer) : bool {
// If order is un-siggned
if (!$order->getId() || $order->getCustomerId()) {
return false;
}

// If order was placed in same store
if ($order->getStoreId() !== $custumer->getStoreId()) {
return false;
}

return true;
}

/**
* @param $email
* @return Customer
* @throws NoSuchEntityException
*/
public function getCustomerByEmail($email)
{
$websiteID = $this->storeManager->getStore()->getWebsiteId();
$customer = $this->customerFactory->create()->setWebsiteId($websiteID)->loadByEmail($email);

return $customer;
}
}
1 change: 1 addition & 0 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Mutation {
removeCartItem(guestCartId: String, item_id: Int!): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\RemoveCartItem")
applyCoupon(guestCartId: String, coupon_code: String!): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ApplyCoupon")
removeCoupon(guestCartId: String): Query @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\RemoveCoupon")
linkOrder(customer_email: String!): Boolean @resolver(class:"\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\LinkOrder")

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

0 comments on commit 9053b10

Please sign in to comment.