Skip to content

Commit

Permalink
MAGETWO-52308: [Github] Not full request information in collectRates(…
Browse files Browse the repository at this point in the history
…) method on Checkout page

 -  Added api method to estimate shipping methods by extended address data
  • Loading branch information
joni-jones committed May 14, 2016
1 parent 5967155 commit d10867f
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@ define(
serviceUrl = resourceUrlManager.getUrlForEstimationShippingMethodsForNewAddress(quote),
payload = JSON.stringify({
address: {
country_id: address.countryId,
region_id: address.regionId,
region: address.region,
postcode: address.postcode
'street': address.street,
'city': address.city,
'region_id': address.regionId,
'region': address.region,
'country_id': address.countryId,
'postcode': address.postcode,
'email': address.email,
'customer_id': address.customerId,
'firstname': address.firstname,
'lastname': address.lastname,
'middlename': address.middlename,
'prefix': address.prefix,
'suffix': address.suffix,
'vat_id': address.vatId,
'company': address.company,
'telephone': address.telephone,
'fax': address.fax,
'custom_attributes': address.customAttributes,
'save_in_address_book': address.saveInAddressBook
}
}
);
Expand Down
12 changes: 12 additions & 0 deletions app/code/Magento/Quote/Api/GuestShipmentEstimationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Api;

/**
* Interface GuestShipmentEstimationInterface
* @api
*/
interface GuestShipmentEstimationInterface extends ShipmentEstimationInterface{}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getList($cartId);
* @param string $cartId The shopping cart ID.
* @param \Magento\Quote\Api\Data\EstimateAddressInterface $address The estimate address
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
* @deprecated
*/
public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddressInterface $address);
}
23 changes: 23 additions & 0 deletions app/code/Magento/Quote/Api/ShipmentEstimationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Api;

use Magento\Quote\Api\Data\AddressInterface;

