Skip to content

Commit 78ff538

Browse files
authored
Merge pull request #70 from IvansZuks/store-in
#1506 - In-Store Delivery / M2 Support
2 parents 806a9bf + cbc39ac commit 78ff538

File tree

3 files changed

+194
-3
lines changed

3 files changed

+194
-3
lines changed

src/Model/Resolver/GetStores.php

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* ScandiPWA - Progressive Web App for Magento
4+
*
5+
* Copyright © Scandiweb, Inc. All rights reserved.
6+
* See LICENSE for license details.
7+
*
8+
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
9+
* @package scandipwa/quote-graphql
10+
* @link https://github.com/scandipwa/quote-graphql
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace ScandiPWA\QuoteGraphQl\Model\Resolver;
16+
17+
use Exception;
18+
use Magento\Directory\Model\CountryFactory;
19+
use Magento\Framework\App\Config\ScopeConfigInterface;
20+
use Magento\Framework\GraphQl\Config\Element\Field;
21+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
22+
use Magento\Framework\GraphQl\Query\Resolver\Value;
23+
use Magento\Framework\GraphQl\Query\ResolverInterface;
24+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
25+
use Magento\InventoryInStorePickup\Model\SearchRequestBuilder;
26+
use Magento\InventoryInStorePickup\Model\GetPickupLocations;
27+
28+
/**
29+
* Class GetStores
30+
* @package ScandiPWA\QuoteGraphQl\Model\Resolver
31+
*/
32+
class GetStores implements ResolverInterface
33+
{
34+
/**
35+
* Config path to radius value (In magento original class it's private)
36+
*/
37+
protected const SEARCH_RADIUS = 'carriers/instore/search_radius';
38+
39+
/**
40+
* @var SearchRequestBuilder
41+
*/
42+
protected $searchRequest;
43+
44+
/**
45+
* @var GetPickupLocations
46+
*/
47+
protected $getPickupLocations;
48+
49+
/**
50+
* @var CountryFactory
51+
*/
52+
protected $countryFactory;
53+
54+
/**
55+
* @var ScopeConfigInterface
56+
*/
57+
protected $scopeConfig;
58+
59+
/**
60+
* GetStores constructor.
61+
* @param SearchRequestBuilder $searchRequest
62+
* @param GetPickupLocations $getPickupLocations
63+
* @param CountryFactory $countryFactory
64+
* @param ScopeConfigInterface $scopeConfig
65+
*/
66+
public function __construct(
67+
SearchRequestBuilder $searchRequest,
68+
GetPickupLocations $getPickupLocations,
69+
CountryFactory $countryFactory,
70+
ScopeConfigInterface $scopeConfig
71+
) {
72+
73+
$this->searchRequest = $searchRequest;
74+
$this->getPickupLocations = $getPickupLocations;
75+
$this->countryFactory = $countryFactory;
76+
$this->scopeConfig = $scopeConfig;
77+
}
78+
79+
/**
80+
* Fetches the data from persistence models and format it according to the GraphQL schema.
81+
*
82+
* @param Field $field
83+
* @param ContextInterface $context
84+
* @param ResolveInfo $info
85+
* @param array|null $value
86+
* @param array|null $args
87+
* @return mixed|Value
88+
* @throws Exception
89+
*/
90+
public function resolve(
91+
Field $field,
92+
$context,
93+
ResolveInfo $info,
94+
array $value = null,
95+
array $args = null
96+
) {
97+
if (!isset($args['search']) || !isset($args['country'])) {
98+
throw new GraphQlInputException(
99+
__('Required parameter "search" or "country" is missing.')
100+
);
101+
}
102+
103+
$result = [];
104+
105+
$searchRequest = $this->searchRequest
106+
->setAreaSearchTerm(sprintf(
107+
'%s:%s',
108+
$args['search'],
109+
$args['country']
110+
))
111+
->setAreaRadius($this->scopeConfig->getValue(self::SEARCH_RADIUS))
112+
->setScopeCode('base')
113+
->create();
114+
$searchResponse = $this->getPickupLocations->execute($searchRequest);
115+
116+
foreach ($searchResponse->getItems() as $item) {
117+
$result[] = [
118+
'city' => $item->getCity(),
119+
'country' => $this->countryFactory->create()->loadByCode($item->getCountryId())->getName(),
120+
'description' => $item->getDescription(),
121+
'name' => $item->getName(),
122+
'phone' => $item->getPhone(),
123+
'pickup_location_code' => $item->getPickupLocationCode(),
124+
'postcode' => $item->getPostcode(),
125+
'region' => $item->getRegion(),
126+
'street' => $item->getStreet()
127+
];
128+
}
129+
130+
return ['stores' => $result];
131+
}
132+
}

src/Model/Resolver/SaveAddressInformation.php

+39-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace ScandiPWA\QuoteGraphQl\Model\Resolver;
1616

