Skip to content

Commit

Permalink
MC-33138: Extend using Magento Recommendations failed
Browse files Browse the repository at this point in the history
  • Loading branch information
serhii-balko committed Apr 8, 2020
1 parent a202112 commit fcd1b58
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 52 deletions.
73 changes: 42 additions & 31 deletions app/code/Magento/Quote/Model/ShippingMethodManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Quote\Model;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\Api\ExtensibleDataInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\EstimateAddressInterface;
use Magento\Quote\Api\Data\ShippingMethodInterface;
use Magento\Quote\Api\ShipmentEstimationInterface;
use Magento\Quote\Model\Cart\ShippingMethodConverter;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Address\Rate;
use Magento\Quote\Model\Quote\TotalsCollector;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;

/**
Expand All @@ -33,21 +40,21 @@ class ShippingMethodManagement implements
/**
* Quote repository.
*
* @var \Magento\Quote\Api\CartRepositoryInterface
* @var CartRepositoryInterface
*/
protected $quoteRepository;

/**
* Shipping method converter
*
* @var \Magento\Quote\Model\Cart\ShippingMethodConverter
* @var ShippingMethodConverter
*/
protected $converter;

/**
* Customer Address repository
*
* @var \Magento\Customer\Api\AddressRepositoryInterface
* @var AddressRepositoryInterface
*/
protected $addressRepository;

Expand All @@ -57,7 +64,7 @@ class ShippingMethodManagement implements
protected $totalsCollector;

/**
* @var \Magento\Framework\Reflection\DataObjectProcessor $dataProcessor
* @var DataObjectProcessor $dataProcessor
*/
private $dataProcessor;

Expand All @@ -79,19 +86,19 @@ class ShippingMethodManagement implements
/**
* Constructor
*
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
* @param CartRepositoryInterface $quoteRepository
* @param Cart\ShippingMethodConverter $converter
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
* @param AddressRepositoryInterface $addressRepository
* @param Quote\TotalsCollector $totalsCollector
* @param AddressInterfaceFactory|null $addressFactory
* @param QuoteAddressResource|null $quoteAddressResource
* @param CustomerSession|null $customerSession
*/
public function __construct(
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
CartRepositoryInterface $quoteRepository,
Cart\ShippingMethodConverter $converter,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Quote\Model\Quote\TotalsCollector $totalsCollector,
AddressRepositoryInterface $addressRepository,
TotalsCollector $totalsCollector,
AddressInterfaceFactory $addressFactory = null,
QuoteAddressResource $quoteAddressResource = null,
CustomerSession $customerSession = null
Expand All @@ -112,10 +119,10 @@ public function __construct(
*/
public function get($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
/** @var Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);

/** @var \Magento\Quote\Model\Quote\Address $shippingAddress */
/** @var Address $shippingAddress */
$shippingAddress = $quote->getShippingAddress();
if (!$shippingAddress->getCountryId()) {
throw new StateException(__('The shipping address is missing. Set the address and try again.'));
Expand All @@ -127,7 +134,7 @@ public function get($cartId)
}

$shippingAddress->collectShippingRates();
/** @var \Magento\Quote\Model\Quote\Address\Rate $shippingRate */
/** @var Rate $shippingRate */
$shippingRate = $shippingAddress->getShippingRateByCode($shippingMethod);
if (!$shippingRate) {
return null;
Expand All @@ -142,7 +149,7 @@ public function getList($cartId)
{
$output = [];

/** @var \Magento\Quote\Model\Quote $quote */
/** @var Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);

// no methods applicable for empty carts or carts with virtual products
Expand All @@ -169,7 +176,7 @@ public function getList($cartId)
*/
public function set($cartId, $carrierCode, $methodCode)
{
/** @var \Magento\Quote\Model\Quote $quote */
/** @var Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
try {
$this->apply($cartId, $carrierCode, $methodCode);
Expand Down Expand Up @@ -199,7 +206,7 @@ public function set($cartId, $carrierCode, $methodCode)
*/
public function apply($cartId, $carrierCode, $methodCode)
{
/** @var \Magento\Quote\Model\Quote $quote */
/** @var Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if (0 == $quote->getItemsCount()) {
throw new InputException(
Expand All @@ -223,9 +230,9 @@ public function apply($cartId, $carrierCode, $methodCode)
/**
* @inheritDoc
*/
public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddressInterface $address)
public function estimateByAddress($cartId, EstimateAddressInterface $address)
{
/** @var \Magento\Quote\Model\Quote $quote */
/** @var Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);

// no methods applicable for empty carts or carts with virtual products
Expand All @@ -241,7 +248,7 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
*/
public function estimateByExtendedAddress($cartId, AddressInterface $address)
{
/** @var \Magento\Quote\Model\Quote $quote */
/** @var Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);

// no methods applicable for empty carts or carts with virtual products
Expand All @@ -256,7 +263,7 @@ public function estimateByExtendedAddress($cartId, AddressInterface $address)
*/
public function estimateByAddressId($cartId, $addressId)
{
/** @var \Magento\Quote\Model\Quote $quote */
/** @var Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);

// no methods applicable for empty carts or carts with virtual products
Expand All @@ -276,13 +283,13 @@ public function estimateByAddressId($cartId, $addressId)
* @param string $postcode
* @param int $regionId
* @param string $region
* @param \Magento\Framework\Api\ExtensibleDataInterface|null $address
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
* @param ExtensibleDataInterface|null $address
* @return ShippingMethodInterface[] An array of shipping methods.
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @deprecated 100.2.0
*/
protected function getEstimatedRates(
\Magento\Quote\Model\Quote $quote,
Quote $quote,
$country,
$postcode,
$regionId,
Expand All @@ -301,9 +308,9 @@ protected function getEstimatedRates(
/**
* Get list of available shipping methods
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Framework\Api\ExtensibleDataInterface $address
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
* @param Quote $quote
* @param ExtensibleDataInterface $address
* @return ShippingMethodInterface[]
*/
private function getShippingMethods(Quote $quote, $address)
{
Expand Down Expand Up @@ -334,27 +341,31 @@ private function getShippingMethods(Quote $quote, $address)
/**
* Get transform address interface into Array
*
* @param \Magento\Framework\Api\ExtensibleDataInterface $address
* @param ExtensibleDataInterface $address
* @return array
*/
private 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;
if ($address instanceof AddressInterface) {
$className = AddressInterface::class;
} elseif ($address instanceof EstimateAddressInterface) {
$className = EstimateAddressInterface::class;
}
return $this->getDataObjectProcessor()->buildOutputDataArray(

$addressData = $this->getDataObjectProcessor()->buildOutputDataArray(
$address,
$className
);
unset($addressData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);

return $addressData;
}

/**
* Gets the data object processor
*
* @return \Magento\Framework\Reflection\DataObjectProcessor
* @return DataObjectProcessor
* @deprecated 100.2.0
*/
private function getDataObjectProcessor()
Expand Down
Loading

0 comments on commit fcd1b58

Please sign in to comment.