![]() 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/old/app/code/Cnc/Checkout/Plugin/Model/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Radosław Stępień <[email protected]> <[email protected]> */ namespace Cnc\Checkout\Plugin\Model; use Ecombricks\InventoryQuote\Data\JointData; use Ecombricks\InventoryQuote\Data\ShippingMethodFullCode; use Magento\Checkout\Api\Data\PaymentDetailsInterface; use Magento\Checkout\Api\Data\ShippingInformationInterface; use Magento\Checkout\Model\PaymentDetailsFactory; use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Exception\StateException; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Api\CartTotalRepositoryInterface; use Magento\Quote\Api\Data\AddressInterface; use Magento\Quote\Api\Data\CartExtensionFactory; use Magento\Quote\Api\Data\CartInterface; use Magento\Quote\Api\PaymentMethodManagementInterface; use Magento\Quote\Model\Quote; use Magento\Quote\Model\QuoteAddressValidator; use Magento\Quote\Model\ShippingAssignmentFactory; use Magento\Quote\Model\ShippingFactory; use Psr\Log\LoggerInterface; /** * @Override override whole plugin class from Ecombricks module due to impossibility to override * around plugin declared here. */ class ShippingInformationManagement { /** * Payment method management * * @var PaymentMethodManagementInterface */ protected $paymentMethodManagement; /** * Payment details factory * * @var PaymentDetailsFactory */ protected $paymentDetailsFactory; /** * Cart total repository * * @var CartTotalRepositoryInterface */ protected $cartTotalsRepository; /** * Quote repository * * @var CartRepositoryInterface */ protected $quoteRepository; /** * Address validator * * @var QuoteAddressValidator */ protected $addressValidator; /** * Cart extension factory * * @var CartExtensionFactory */ protected $cartExtensionFactory; /** * Shipping assignment actory * * @var ShippingAssignmentFactory */ protected $shippingAssignmentFactory; /** * Shipping factory * * @var ShippingFactory */ protected $shippingFactory; /** * Logger * * @var LoggerInterface */ protected $logger; /** * Joint data * * @var JointData */ protected $jointData; /** * Shipping method full code * * @var ShippingMethodFullCode */ protected $shippingMethodFullCode; /** * Constructor * * @param PaymentMethodManagementInterface $paymentMethodManagement * @param PaymentDetailsFactory $paymentDetailsFactory * @param CartTotalRepositoryInterface $cartTotalsRepository * @param CartRepositoryInterface $quoteRepository * @param QuoteAddressValidator $addressValidator * @param CartExtensionFactory $cartExtensionFactory * @param ShippingAssignmentFactory $shippingAssignmentFactory * @param ShippingFactory $shippingFactory * @param LoggerInterface $logger * @param JointData $jointData * @param ShippingMethodFullCode $shippingMethodFullCode * @return void */ public function __construct( PaymentMethodManagementInterface $paymentMethodManagement, PaymentDetailsFactory $paymentDetailsFactory, CartTotalRepositoryInterface $cartTotalsRepository, CartRepositoryInterface $quoteRepository, QuoteAddressValidator $addressValidator, CartExtensionFactory $cartExtensionFactory, ShippingAssignmentFactory $shippingAssignmentFactory, ShippingFactory $shippingFactory, LoggerInterface $logger, JointData $jointData, 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 Quote $quote * @return \Ecombricks\InventoryCheckout\Plugin\Model\ShippingInformationManagement * @throws InputException */ protected function validateQuoteItemsCount(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.") ); } return $this; } /** * Prepare shipping address * * @param AddressInterface $shippingAddress * @return $this * @throws StateException */ protected function prepareShippingAddress(AddressInterface $shippingAddress) { if (!$shippingAddress || !$shippingAddress->getCountryId()) { throw new StateException(__('The shipping address is missing. Set the address and try again.')); } if (!$shippingAddress->getCustomerAddressId()) { $shippingAddress->setCustomerAddressId(null); } return $this; } /** * Prepare billing address * * @param AddressInterface $billingAddress * @return $this * @throws StateException */ protected function prepareBillingAddress(AddressInterface $billingAddress) { if (!$billingAddress->getCustomerAddressId()) { $billingAddress->setCustomerAddressId(null); } return $this; } /** * Get shipping carrier codes * * @param ShippingInformationInterface $addressInformation * @return array */ protected function getShippingCarrierCodes(ShippingInformationInterface $addressInformation) { return $this->jointData->parse($addressInformation->getShippingCarrierCode()); } /** * Get shipping method codes * * @param ShippingInformationInterface $addressInformation * @return array */ protected function getShippingMethodCodes(ShippingInformationInterface $addressInformation) { return $this->jointData->parse($addressInformation->getShippingMethodCode()); } /** * Get shipping methods * * @param ShippingInformationInterface $addressInformation * @return array */ protected function getShippingMethods(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 CartInterface $quote * @param AddressInterface $shippingAddress * @param array $shippingMethods * @return $this */ protected function prepareShippingAssignment( CartInterface $quote, 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 CartInterface $quote * @return $this * @throws NoSuchEntityException */ protected function validateShippingRates(CartInterface $quote) { $shippingAddress = $quote->getShippingAddress(); if (!$quote->getIsVirtual() && !$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) { throw new NoSuchEntityException(__('Carriers with such methods not found: %1', $shippingAddress->getShippingMethod())); } return $this; } /** * Create payment details * * @param int $cartId * @return 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 ShippingInformationInterface $addressInformation * @return PaymentDetailsInterface * @throws InputException * @throws NoSuchEntityException * @throws StateException */ public function saveAddressInformation( $cartId, ShippingInformationInterface $addressInformation ) { $quote = $this->quoteRepository->getActive($cartId); // KDC modification start - save customer carriers data, rest of plugin logic is moved from // Ecombricks module because of impossibility to override $extAttributes = $addressInformation->getExtensionAttributes(); $cncCustomerCarrierIdFr = $extAttributes->getCncCustomerCarrierIdFr(); $cncCustomerCarrierNumberFr = $extAttributes->getCncCustomerCarrierNumberFr(); $cncCustomerCarrierIdUs = $extAttributes->getCncCustomerCarrierIdUs(); $cncCustomerCarrierNumberUs = $extAttributes->getCncCustomerCarrierNumberUs(); $quote->setCncCustomerCarrierIdFr($cncCustomerCarrierIdFr); $quote->setCncCustomerCarrierNumberFr($cncCustomerCarrierNumberFr); $quote->setCncCustomerCarrierIdUs($cncCustomerCarrierIdUs); $quote->setCncCustomerCarrierNumberUs($cncCustomerCarrierNumberUs); // KDC modification end $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 InputException( __('The shipping information was unable to be saved. Error: "%message"', ['message' => $exception->getMessage()]) ); } catch (\Exception $exception) { $this->logger->critical($exception); throw new 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 ShippingInformationInterface $addressInformation * @return PaymentDetailsInterface * @throws InputException * @throws NoSuchEntityException * @throws StateException */ public function aroundSaveAddressInformation( \Magento\Checkout\Model\ShippingInformationManagement $subject, \Closure $proceed, $cartId, ShippingInformationInterface $addressInformation ) { return $this->saveAddressInformation($cartId, $addressInformation); } }