17+
use Exception;
1718
use Magento\Checkout\Api\Data\ShippingInformationInterface;
1819
use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory;
1920
use Magento\Checkout\Api\GuestShippingInformationManagementInterface;
@@ -24,6 +25,8 @@
2425
use Magento\Framework\GraphQl\Query\ResolverInterface;
2526
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2627
use Magento\Checkout\Model\ShippingInformationManagement;
28+
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;
29+
use Magento\Quote\Api\Data\AddressExtensionInterfaceFactory;
2730
use Magento\Quote\Model\Webapi\ParamOverriderCartId;
2831
use Magento\Quote\Api\Data\AddressInterfaceFactory;
2932
use \Magento\Quote\Api\Data\PaymentMethodInterface;
@@ -59,29 +62,50 @@ class SaveAddressInformation implements ResolverInterface {
5962
*/
6063
protected $addressInterfaceFactory;
6164

65+
/**
66+
* @var AddressExtensionInterfaceFactory
67+
*/
68+
protected $addressExtensionInterfaceFactory;
69+
70+
/**
71+
* Pick up in store method code (In magento original class it's private)
72+
*/
73+
protected const PICKUP_METHOD_CODE = 'pickup';
74+
75+
/**
76+
* SaveAddressInformation constructor.
77+
* @param ShippingInformationManagementInterface $shippingInformationManagement
78+
* @param GuestShippingInformationManagementInterface $guestShippingInformationManagement
79+
* @param ShippingInformationInterfaceFactory $shippingInformation
80+
* @param ParamOverriderCartId $overriderCartId
81+
* @param AddressInterfaceFactory $addressInterfaceFactory
82+
* @param AddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
83+
*/
6284
public function __construct(
6385
ShippingInformationManagementInterface $shippingInformationManagement,
6486
GuestShippingInformationManagementInterface $guestShippingInformationManagement,
6587
ShippingInformationInterfaceFactory $shippingInformation,
6688
ParamOverriderCartId $overriderCartId,
67-
AddressInterfaceFactory $addressInterfaceFactory
89+
AddressInterfaceFactory $addressInterfaceFactory,
90+
AddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
6891
) {
6992
$this->shippingInformation = $shippingInformation;
7093
$this->overriderCartId = $overriderCartId;
7194
$this->shippingInformationManagement = $shippingInformationManagement;
7295
$this->guestShippingInformationManagement = $guestShippingInformationManagement;
7396
$this->addressInterfaceFactory = $addressInterfaceFactory;
97+
$this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
7498
}
7599

76100
/**
77101
* Fetches the data from persistence models and format it according to the GraphQL schema.
78102
*
79-
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
103+
* @param Field $field
80104
* @param ContextInterface $context
81105
* @param ResolveInfo $info
82106
* @param array|null $value
83107
* @param array|null $args
84-
* @throws \Exception
108+
* @throws Exception
85109
* @return mixed|Value
86110
*/
87111
public function resolve(
@@ -103,6 +127,18 @@ public function resolve(
103127
$shippingAddressObject = $this->addressInterfaceFactory->create([ 'data' => $shippingAddress ]);
104128
$billingAddressObject = $this->addressInterfaceFactory->create([ 'data' => $billingAddress ]);
105129

130+
if ($shippingMethodCode === self::PICKUP_METHOD_CODE && isset($shippingAddress['extension_attributes'])) {
131+
foreach ($shippingAddress['extension_attributes'] as $attribute) {
132+
if ($attribute['attribute_code'] === PickupLocationInterface::PICKUP_LOCATION_CODE) {
133+
$shippingAddressObject->setExtensionAttributes(
134+
$this->addressExtensionInterfaceFactory->create()->setPickupLocationCode($attribute['value'])
135+
);
136+
137+
break;
138+
}
139+
}
140+
}
141+
106142
$addressInformation = $this->shippingInformation->create([
107143
'data' => [
108144
'shipping_address' => $shippingAddressObject,

src/etc/schema.graphqls

+23
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Query {
1818
getOrderById(id: Int!): Order @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\ExpandedOrderResolver") @doc(description: "The Sales Order query returns information about a Sales order")
1919
getBraintreeConfig: Braintree @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\BraintreeResolver")
2020
getCartDisplayConfig: CartDisplayConfig @doc(description: "Get cart display settings") @resolver(class: "\\ScandiPWA\\QuoteGraphQl\\Model\\Resolver\\CartDisplayConfigResolver")
21+
getStores(search: String, country: String): Stores @resolver(class: "ScandiPWA\\StoreInPickUp\\Model\\Resolver\\GetStores")
2122
}
2223

2324
type Mutation {
@@ -121,6 +122,7 @@ input AddressInput {
121122
company: String
122123
street: [String]
123124
vat_id: String
125+
extension_attributes: [AddressExtensionAttributes]
124126
}
125127

126128
input EstimateShippingCostsAddress {
@@ -391,3 +393,24 @@ type CustomizableFieldValue {
391393
price_type: PriceTypeEnum
392394
sku: String
393395
}
396+
397+
type Stores {
398+
stores: [Store]
399+
}
400+
401+
type Store {
402+
city: String
403+
country: String
404+
description: String
405+
name: String
406+
phone: String
407+
pickup_location_code: String
408+
postcode: String
409+
region: String
410+
street: String
411+
}
412+
413+
input AddressExtensionAttributes {
414+
attribute_code: String
415+
value: String
416+
}

0 commit comments

Comments
 (0)