![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventoryCheckout/Plugin/Model/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See COPYING.txt for license details. */ namespace Ecombricks\InventoryCheckout\Plugin\Model; /** * Shipping information management plugin */ class ShippingInformationManagement { /** * Payment method management * * @var \Magento\Quote\Api\PaymentMethodManagementInterface */ protected $paymentMethodManagement; /** * Payment details factory * * @var \Magento\Checkout\Model\PaymentDetailsFactory */ protected $paymentDetailsFactory; /** * Cart total repository * * @var \Magento\Quote\Api\CartTotalRepositoryInterface */ protected $cartTotalsRepository; /** * Quote repository * * @var \Magento\Quote\Api\CartRepositoryInterface */ protected $quoteRepository; /** * Address validator * * @var \Magento\Quote\Model\QuoteAddressValidator */ protected $addressValidator; /** * Cart extension factory * * @var \Magento\Quote\Api\Data\CartExtensionFactory */ protected $cartExtensionFactory; /** * Shipping assignment actory * * @var \Magento\Quote\Model\ShippingAssignmentFactory */ protected $shippingAssignmentFactory; /** * Shipping factory * * @var \Magento\Quote\Model\ShippingFactory */ protected $shippingFactory; /** * Logger * * @var \Psr\Log\LoggerInterface */ protected $logger; /** * Joint data * * @var \Ecombricks\InventoryQuote\Data\JointData */ protected $jointData; /** * Shipping method full code * * @var \Ecombricks\InventoryQuote\Data\ShippingMethodFullCode */ protected $shippingMethodFullCode; /** * 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 \Magento\Quote\Api\Data\CartExtensionFactory $cartExtensionFactory * @param \Magento\Quote\Model\ShippingAssignmentFactory $shippingAssignmentFactory * @param \Magento\Quote\Model\ShippingFactory $shippingFactory * @param \Psr\Log\LoggerInterface $logger * @param \Ecombricks\InventoryQuote\Data\JointData $jointData * @param \Ecombricks\InventoryQuote\Data\ShippingMethodFullCode $shippingMethodFullCode * @return void */ public function __construct( \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement, \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory, \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Quote\Model\QuoteAddressValidator $addressValidator, \Magento\Quote\Api\Data\CartExtensionFactory $cartExtensionFactory, \Magento\Quote\Model\ShippingAssignmentFactory $shippingAssignmentFactory, \Magento\Quote\Model\ShippingFactory $shippingFactory, \Psr\Log\LoggerInterface $logger, \Ecombricks\InventoryQuote\Data\JointData $jointData, \Ecombricks\InventoryQuote\Data\ShippingMethodFullCode $shippingMethodFullCode ) { $this->paymentMethodManagement = $paymentMethodManagement; $this->paymentDetailsFactory = $paymentDetailsFactory; $this->cartTotalsRepository = $cartTotalsRepository; $this->quoteRepository = $quoteRepository; $this->addressValidator = $addressValidator; $this->cartExtensionFactory = $cartExtensionFactory; $this->shippingAssignmentFactory = $shippingAssignmentFactory; $this->shippingFactory = $shippingFactory; $this->logger = $logger; $this->jointData = $jointData; $this->shippingMethodFullCode = $shippingMethodFullCode; } /** * Validate quote items count * * @param \Magento\Quote\Model\Quote $quote * @return $this * @throws \Magento\Framework\Exception\InputException */ protected function validateQuoteItemsCount(\Magento\Quote\Model\Quote $quote) { if (0 == $quote->getItemsCount()) { throw new \Magento\Framework\Exception\InputException( __("The shipping method can't be set for an empty cart. Add an item to cart and try again.") ); } return $this; } /** * Prepare shipping address * * @param \Magento\Quote\Api\Data\AddressInterface $shippingAddress * @return $this * @throws \Magento\Framework\Exception\StateException */ protected function prepareShippingAddress(\Magento\Quote\Api\Data\AddressInterface $shippingAddress) { if (!$shippingAddress || !$shippingAddress->getCountryId()) { throw new \Magento\Framework\Exception\StateException(__('The shipping address is missing. Set the address and try again.')); } if (!$shippingAddress->getCustomerAddressId()) { $shippingAddress->setCustomerAddressId(null); } return $this; } /** * Prepare billing address * * @param \Magento\Quote\Api\Data\AddressInterface $billingAddress * @return $this * @throws \Magento\Framework\Exception\StateException */ protected function prepareBillingAddress(\Magento\Quote\Api\Data\AddressInterface $billingAddress) { if (!$billingAddress->getCustomerAddressId()) { $billingAddress->setCustomerAddressId(null); } return $this; } /** * Get shipping carrier codes * * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation * @return array */ protected function getShippingCarrierCodes(\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation) { return $this->jointData->parse($addressInformation->getShippingCarrierCode()); } /** * Get shipping method codes * * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation * @return array */ protected function getShippingMethodCodes(\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation) { return $this->jointData->parse($addressInformation->getShippingMethodCode()); } /** * Get shipping methods * * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation * @return array */ protected function getShippingMethods(\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation) { $shippingMethods = []; $shippingCarrierCodes = $this->getShippingCarrierCodes($addressInformation); $shippingMethodCodes = $this->getShippingMethodCodes($addressInformation); foreach ($shippingCarrierCodes as $sourceCode => $shippingCarrierCode) { if (empty($shippingMethodCodes[$sourceCode])) { continue; } $shippingMethods[$sourceCode] = $this->shippingMethodFullCode->generate($shippingCarrierCode, $shippingMethodCodes[$sourceCode]); } return $shippingMethods; } /** * Prepare shipping assignment * * @param \Magento\Quote\Api\Data\CartInterface $quote * @param \Magento\Quote\Api\Data\AddressInterface $shippingAddress * @param array $shippingMethods * @return $this */ protected function prepareShippingAssignment( \Magento\Quote\Api\Data\CartInterface $quote, \Magento\Quote\Api\Data\AddressInterface $shippingAddress, $shippingMethods ) { $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($shippingAddress); $shipping->setMethod($shippingMethods); $shippingAssignment->setShipping($shipping); $cartExtension->setShippingAssignments([$shippingAssignment]); $quote->setExtensionAttributes($cartExtension); return $this; } /** * Validate shipping rates * * @param \Magento\Quote\Api\Data\CartInterface $quote * @return $this * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function validateShippingRates(\Magento\Quote\Api\Data\CartInterface $quote) { $shippingAddress = $quote->getShippingAddress(); if (!$quote->getIsVirtual() && !$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Carriers with such methods not found: %1', $shippingAddress->getShippingMethod())); } return $this; } /** * Create payment details * * @param int $cartId * @return \Magento\Checkout\Api\Data\PaymentDetailsInterface */ protected function createPaymentDetails($cartId) { $paymentDetails = $this->paymentDetailsFactory->create(); $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId)); $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId)); return $paymentDetails; } /** * Save address information * * @param int $cartId * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation * @return \Magento\Checkout\Api\Data\PaymentDetailsInterface * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\StateException */ public function saveAddressInformation( $cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation ) { $quote = $this->quoteRepository->getActive($cartId); $this->validateQuoteItemsCount($quote); $shippingAddress = $addressInformation->getShippingAddress(); $this->prepareShippingAddress($shippingAddress); try { $billingAddress = $addressInformation->getBillingAddress(); if ($billingAddress) { $this->prepareBillingAddress($billingAddress); $this->addressValidator->validateForCart($quote, $billingAddress); $quote->setBillingAddress($billingAddress); } $this->addressValidator->validateForCart($quote, $shippingAddress); $shippingAddress->setLimitCarrier($this->getShippingCarrierCodes($addressInformation)); $this->prepareShippingAssignment($quote, $shippingAddress, $this->getShippingMethods($addressInformation)); $quote->setIsMultiShipping(false); $this->quoteRepository->save($quote); } catch (\Magento\Framework\Exception\LocalizedException $exception) { $this->logger->critical($exception); throw new \Magento\Framework\Exception\InputException( __('The shipping information was unable to be saved. Error: "%message"', ['message' => $e->getMessage()]) ); } catch (\Exception $exception) { $this->logger->critical($exception); throw new \Magento\Framework\Exception\InputException( __('The shipping information was unable to be saved. Verify the input data and try again.') ); } $this->validateShippingRates($quote); return $this->createPaymentDetails($cartId); } /** * Around save address information * * @param \Magento\Checkout\Model\ShippingInformationManagement $subject * @param \Closure $proceed * @param integer $cartId * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation * @return \Magento\Checkout\Api\Data\PaymentDetailsInterface * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\StateException */ public function aroundSaveAddressInformation( \Magento\Checkout\Model\ShippingInformationManagement $subject, \Closure $proceed, $cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation ) { return $this->saveAddressInformation($cartId, $addressInformation); } }