SetShippingMethodOnCart.php 3.7 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
use Magento\Checkout\Model\ShippingInformationFactory;
use Magento\Checkout\Api\ShippingInformationManagementInterface;
use Magento\Checkout\Model\ShippingInformation;

/**
 * Class SetShippingMethodsOnCart
 *
 * Set shipping method for a specified shopping cart address
 */
class SetShippingMethodOnCart
{
    /**
     * @var ShippingInformationFactory
     */
    private $shippingInformationFactory;

    /**
     * @var QuoteAddressFactory
     */
    private $quoteAddressFactory;

    /**
     * @var QuoteAddressResource
     */
    private $quoteAddressResource;

    /**
     * @var ShippingInformationManagementInterface
     */
    private $shippingInformationManagement;

    /**
     * @param ShippingInformationManagementInterface $shippingInformationManagement
     * @param QuoteAddressFactory $quoteAddressFactory
     * @param QuoteAddressResource $quoteAddressResource
     * @param ShippingInformationFactory $shippingInformationFactory
     */
    public function __construct(
        ShippingInformationManagementInterface $shippingInformationManagement,
        QuoteAddressFactory $quoteAddressFactory,
        QuoteAddressResource $quoteAddressResource,
        ShippingInformationFactory $shippingInformationFactory
    ) {
        $this->shippingInformationManagement = $shippingInformationManagement;
        $this->quoteAddressResource = $quoteAddressResource;
        $this->quoteAddressFactory = $quoteAddressFactory;
        $this->shippingInformationFactory = $shippingInformationFactory;
    }

    /**
     * Sets shipping method for a specified shopping cart address
     *
     * @param Quote $cart
     * @param int $cartAddressId
     * @param string $carrierCode
     * @param string $methodCode
     * @throws GraphQlInputException
     * @throws GraphQlNoSuchEntityException
     */
    public function execute(Quote $cart, int $cartAddressId, string $carrierCode, string $methodCode): void
    {
        $quoteAddress = $this->quoteAddressFactory->create();
        $this->quoteAddressResource->load($quoteAddress, $cartAddressId);

        /** @var ShippingInformation $shippingInformation */
        $shippingInformation = $this->shippingInformationFactory->create();

        /* If the address is not a shipping address (but billing) the system will find the proper shipping address for
           the selected cart and set the information there (actual for single shipping address) */
        $shippingInformation->setShippingAddress($quoteAddress);
        $shippingInformation->setShippingCarrierCode($carrierCode);
        $shippingInformation->setShippingMethodCode($methodCode);

        try {
            $this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
        } catch (NoSuchEntityException $exception) {
            throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
        } catch (StateException $exception) {
            throw new GraphQlInputException(__($exception->getMessage()));
        } catch (InputException $exception) {
            throw new GraphQlInputException(__($exception->getMessage()));
        }
    }
}