/**
* Interface ShipmentManagementInterface
* @api
*/
interface ShipmentEstimationInterface
{
/**
* Estimate shipping by address and return list of available shipping methods
* @param mixed $cartId
* @param AddressInterface $address
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods
*/
public function estimateByExtendedAddress($cartId, AddressInterface $address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface ShippingMethodManagementInterface
* @param int $cartId The shopping cart ID.
* @param \Magento\Quote\Api\Data\EstimateAddressInterface $address The estimate address
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
* @deprecated
*/
public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddressInterface $address);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

namespace Magento\Quote\Model\GuestCart;

use Magento\Framework\App\ObjectManager;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\GuestShipmentEstimationInterface;
use Magento\Quote\Api\ShipmentEstimationInterface;
use Magento\Quote\Api\ShippingMethodManagementInterface;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\QuoteIdMaskFactory;
Expand All @@ -15,7 +19,8 @@
*/
class GuestShippingMethodManagement implements
\Magento\Quote\Api\GuestShippingMethodManagementInterface,
\Magento\Quote\Model\GuestCart\GuestShippingMethodManagementInterface
\Magento\Quote\Model\GuestCart\GuestShippingMethodManagementInterface,
GuestShipmentEstimationInterface
{
/**
* @var ShippingMethodManagementInterface
Expand All @@ -27,6 +32,11 @@ class GuestShippingMethodManagement implements
*/
private $quoteIdMaskFactory;

/**
* @var ShipmentEstimationInterface
*/
private $shipmentEstimationManagement;

/**
* Constructs a shipping method read service object.
*
Expand Down Expand Up @@ -80,4 +90,30 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
return $this->shippingMethodManagement->estimateByAddress($quoteIdMask->getQuoteId(), $address);
}

/**
* @inheritdoc
*/
public function estimateByExtendedAddress($cartId, AddressInterface $address)
{
/** @var $quoteIdMask QuoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');

return $this->getShipmentEstimationManagement()
->estimateByExtendedAddress((int) $quoteIdMask->getQuoteId(), $address);
}

/**
* Get shipment estimation management service
* @return ShipmentEstimationInterface
* @deprecated
*/
private function getShipmentEstimationManagement()
{
if ($this->shipmentEstimationManagement === null) {
$this->shipmentEstimationManagement = ObjectManager::getInstance()
->get(ShipmentEstimationInterface::class);
}
return $this->shipmentEstimationManagement;
}
}
49 changes: 42 additions & 7 deletions app/code/Magento/Quote/Model/ShippingMethodManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
*/
namespace Magento\Quote\Model;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\StateException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\EstimateAddressInterface;
use Magento\Quote\Api\ShipmentEstimationInterface;
use Magento\Quote\Model\Quote;

/**
* Shipping method read service.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ShippingMethodManagement implements
\Magento\Quote\Api\ShippingMethodManagementInterface,
\Magento\Quote\Model\ShippingMethodManagementInterface
\Magento\Quote\Model\ShippingMethodManagementInterface,
ShipmentEstimationInterface
{
/**
* Quote repository.
Expand Down Expand Up @@ -193,6 +198,21 @@ public function estimateByAddress($cartId, \Magento\Quote\Api\Data\EstimateAddre
);
}

/**
* @inheritdoc
*/
public function estimateByExtendedAddress($cartId, AddressInterface $address)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);

// no methods applicable for empty carts or carts with virtual products
if ($quote->isVirtual() || 0 == $quote->getItemsCount()) {
return [];
}
return $this->getShippingMethods($quote, $address->getData());
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -227,14 +247,29 @@ public function estimateByAddressId($cartId, $addressId)
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[] An array of shipping methods.
*/
protected function getEstimatedRates(\Magento\Quote\Model\Quote $quote, $country, $postcode, $regionId, $region)
{
$data = [
EstimateAddressInterface::KEY_COUNTRY_ID => $country,
EstimateAddressInterface::KEY_POSTCODE => $postcode,
EstimateAddressInterface::KEY_REGION_ID => $regionId,
EstimateAddressInterface::KEY_REGION => $region
];
return $this->getShippingMethods($quote, $data);
}

/**
* Get list of available shipping methods
* @param \Magento\Quote\Model\Quote $quote
* @param array $addressData
* @return \Magento\Quote\Api\Data\ShippingMethodInterface[]
*/
private function getShippingMethods(Quote $quote, array $addressData)
{
$output = [];
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCountryId($country);
$shippingAddress->setPostcode($postcode);
$shippingAddress->setRegionId($regionId);
$shippingAddress->setRegion($region);
$shippingAddress->addData($addressData);
$shippingAddress->setCollectShippingRates(true);

$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
$shippingRates = $shippingAddress->getGroupedAllShippingRates();
foreach ($shippingRates as $carrierRates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@

namespace Magento\Quote\Test\Unit\Model\GuestCart;

use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\ShipmentEstimationInterface;
use Magento\Quote\Api\Data\ShippingMethodInterface;
use Magento\Quote\Model\GuestCart\GuestShippingMethodManagement;

class GuestShippingMethodManagementTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Quote\Api\GuestShippingMethodManagementInterface
* @var GuestShippingMethodManagement
*/
private $model;

Expand All @@ -25,19 +32,24 @@ class GuestShippingMethodManagementTest extends \PHPUnit_Framework_TestCase
private $quoteIdMaskFactoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var ShipmentEstimationInterface|MockObject
*/
private $quoteIdMaskMock;
private $shipmentEstimationManagement;

/**
* @var string
* @var QuoteIdMask|MockObject
*/
private $maskedCartId;
private $quoteIdMask;

/**
* @var string
*/
private $cartId;
private $maskedCartId = 'f216207248d65c789b17be8545e0aa73';

/**
* @var int
*/
private $cartId = 867;

protected function setUp()
{
Expand All @@ -46,22 +58,26 @@ protected function setUp()
$this->shippingMethodManagementMock =
$this->getMock('Magento\Quote\Model\ShippingMethodManagement', [], [], '', false);

$this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
$this->cartId = 867;

$guestCartTestHelper = new GuestCartTestHelper($this);
list($this->quoteIdMaskFactoryMock, $this->quoteIdMaskMock) = $guestCartTestHelper->mockQuoteIdMask(
list($this->quoteIdMaskFactoryMock, $this->quoteIdMask) = $guestCartTestHelper->mockQuoteIdMask(
$this->maskedCartId,
$this->cartId
);

$this->shipmentEstimationManagement = $this->getMockForAbstractClass(ShipmentEstimationInterface::class);

$this->model = $objectManager->getObject(
'Magento\Quote\Model\GuestCart\GuestShippingMethodManagement',
GuestShippingMethodManagement::class,
[
'shippingMethodManagement' => $this->shippingMethodManagementMock,
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
]
);

$refObject = new \ReflectionClass(GuestShippingMethodManagement::class);
$refProperty = $refObject->getProperty('shipmentEstimationManagement');
$refProperty->setAccessible(true);
$refProperty->setValue($this->model, $this->shipmentEstimationManagement);
}

public function testSet()
Expand Down Expand Up @@ -99,4 +115,23 @@ public function testGet()

$this->assertEquals($retValue, $this->model->get($this->maskedCartId));
}

/**
* @covers \Magento\Quote\Model\GuestCart\GuestShippingMethodManagement::getShipmentEstimationManagement
*/
public function testEstimateByExtendedAddress()
{
$address = $this->getMockForAbstractClass(AddressInterface::class);

$methodObject = $this->getMockForAbstractClass(ShippingMethodInterface::class);
$expectedRates = [$methodObject];

$this->shipmentEstimationManagement->expects(static::once())
->method('estimateByExtendedAddress')
->with($this->cartId, $address)
->willReturn($expectedRates);

$carriersRates = $this->model->estimateByExtendedAddress($this->maskedCartId, $address);
static::assertEquals($expectedRates, $carriersRates);
}
}
Loading

0 comments on commit d10867f

Please sign in to comment.