mirror of
https://github.com/bdnugget/magento2.git
synced 2026-11-07 09:17:35 +00:00
* LYNX-145: Add options to item's product data in cart * LYNX-145: Refactring * LYNX-145: Tests refactoring * LYNX-145: Refactoring - CR changes * LYNX-145: Refactoring - CR changes * LYNX-145: Fix static tests * LYNX-145: Added _resetState() for product attributes * LYNX-145: Refactoring * LYNX-145: Refactoring * LYNX-337: Add sorting parameter to products in the cart * LYNX-337: Refactoring * LYNX-337: Refactoring * LYNX-337: Base test case * LYNX-337: WepAPI tests coverage * LYNX-337: Pre-CR refactoring * LYNX-337: Refactoring; fix static tests * LYNX-354: Update estimateShippingMethods GraphQl schema * LYNX-354: Refactoring; bugfixing; * LYNX-354: Tests refactoring * LYNX-354: pre-CR changes * LYNX-336: Multicoupon * LYNX-354: CR changes * LYNX-354: CR changes * LYNX-337: CR refactoring * LYNX-337: Update tests --------- Co-authored-by: Sergii Ivashchenko <serg.ivashchenko@gmail.com>
133 lines
5.2 KiB
PHP
133 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* Copyright 2023 Adobe
|
|
* All Rights Reserved.
|
|
*
|
|
* NOTICE: All information contained herein is, and remains
|
|
* the property of Adobe and its suppliers, if any. The intellectual
|
|
* and technical concepts contained herein are proprietary to Adobe
|
|
* and its suppliers and are protected by all applicable intellectual
|
|
* property laws, including trade secret and copyright laws.
|
|
* Dissemination of this information or reproduction of this material
|
|
* is strictly forbidden unless prior written permission is obtained from
|
|
* Adobe.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace Magento\QuoteGraphQl\Model\Resolver;
|
|
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Magento\Framework\Api\ExtensibleDataObjectConverter;
|
|
use Magento\Framework\GraphQl\Config\Element\Field;
|
|
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
|
|
use Magento\Framework\GraphQl\Query\ResolverInterface;
|
|
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
|
use Magento\Quote\Api\CartRepositoryInterface;
|
|
use Magento\Quote\Api\Data\AddressInterface;
|
|
use Magento\Quote\Api\Data\CartInterface;
|
|
use Magento\Quote\Api\Data\ShippingMethodInterface;
|
|
use Magento\Quote\Api\ShipmentEstimationInterface;
|
|
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
|
|
use Magento\Quote\Model\Quote\AddressFactory;
|
|
use Magento\Quote\Model\Cart\ShippingMethodConverter;
|
|
use Magento\QuoteGraphQl\Model\FormatMoneyTypeData;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
*/
|
|
class EstimateShippingMethods implements ResolverInterface
|
|
{
|
|
/**
|
|
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
|
|
* @param CartRepositoryInterface $cartRepository
|
|
* @param AddressFactory $addressFactory
|
|
* @param ShipmentEstimationInterface $shipmentEstimation
|
|
* @param ExtensibleDataObjectConverter $dataObjectConverter
|
|
* @param ShippingMethodConverter $shippingMethodConverter
|
|
* @param FormatMoneyTypeData $formatMoneyTypeData
|
|
*/
|
|
public function __construct(
|
|
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
|
|
private CartRepositoryInterface $cartRepository,
|
|
private AddressFactory $addressFactory,
|
|
private ShipmentEstimationInterface $shipmentEstimation,
|
|
private ExtensibleDataObjectConverter $dataObjectConverter,
|
|
private ShippingMethodConverter $shippingMethodConverter,
|
|
private FormatMoneyTypeData $formatMoneyTypeData,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
|
|
{
|
|
$this->validateInput($args);
|
|
try {
|
|
$cart = $this->cartRepository->get($this->maskedQuoteIdToQuoteId->execute($args['input']['cart_id']));
|
|
} catch (NoSuchEntityException $ex) {
|
|
throw new GraphQlInputException(
|
|
__(
|
|
'Could not find a cart with ID "%masked_id"',
|
|
[
|
|
'masked_id' => $args['input']['cart_id']
|
|
]
|
|
)
|
|
);
|
|
}
|
|
return $this->getAvailableShippingMethodsForAddress($args['input']['address'], $cart);
|
|
}
|
|
|
|
/**
|
|
* Validates arguments passed to resolver
|
|
*
|
|
* @param array $args
|
|
* @throws GraphQlInputException
|
|
*/
|
|
private function validateInput(array $args)
|
|
{
|
|
if (empty($args['input']['cart_id'])) {
|
|
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
|
|
}
|
|
|
|
if (empty($args['input']['address']['country_code'])) {
|
|
throw new GraphQlInputException(__('Required parameter "country_code" is missing'));
|
|
}
|
|
|
|
if (isset($args['input']['address']['region']) && empty($args['input']['address']['region'])) {
|
|
throw new GraphQlInputException(__('Missing parameter(s) for region input'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the list of available shipping methods given a cart, country_id and optional customer address parameters
|
|
*
|
|
* @param array $args
|
|
* @param CartInterface $cart
|
|
* @return array
|
|
*/
|
|
private function getAvailableShippingMethodsForAddress(array $args, CartInterface $cart): array
|
|
{
|
|
$data = [
|
|
AddressInterface::KEY_COUNTRY_ID => $args['country_code'],
|
|
AddressInterface::KEY_REGION => $args['region'][AddressInterface::KEY_REGION] ?? null,
|
|
AddressInterface::KEY_REGION_ID => $args['region'][AddressInterface::KEY_REGION_ID] ?? null,
|
|
AddressInterface::KEY_REGION_CODE => $args['region'][AddressInterface::KEY_REGION_CODE] ?? null,
|
|
AddressInterface::KEY_POSTCODE => $args[AddressInterface::KEY_POSTCODE] ?? null,
|
|
];
|
|
|
|
/** @var $address AddressInterface */
|
|
$address = $this->addressFactory->create(['data' => array_filter($data)]);
|
|
$shippingMethods = [];
|
|
|
|
foreach ($this->shipmentEstimation->estimateByExtendedAddress($cart->getId(), $address) as $method) {
|
|
$shippingMethods[] = $this->formatMoneyTypeData->execute(
|
|
$this->dataObjectConverter->toFlatArray($method, [], ShippingMethodInterface::class),
|
|
$cart->getCurrency()->getQuoteCurrencyCode()
|
|
);
|
|
}
|
|
return $shippingMethods;
|
|
}
|
|
}
|