Skip to content

Commit 2898df4

Browse files
ENGCOM-5138: Don't throw shipping method exception when creating quote with only virtual products in API #14384
- Merge Pull Request #14384 from Maikel-Koek/magento2:virtual-products-api-quote - Merged commits: 1. 0bcde8c 2. 9afb7bd 3. f902d82 4. a89e84a 5. 46d01a8
2 parents 211dd25 + 46d01a8 commit 2898df4

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

app/code/Magento/Checkout/Model/ShippingInformationManagement.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ public function saveAddressInformation(
191191

192192
$shippingAddress = $quote->getShippingAddress();
193193

194-
if (!$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) {
194+
if (!$quote->getIsVirtual()
195+
&& !$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())
196+
) {
195197
throw new NoSuchEntityException(
196198
__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode)
197199
);
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Checkout\Model;
8+
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Checkout\Api\Data\ShippingInformationInterface;
11+
use Magento\Checkout\Api\PaymentInformationManagementInterface;
12+
use Magento\Checkout\Api\ShippingInformationManagementInterface;
13+
use Magento\Customer\Api\CustomerRepositoryInterface;
14+
use Magento\Quote\Api\CartItemRepositoryInterface;
15+
use Magento\Quote\Api\CartManagementInterface;
16+
use Magento\Quote\Api\Data\AddressInterfaceFactory;
17+
use Magento\Quote\Api\Data\CartItemInterface;
18+
use Magento\Quote\Api\Data\PaymentInterface;
19+
use Magento\Quote\Api\ShipmentEstimationInterface;
20+
use Magento\Sales\Api\InvoiceOrderInterface;
21+
22+
class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase
23+
{
24+
/** @var CartManagementInterface */
25+
private $cartManagement;
26+
27+
/** @var CartItemRepositoryInterface */
28+
private $cartItemRepository;
29+
30+
/** @var CartItemInterface */
31+
private $cartItem;
32+
33+
/** @var ShippingInformationManagementInterface */
34+
private $shippingInformationManagement;
35+
36+
/** @var ShippingInformationInterface */
37+
private $shippingInformation;
38+
39+
/** @var CustomerRepositoryInterface */
40+
private $customerRepository;
41+
42+
/** @var AddressInterfaceFactory */
43+
private $apiAddressFactory;
44+
45+
/** @var ShipmentEstimationInterface */
46+
private $shipmentEstimation;
47+
48+
/** @var PaymentInformationManagementInterface */
49+
private $paymentInformationManagement;
50+
51+
/** @var PaymentInterface */
52+
private $payment;
53+
54+
/** @var InvoiceOrderInterface */
55+
private $invoiceOrder;
56+
57+
public function setUp()
58+
{
59+
$objectManager = Bootstrap::getObjectManager();
60+
61+
$this->cartManagement = $objectManager->create(CartManagementInterface::class);
62+
$this->cartItemRepository = $objectManager->create(CartItemRepositoryInterface::class);
63+
$this->cartItem = $objectManager->create(CartItemInterface::class);
64+
$this->shippingInformationManagement = $objectManager->create(ShippingInformationManagementInterface::class);
65+
$this->shippingInformation = $objectManager->create(ShippingInformationInterface::class);
66+
$this->customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
67+
$this->apiAddressFactory = $objectManager->create(AddressInterfaceFactory::class);
68+
$this->shipmentEstimation = $objectManager->create(ShipmentEstimationInterface::class);
69+
$this->paymentInformationManagement = $objectManager->create(PaymentInformationManagementInterface::class);
70+
$this->payment = $objectManager->create(PaymentInterface::class);
71+
$this->invoiceOrder = $objectManager->create(InvoiceOrderInterface::class);
72+
}
73+
74+
/**
75+
* @magentoDataFixture Magento/Customer/_files/customer.php
76+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
77+
* @magentoDataFixture Magento/Catalog/_files/product_virtual_in_stock.php
78+
*/
79+
public function testQuoteApiWithOnlyVirtualProducts()
80+
{
81+
$customer = $this->customerRepository->getById(1);
82+
83+
// Create empty quote
84+
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customer->getId());
85+
86+
$cartItem = $this->cartItem
87+
->setSku('virtual-product')
88+
->setQty(1)
89+
->setQuoteId($quoteId);
90+
91+
// Add item to cart
92+
$this->cartItemRepository->save($cartItem);
93+
94+
$billingAddress = $shippingAddress = null;
95+
foreach ($customer->getAddresses() as $address) {
96+
$billingAddress = $address;
97+
$shippingAddress = $address;
98+
break;
99+
}
100+
101+
/** @var \Magento\Quote\Model\Quote\Address $apiBillingAddress */
102+
$apiBillingAddress = $this->apiAddressFactory->create();
103+
$apiBillingAddress->setRegion($billingAddress->getRegion())
104+
->setRegionId($billingAddress->getRegionId())
105+
->setCountryId($billingAddress->getCountryId())
106+
->setStreet($billingAddress->getStreet())
107+
->setPostcode($billingAddress->getPostcode())
108+
->setCity($billingAddress->getCity())
109+
->setFirstname($billingAddress->getFirstname())
110+
->setLastname($billingAddress->getLastname())
111+
->setEmail($customer->getEmail())
112+
->setTelephone($billingAddress->getTelephone());
113+
114+
/** @var \Magento\Quote\Model\Quote\Address $apiShippingAddress */
115+
$apiShippingAddress = $this->apiAddressFactory->create();
116+
$apiShippingAddress->setRegion($shippingAddress->getRegion())
117+
->setRegionId($shippingAddress->getRegionId())
118+
->setCountryId($shippingAddress->getCountryId())
119+
->setStreet($shippingAddress->getStreet())
120+
->setPostcode($shippingAddress->getPostcode())
121+
->setCity($shippingAddress->getCity())
122+
->setFirstname($shippingAddress->getFirstname())
123+
->setLastname($shippingAddress->getLastname())
124+
->setEmail($customer->getEmail())
125+
->setTelephone($shippingAddress->getTelephone());
126+
127+
// Estimate shipping
128+
$this->shipmentEstimation->estimateByExtendedAddress($quoteId, $apiShippingAddress);
129+
130+
$addressInformation = $this->shippingInformation
131+
->setBillingAddress($apiBillingAddress)
132+
->setShippingAddress($apiShippingAddress)
133+
->setShippingCarrierCode('flatrate')
134+
->setShippingMethodCode('flatrate');
135+
136+
// Set address information on quote
137+
$this->shippingInformationManagement->saveAddressInformation($quoteId, $addressInformation);
138+
}
139+
}

0 commit comments

Comments
 (0)