From c0fb5c65ae0d686596591dc763a3c75330bfed3b Mon Sep 17 00:00:00 2001 From: Ivans Zuks Date: Fri, 19 Mar 2021 16:30:11 +0200 Subject: [PATCH 1/2] Added StorInPickUp BE support --- src/Model/Resolver/GetStores.php | 119 ++++++++++++++++++ src/Model/Resolver/SaveAddressInformation.php | 42 ++++++- src/etc/schema.graphqls | 34 +++++ 3 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 src/Model/Resolver/GetStores.php diff --git a/src/Model/Resolver/GetStores.php b/src/Model/Resolver/GetStores.php new file mode 100644 index 0000000..d49bb63 --- /dev/null +++ b/src/Model/Resolver/GetStores.php @@ -0,0 +1,119 @@ +searchRequest = $searchRequest; + $this->getPickupLocations = $getPickupLocations; + $this->countryFactory = $countryFactory; + } + + /** + * 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(200) + ->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]; + } +} diff --git a/src/Model/Resolver/SaveAddressInformation.php b/src/Model/Resolver/SaveAddressInformation.php index 28a6335..86f13c6 100644 --- a/src/Model/Resolver/SaveAddressInformation.php +++ b/src/Model/Resolver/SaveAddressInformation.php @@ -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; @@ -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; @@ -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( @@ -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, diff --git a/src/etc/schema.graphqls b/src/etc/schema.graphqls index dc54016..3a7c6a2 100755 --- a/src/etc/schema.graphqls +++ b/src/etc/schema.graphqls @@ -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\\QuoteGraphQl\\Model\\Resolver\\GetStores") } type Mutation { @@ -117,6 +118,12 @@ input AddressInput { company: String street: [String] vat_id: String + extension_attributes: [AddressExtensionAttributes] +} + +input AddressExtensionAttributes { + attribute_code: String + value: String } input EstimateShippingCostsAddress { @@ -376,3 +383,30 @@ type AppliedTaxItemRate { extend input CartAddressInput { vat_id: String } + +type CustomizableFileOption implements CustomizableOptionInterface { + value: CustomizableFileValue +} + +type CustomizableFieldValue { + file_extension: String @doc(description: "Option file extensions (If type file).") + price: Float + 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 +} From 7ba683ec130eedcdfcb04484cc31ecffc6232d98 Mon Sep 17 00:00:00 2001 From: Ivans Zuks Date: Thu, 20 May 2021 17:54:14 +0300 Subject: [PATCH 2/2] Fixed permissions --- src/Model/Resolver/GetStores.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 src/Model/Resolver/GetStores.php diff --git a/src/Model/Resolver/GetStores.php b/src/Model/Resolver/GetStores.php old mode 100644 new mode 100755