![]() 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/InventoryQuote/Model/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ namespace Ecombricks\InventoryQuote\Model; /** * Shipping method management */ class ShippingMethodManagement extends \Magento\Quote\Model\ShippingMethodManagement implements \Ecombricks\InventoryQuote\Api\ShippingMethodManagementInterface, \Ecombricks\InventoryQuote\Model\ShippingMethodManagementInterface, \Ecombricks\InventoryQuote\Api\ShipmentEstimationInterface { /** * Data processor * * @var \Magento\Framework\Reflection\DataObjectProcessor */ protected $dataProcessor; /** * Address factory * * @var \Magento\Customer\Api\Data\AddressInterfaceFactory */ protected $addressFactory; /** * Quote address resource * * @var \Magento\Quote\Model\ResourceModel\Quote\Address */ protected $quoteAddressResource; /** * Customer session * * @var \Magento\Customer\Model\Session */ protected $customerSession; /** * 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\CartRepositoryInterface $quoteRepository * @param \Magento\Quote\Model\Cart\ShippingMethodConverter $converter * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository * @param \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector * @param \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor * @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory * @param \Magento\Quote\Model\ResourceModel\Quote\Address $quoteAddressResource * @param \Magento\Customer\Model\Session $customerSession * @param \Ecombricks\InventoryQuote\Data\JointData $jointData * @param \Ecombricks\InventoryQuote\Data\ShippingMethodFullCode $shippingMethodFullCode * @return void */ public function __construct( \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Quote\Model\Cart\ShippingMethodConverter $converter, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository, \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector, \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor, \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory, \Magento\Quote\Model\ResourceModel\Quote\Address $quoteAddressResource, \Magento\Customer\Model\Session $customerSession, \Ecombricks\InventoryQuote\Data\JointData $jointData, \Ecombricks\InventoryQuote\Data\ShippingMethodFullCode $shippingMethodFullCode ) { parent::__construct( $quoteRepository, $converter, $addressRepository, $totalsCollector, $addressFactory, $quoteAddressResource, $customerSession ); $this->dataProcessor = $dataProcessor; $this->addressFactory = $addressFactory; $this->quoteAddressResource = $quoteAddressResource; $this->customerSession = $customerSession; $this->jointData = $jointData; $this->shippingMethodFullCode = $shippingMethodFullCode; } /** * Get * * @param int $cartId * @return \Ecombricks\InventoryQuote\Api\Data\ShippingMethodInterface|null * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\StateException */ public function get($cartId) { return null; } /** * Get * * @param integer $cartId * @return \Ecombricks\InventoryQuote\Api\Data\ShippingMethodInterface[] * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\StateException */ public function inventoryGet($cartId) { $quote = $this->quoteRepository->getActive($cartId); $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getCountryId()) { throw new \Magento\Framework\Exception\StateException(__('The shipping address is missing. Set the address and try again.')); } $shippingMethods = $shippingAddress->getShippingMethod(); if (empty($shippingMethods)) { return []; } $shippingAddress->collectShippingRates(); $shippingRates = $shippingAddress->getShippingRateByCode($shippingMethods); if (empty($shippingRates)) { return []; } $output = []; $quoteCurrencyCode = $quote->getQuoteCurrencyCode(); foreach ($shippingRates as $shippingRate) { $output[] = $this->converter->modelToDataObject($shippingRate, $quoteCurrencyCode); } return $output; } /** * Get list * * @param integer $cartId * @return \Ecombricks\InventoryQuote\Api\Data\ShippingMethodInterface[] * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\StateException */ public function getList($cartId) { $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual() || 0 == $quote->getItemsCount()) { return []; } $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getCountryId()) { throw new \Magento\Framework\Exception\StateException(__('The shipping address is missing. Set the address and try again.')); } $shippingAddress->collectShippingRates(); $shippingRates = $shippingAddress->getGroupedAllShippingRates(); if (empty($shippingRates)) { return []; } $output = []; $quoteCurrencyCode = $quote->getQuoteCurrencyCode(); foreach ($shippingRates as $sourceShippingRates) { foreach ($sourceShippingRates as $carrierShippingRates) { foreach ($carrierShippingRates as $shippingRate) { $object = $this->converter->modelToDataObject($shippingRate, $quoteCurrencyCode); $output[] = $object; } } } return $output; } /** * Apply * * @param int $cartId * @param string $carrierCode * @param string $methodCode * @return void * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws StateException * @throws \Exception */ public function apply($cartId, $carrierCode, $methodCode) { $quote = $this->quoteRepository->getActive($cartId); 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.') ); } if ($quote->isVirtual()) { throw new \Magento\Framework\Exception\NoSuchEntityException( __('The Cart includes virtual product(s) only, so a shipping address is not used.') ); } $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getCountryId()) { $this->quoteAddressResource->delete($shippingAddress); throw new \Magento\Framework\Exception\StateException(__('The shipping address is missing. Set the address and try again.')); } $shippingMethods = []; $shippingCarrierCodes = $this->jointData->parse($carrierCode); $shippingMethodCodes = $this->jointData->parse($methodCode); foreach ($shippingCarrierCodes as $sourceCode => $shippingCarrierCode) { if (empty($shippingMethodCodes[$sourceCode])) { continue; } $shippingMethods[$sourceCode] = $this->shippingMethodFullCode->generate($shippingCarrierCode, $shippingMethodCodes[$sourceCode]); } $shippingAddress->setShippingMethod($shippingMethods); } /** * Estimate by address * * @param mixed $cartId * @param \Magento\Quote\Api\Data\EstimateAddressInterface $address * @return \Ecombricks\InventoryQuote\Api\Data\ShippingMethodInterface[] */ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddressInterface $address) { $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual() || 0 == $quote->getItemsCount()) { return []; } return $this->getShippingMethods($quote, $address); } /** * Estimate by extended address * * @param mixed $cartId * @param \Magento\Quote\Api\Data\AddressInterface $address * @return \Ecombricks\InventoryQuote\Api\Data\ShippingMethodInterface[] */ public function estimateByExtendedAddress($cartId, \Magento\Quote\Api\Data\AddressInterface $address) { $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual() || 0 == $quote->getItemsCount()) { return []; } return $this->getShippingMethods($quote, $address); } /** * Estimate by address ID * * @param mixed $cartId * @param integer $addressId * @return \Ecombricks\InventoryQuote\Api\Data\ShippingMethodInterface[] */ public function estimateByAddressId($cartId, $addressId) { $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual() || 0 == $quote->getItemsCount()) { return []; } $address = $this->addressRepository->getById($addressId); return $this->getShippingMethods($quote, $address); } /** * Get shipping methods * * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Framework\Api\ExtensibleDataInterface $address * @return \Ecombricks\InventoryQuote\Api\Data\ShippingMethodInterface[] */ protected function getShippingMethods(\Magento\Quote\Model\Quote $quote, $address) { $output = []; $shippingAddress = $quote->getShippingAddress(); $shippingAddress->addData($this->extractAddressData($address)); $shippingAddress->setCollectShippingRates(true); $this->totalsCollector->collectAddressTotals($quote, $shippingAddress); $quoteCustomerGroupId = $quote->getCustomerGroupId(); $customerGroupId = $this->customerSession->getCustomerGroupId(); $isCustomerGroupChanged = $quoteCustomerGroupId !== $customerGroupId; if ($isCustomerGroupChanged) { $quote->setCustomerGroupId($customerGroupId); } $shippingRates = $shippingAddress->getGroupedAllShippingRates(); $quoteCurrencyCode = $quote->getQuoteCurrencyCode(); foreach ($shippingRates as $sourceShippingRates) { foreach ($sourceShippingRates as $carrierShippingRates) { foreach ($carrierShippingRates as $shippingRate) { $object = $this->converter->modelToDataObject($shippingRate, $quoteCurrencyCode); $output[] = $object; } } } if ($isCustomerGroupChanged) { $quote->setCustomerGroupId($quoteCustomerGroupId); } return $output; } /** * Extract address data * * @param \Magento\Framework\Api\ExtensibleDataInterface $address * @return array */ protected function extractAddressData($address) { $className = \Magento\Customer\Api\Data\AddressInterface::class; if ($address instanceof \Magento\Quote\Api\Data\AddressInterface) { $className = \Magento\Quote\Api\Data\AddressInterface::class; } elseif ($address instanceof \Magento\Quote\Api\Data\EstimateAddressInterface) { $className = \Magento\Quote\Api\Data\EstimateAddressInterface::class; } return $this->dataProcessor->buildOutputDataArray($address, $className); } }