Skip to content

Commit

Permalink
Merge pull request #70 from IvansZuks/store-in
Browse files Browse the repository at this point in the history
#1506 - In-Store Delivery / M2 Support
  • Loading branch information
yeegor authored May 5, 2021
2 parents 806a9bf + cbc39ac commit 78ff538
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 3 deletions.
132 changes: 132 additions & 0 deletions src/Model/Resolver/GetStores.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/quote-graphql
* @link https://github.com/scandipwa/quote-graphql
*/

declare(strict_types=1);

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Exception;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
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\InventoryInStorePickup\Model\SearchRequestBuilder;
use Magento\InventoryInStorePickup\Model\GetPickupLocations;

/**
* Class GetStores
* @package ScandiPWA\QuoteGraphQl\Model\Resolver
*/
class GetStores implements ResolverInterface
{
/**
* Config path to radius value (In magento original class it's private)
*/
protected const SEARCH_RADIUS = 'carriers/instore/search_radius';

/**
* @var SearchRequestBuilder
*/
protected $searchRequest;

/**
* @var GetPickupLocations
*/
protected $getPickupLocations;

/**
* @var CountryFactory
*/
protected $countryFactory;

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* GetStores constructor.
* @param SearchRequestBuilder $searchRequest
* @param GetPickupLocations $getPickupLocations
* @param CountryFactory $countryFactory
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
SearchRequestBuilder $searchRequest,
GetPickupLocations $getPickupLocations,
CountryFactory $countryFactory,
ScopeConfigInterface $scopeConfig
) {

$this->searchRequest = $searchRequest;
$this->getPickupLocations = $getPickupLocations;
$this->countryFactory = $countryFactory;
$this->scopeConfig = $scopeConfig;
}

/**
* Fetches the data from persistence models and format it according to the GraphQL schema.
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return mixed|Value
* @throws Exception
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($args['search']) || !isset($args['country'])) {
throw new GraphQlInputException(
__('Required parameter "search" or "country" is missing.')
);
}

$result = [];

$searchRequest = $this->searchRequest
->setAreaSearchTerm(sprintf(
'%s:%s',
$args['search'],
$args['country']
))
->setAreaRadius($this->scopeConfig->getValue(self::SEARCH_RADIUS))
->setScopeCode('base')
->create();
$searchResponse = $this->getPickupLocations->execute($searchRequest);

foreach ($searchResponse->getItems() as $item) {
$result[] = [
'city' => $item->getCity(),
'country' => $this->countryFactory->create()->loadByCode($item->getCountryId())->getName(),
'description' => $item->getDescription(),
'name' => $item->getName(),
'phone' => $item->getPhone(),
'pickup_location_code' => $item->getPickupLocationCode(),
'postcode' => $item->getPostcode(),
'region' => $item->getRegion(),
'street' => $item->getStreet()
];
}

return ['stores' => $result];
}
}
42 changes: 39 additions & 3 deletions src/Model/Resolver/SaveAddressInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Exception;
use Magento\Checkout\Api\Data\ShippingInformationInterface;
use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory;
use Magento\Checkout\Api\GuestShippingInformationManagementInterface;
Expand All @@ -24,6 +25,8 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Checkout\Model\ShippingInformationManagement;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;
use Magento\Quote\Api\Data\AddressExtensionInterfaceFactory;
use Magento\Quote\Model\Webapi\ParamOverriderCartId;
use Magento\Quote\Api\Data\AddressInterfaceFactory;
use \Magento\Quote\Api\Data\PaymentMethodInterface;
Expand Down Expand Up @@ -59,29 +62,50 @@ class SaveAddressInformation implements ResolverInterface {
*/
protected $addressInterfaceFactory;

/**
* @var AddressExtensionInterfaceFactory
*/
protected $addressExtensionInterfaceFactory;

/**
* Pick up in store method code (In magento original class it's private)
*/
protected const PICKUP_METHOD_CODE = 'pickup';

/**
* SaveAddressInformation constructor.
* @param ShippingInformationManagementInterface $shippingInformationManagement
* @param GuestShippingInformationManagementInterface $guestShippingInformationManagement
* @param ShippingInformationInterfaceFactory $shippingInformation
* @param ParamOverriderCartId $overriderCartId
* @param AddressInterfaceFactory $addressInterfaceFactory
* @param AddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
*/
public function __construct(
ShippingInformationManagementInterface $shippingInformationManagement,
GuestShippingInformationManagementInterface $guestShippingInformationManagement,
ShippingInformationInterfaceFactory $shippingInformation,
ParamOverriderCartId $overriderCartId,
AddressInterfaceFactory $addressInterfaceFactory
AddressInterfaceFactory $addressInterfaceFactory,
AddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
) {
$this->shippingInformation = $shippingInformation;
$this->overriderCartId = $overriderCartId;
$this->shippingInformationManagement = $shippingInformationManagement;
$this->guestShippingInformationManagement = $guestShippingInformationManagement;
$this->addressInterfaceFactory = $addressInterfaceFactory;
$this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
}

/**
* Fetches the data from persistence models and format it according to the GraphQL schema.
*
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @throws \Exception
* @throws Exception
* @return mixed|Value
*/
public function resolve(
Expand All @@ -103,6 +127,18 @@ public function resolve(
$shippingAddressObject = $this->addressInterfaceFactory->create([ 'data' => $shippingAddress ]);
$billingAddressObject = $this->addressInterfaceFactory->create([ 'data' => $billingAddress ]);

if ($shippingMethodCode === self::PICKUP_METHOD_CODE && isset($shippingAddress['extension_attributes'])) {
foreach ($shippingAddress['extension_attributes'] as $attribute) {
if ($attribute['attribute_code'] === PickupLocationInterface::PICKUP_LOCATION_CODE) {
$shippingAddressObject->setExtensionAttributes(
$this->addressExtensionInterfaceFactory->create()->setPickupLocationCode($attribute['value'])
);

break;
}
}
}

$addressInformation = $this->shippingInformation->create([
'data' => [
'shipping_address' => $shippingAddressObject,
Expand Down
23 changes: 23 additions & 0 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Query {
getOrderById(id: Int!): Order @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ExpandedOrderResolver") @doc(description: "The Sales Order query returns information about a Sales order")
getBraintreeConfig: Braintree @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\BraintreeResolver")
getCartDisplayConfig: CartDisplayConfig @doc(description: "Get cart display settings") @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\CartDisplayConfigResolver")
getStores(search: String, country: String): Stores @resolver(class: "ScandiPWA\\StoreInPickUp\\Model\\Resolver\\GetStores")
}

type Mutation {
Expand Down Expand Up @@ -121,6 +122,7 @@ input AddressInput {
company: String
street: [String]
vat_id: String
extension_attributes: [AddressExtensionAttributes]
}

input EstimateShippingCostsAddress {
Expand Down Expand Up @@ -391,3 +393,24 @@ type CustomizableFieldValue {
price_type: PriceTypeEnum
sku: String
}

type Stores {
stores: [Store]
}

type Store {
city: String
country: String
description: String
name: String
phone: String
pickup_location_code: String
postcode: String
region: String
street: String
}

input AddressExtensionAttributes {
attribute_code: String
value: String
}

0 comments on commit 78ff538

Please sign in to comment.