Skip to content

Commit

Permalink
https://github.com/tradefurniturecompany/site/issues/271
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-fediuk committed Jul 28, 2023
1 parent 98e16ad commit 56d3258
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
# How to fix the «Invalid template file» / «require_js.phtml» failure of Magento 2.3.x in Windows?
# https://mage2.pro/t/5774
!/vendor/magento/framework/View/Element/Template/File/Validator.php
# 2023-07-28 Dmitrii Fediuk https://upwork.com/fl/mage2pro
# "«Invalid state change requested» on `/onestepcheckout/`": https://github.com/tradefurniturecompany/site/issues/271
!/vendor/magento/module-checkout/Model/ShippingInformationManagement.php
# 2020-02-18 Dmitrii Fediuk https://upwork.com/fl/mage2pro
# "«PayPal gateway has rejected request.
# You do not have permissions to make this API call» / «#10002: Authentication/Authorization Failed»":
Expand Down
259 changes: 259 additions & 0 deletions vendor/magento/module-checkout/Model/ShippingInformationManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Checkout\Model;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Psr\Log\LoggerInterface as Logger;
use Magento\Quote\Model\QuoteAddressValidator;
use Magento\Quote\Api\Data\CartExtensionFactory;
use Magento\Quote\Model\ShippingAssignmentFactory;
use Magento\Quote\Model\ShippingFactory;
use Magento\Framework\App\ObjectManager;

/**
* Class ShippingInformationManagement
*
* @package Magento\Checkout\Model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInformationManagementInterface
{
/**
* @var \Magento\Quote\Api\PaymentMethodManagementInterface
*/
protected $paymentMethodManagement;

/**
* @var PaymentDetailsFactory
*/
protected $paymentDetailsFactory;

/**
* @var \Magento\Quote\Api\CartTotalRepositoryInterface
*/
protected $cartTotalsRepository;

/**
* @var \Magento\Quote\Api\CartRepositoryInterface
*/
protected $quoteRepository;

/**
* @var Logger
*/
protected $logger;

/**
* @var QuoteAddressValidator
*/
protected $addressValidator;

/**
* @var \Magento\Customer\Api\AddressRepositoryInterface
* @deprecated 100.2.0
*/
protected $addressRepository;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
* @deprecated 100.2.0
*/
protected $scopeConfig;

/**
* @var \Magento\Quote\Model\Quote\TotalsCollector
* @deprecated 100.2.0
*/
protected $totalsCollector;

/**
* @var \Magento\Quote\Api\Data\CartExtensionFactory
*/
private $cartExtensionFactory;

/**
* @var \Magento\Quote\Model\ShippingAssignmentFactory
*/
protected $shippingAssignmentFactory;

/**
* @var \Magento\Quote\Model\ShippingFactory
*/
private $shippingFactory;

/**
* Constructor
*
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
* @param \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory
* @param \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
* @param \Magento\Quote\Model\QuoteAddressValidator $addressValidator
* @param Logger $logger
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
* @param CartExtensionFactory|null $cartExtensionFactory
* @param ShippingAssignmentFactory|null $shippingAssignmentFactory
* @param ShippingFactory|null $shippingFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
\Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory,
\Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
QuoteAddressValidator $addressValidator,
Logger $logger,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
CartExtensionFactory $cartExtensionFactory = null,
ShippingAssignmentFactory $shippingAssignmentFactory = null,
ShippingFactory $shippingFactory = null
) {
$this->paymentMethodManagement = $paymentMethodManagement;
$this->paymentDetailsFactory = $paymentDetailsFactory;
$this->cartTotalsRepository = $cartTotalsRepository;
$this->quoteRepository = $quoteRepository;
$this->addressValidator = $addressValidator;
$this->logger = $logger;
$this->addressRepository = $addressRepository;
$this->scopeConfig = $scopeConfig;
$this->totalsCollector = $totalsCollector;
$this->cartExtensionFactory = $cartExtensionFactory ?: ObjectManager::getInstance()
->get(CartExtensionFactory::class);
$this->shippingAssignmentFactory = $shippingAssignmentFactory ?: ObjectManager::getInstance()
->get(ShippingAssignmentFactory::class);
$this->shippingFactory = $shippingFactory ?: ObjectManager::getInstance()
->get(ShippingFactory::class);
}

/**
* Save address information.
*
* @param int $cartId
* @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
* @return \Magento\Checkout\Api\Data\PaymentDetailsInterface
* @throws InputException
* @throws NoSuchEntityException
* @throws StateException
*/
public function saveAddressInformation(
$cartId,
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
) {
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
$this->validateQuote($quote);

$address = $addressInformation->getShippingAddress();
if (!$address || !$address->getCountryId()) {
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
}
if (!$address->getCustomerAddressId()) {
$address->setCustomerAddressId(null);
}

try {
$billingAddress = $addressInformation->getBillingAddress();
if ($billingAddress) {
if (!$billingAddress->getCustomerAddressId()) {
$billingAddress->setCustomerAddressId(null);
}
$this->addressValidator->validateForCart($quote, $billingAddress);
$quote->setBillingAddress($billingAddress);
}

$this->addressValidator->validateForCart($quote, $address);
$carrierCode = $addressInformation->getShippingCarrierCode();
$address->setLimitCarrier($carrierCode);
$methodCode = $addressInformation->getShippingMethodCode();
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);

$quote->setIsMultiShipping(false);

$this->quoteRepository->save($quote);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new InputException(
__('The shipping information was unable to be saved. Verify the input data and try again.')
);
}

$shippingAddress = $quote->getShippingAddress();

if (!$quote->getIsVirtual()
&& !$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())
) {
throw new NoSuchEntityException(
__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode)
);
}

/** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
$paymentDetails = $this->paymentDetailsFactory->create();
$paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
$paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
return $paymentDetails;
}

/**
* Validate quote
*
* @param \Magento\Quote\Model\Quote $quote
* @throws InputException
* @throws NoSuchEntityException
* @return void
*/
protected function validateQuote(\Magento\Quote\Model\Quote $quote)
{
if (0 == $quote->getItemsCount()) {
throw new InputException(
__("The shipping method can't be set for an empty cart. Add an item to cart and try again.")
);
}
}

/**
* Prepare shipping assignment.
*
* @param CartInterface $quote
* @param AddressInterface $address
* @param string $method
* @return CartInterface
*/
private function prepareShippingAssignment(CartInterface $quote, AddressInterface $address, $method)
{
$cartExtension = $quote->getExtensionAttributes();
if ($cartExtension === null) {
$cartExtension = $this->cartExtensionFactory->create();
}

$shippingAssignments = $cartExtension->getShippingAssignments();
if (empty($shippingAssignments)) {
$shippingAssignment = $this->shippingAssignmentFactory->create();
} else {
$shippingAssignment = $shippingAssignments[0];
}

$shipping = $shippingAssignment->getShipping();
if ($shipping === null) {
$shipping = $this->shippingFactory->create();
}

$shipping->setAddress($address);
$shipping->setMethod($method);
$shippingAssignment->setShipping($shipping);
$cartExtension->setShippingAssignments([$shippingAssignment]);
return $quote->setExtensionAttributes($cartExtension);
}
}

0 comments on commit 56d3258

Please # to comment.