Skip to content

Commit 4a7cca6

Browse files
author
Partica, Cristian
committed
MAGETWO-44615: Add product to guest cart via API shows zero price
- moved default addresses code into QuoteManagement createEmptyCart method
1 parent 24c6f79 commit 4a7cca6

File tree

4 files changed

+34
-52
lines changed

4 files changed

+34
-52
lines changed

app/code/Magento/Quote/Model/GuestCart/GuestCartManagement.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Quote\Model\QuoteIdMaskFactory;
1313
use Magento\Quote\Api\Data\PaymentInterface;
1414
use Magento\Quote\Api\CartRepositoryInterface;
15-
use Magento\Quote\Model\Quote\Address;
1615

1716
/**
1817
* Cart Management class for guest carts.
@@ -36,29 +35,21 @@ class GuestCartManagement implements GuestCartManagementInterface
3635
*/
3736
protected $cartRepository;
3837

39-
/**
40-
* @var \Magento\Quote\Model\Quote\AddressFactory
41-
*/
42-
protected $quoteAddressFactory;
43-
4438
/**
4539
* Initialize dependencies.
4640
*
4741
* @param CartManagementInterface $quoteManagement
4842
* @param QuoteIdMaskFactory $quoteIdMaskFactory
49-
* @param \Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory
5043
* @param CartRepositoryInterface $cartRepository
5144
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5245
*/
5346
public function __construct(
5447
CartManagementInterface $quoteManagement,
5548
QuoteIdMaskFactory $quoteIdMaskFactory,
56-
\Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory,
5749
CartRepositoryInterface $cartRepository
5850
) {
5951
$this->quoteManagement = $quoteManagement;
6052
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
61-
$this->quoteAddressFactory = $quoteAddressFactory;
6253
$this->cartRepository = $cartRepository;
6354
}
6455

@@ -70,15 +61,6 @@ public function createEmptyCart()
7061
/** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
7162
$quoteIdMask = $this->quoteIdMaskFactory->create();
7263
$cartId = $this->quoteManagement->createEmptyCart();
73-
74-
/** @var \Magento\Quote\Model\Quote $cart */
75-
$cart = $this->cartRepository->get($cartId);
76-
$billingAddress = $this->quoteAddressFactory->create()->setAddressType(Address::TYPE_BILLING);
77-
$cart->addAddress($billingAddress);
78-
$shippingAddress = $this->quoteAddressFactory->create()->setAddressType(Address::TYPE_SHIPPING);
79-
$cart->addAddress($shippingAddress);
80-
$cart->save();
81-
8264
$quoteIdMask->setQuoteId($cartId)->save();
8365
return $quoteIdMask->getMaskedId();
8466
}

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Sales\Api\Data\OrderInterfaceFactory as OrderFactory;
2121
use Magento\Sales\Api\OrderManagementInterface as OrderManagement;
2222
use Magento\Store\Model\StoreManagerInterface;
23+
use Magento\Quote\Model\Quote\Address;
2324

2425
/**
2526
* Class QuoteManagement
@@ -94,6 +95,11 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
9495
*/
9596
protected $customerModelFactory;
9697

98+
/**
99+
* @var \Magento\Quote\Model\Quote\AddressFactory
100+
*/
101+
protected $quoteAddressFactory;
102+
97103
/**
98104
* @var \Magento\Framework\Api\DataObjectHelper
99105
*/
@@ -154,6 +160,7 @@ public function __construct(
154160
QuoteRepository $quoteRepository,
155161
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
156162
\Magento\Customer\Model\CustomerFactory $customerModelFactory,
163+
\Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory,
157164
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
158165
StoreManagerInterface $storeManager,
159166
\Magento\Checkout\Model\Session $checkoutSession,
@@ -173,6 +180,7 @@ public function __construct(
173180
$this->quoteRepository = $quoteRepository;
174181
$this->customerRepository = $customerRepository;
175182
$this->customerModelFactory = $customerModelFactory;
183+
$this->quoteAddressFactory = $quoteAddressFactory;
176184
$this->dataObjectHelper = $dataObjectHelper;
177185
$this->storeManager = $storeManager;
178186
$this->checkoutSession = $checkoutSession;
@@ -188,6 +196,9 @@ public function createEmptyCart()
188196
$storeId = $this->storeManager->getStore()->getStoreId();
189197
$quote = $this->createAnonymousCart($storeId);
190198

199+
$quote->setBillingAddress($this->quoteAddressFactory->create());
200+
$quote->setShippingAddress($this->quoteAddressFactory->create());
201+
191202
try {
192203
$this->quoteRepository->save($quote);
193204
} catch (\Exception $e) {

app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCartManagementTest.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class GuestCartManagementTest extends \PHPUnit_Framework_TestCase
2424
*/
2525
protected $quoteIdMaskFactoryMock;
2626

27-
/**
28-
* @var \PHPUnit_Framework_MockObject_MockObject
29-
*/
30-
protected $quoteAddressFactory;
31-
3227
/**
3328
* @var \PHPUnit_Framework_MockObject_MockObject
3429
*/
@@ -69,15 +64,6 @@ protected function setUp()
6964
'',
7065
false
7166
);
72-
73-
$this->quoteAddressFactory = $this->getMock(
74-
'Magento\Quote\Model\Quote\AddressFactory',
75-
['create'],
76-
[],
77-
'',
78-
false
79-
);
80-
8167
$this->quoteIdMaskMock = $this->getMock(
8268
'Magento\Quote\Model\QuoteIdMask',
8369
['getQuoteId', 'getMaskedId', 'load', 'save', 'setQuoteId'],
@@ -109,7 +95,6 @@ protected function setUp()
10995
[
11096
'quoteManagement' => $this->quoteManagementMock,
11197
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
112-
'quoteAddressFactory' => $this->quoteAddressFactory,
11398
'cartRepository' => $this->cartRepositoryMock
11499
]
115100
);
@@ -119,30 +104,11 @@ public function testCreateEmptyCart()
119104
{
120105
$maskedCartId = 'masked1cart2id3';
121106
$cartId = 1;
122-
$quoteMock = $this->getMock(
123-
'Magento\Quote\Model\Quote',
124-
['get', 'save', 'addAddress'],
125-
[],
126-
'',
127-
false
128-
);
129-
$quoteAddress = $this->getMock(
130-
'\Magento\Quote\Model\Quote\Address',
131-
['get'],
132-
[],
133-
'',
134-
false
135-
);
136107
$this->quoteIdMaskMock->expects($this->once())->method('setQuoteId')->with($cartId)->willReturnSelf();
137108
$this->quoteIdMaskMock->expects($this->once())->method('save')->willReturnSelf();
138109
$this->quoteIdMaskMock->expects($this->once())->method('getMaskedId')->willreturn($maskedCartId);
139110
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
140111
$this->quoteManagementMock->expects($this->once())->method('createEmptyCart')->willReturn($cartId);
141-
$this->quoteAddressFactory->expects($this->any())->method('create')->willReturn($quoteAddress);
142-
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
143-
$this->cartRepositoryMock->expects($this->once())->method('get')->willReturn($quoteMock);
144-
$quoteMock->expects($this->any())->method('addAddress')->with($quoteAddress)->willReturnSelf();
145-
$quoteMock->expects($this->any())->method('save')->willReturnSelf();
146112

147113
$this->assertEquals($maskedCartId, $this->guestCartManagement->createEmptyCart());
148114
}

app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase
8787
*/
8888
protected $customerFactoryMock;
8989

90+
/**
91+
* @var \PHPUnit_Framework_MockObject_MockObject
92+
*/
93+
protected $quoteAddressFactory;
94+
9095
/**
9196
* @var \PHPUnit_Framework_MockObject_MockObject
9297
*/
@@ -203,6 +208,14 @@ protected function setUp()
203208
false
204209
);
205210

211+
$this->quoteAddressFactory = $this->getMock(
212+
'Magento\Quote\Model\Quote\AddressFactory',
213+
['create'],
214+
[],
215+
'',
216+
false
217+
);
218+
206219
$this->dataObjectHelperMock = $this->getMock('\Magento\Framework\Api\DataObjectHelper', [], [], '', false);
207220
$this->checkoutSessionMock = $this->getMock(
208221
'Magento\Checkout\Model\Session',
@@ -236,6 +249,7 @@ protected function setUp()
236249
'quoteRepository' => $this->quoteRepositoryMock,
237250
'customerRepository' => $this->customerRepositoryMock,
238251
'customerModelFactory' => $this->customerFactoryMock,
252+
'quoteAddressFactory' => $this->quoteAddressFactory,
239253
'dataObjectHelper' => $this->dataObjectHelperMock,
240254
'storeManager' => $this->storeManagerMock,
241255
'checkoutSession' => $this->checkoutSessionMock,
@@ -252,6 +266,13 @@ public function testCreateEmptyCartAnonymous()
252266

253267
$quoteMock = $this->getMock('\Magento\Quote\Model\Quote', [], [], '', false);
254268

269+
$quoteAddress = $this->getMock('\Magento\Quote\Model\Quote\Address', [], [], '', false);
270+
271+
$quoteMock->expects($this->any())->method('setBillingAddress')->with($quoteAddress)->willReturnSelf();
272+
$quoteMock->expects($this->any())->method('setShippingAddress')->with($quoteAddress)->willReturnSelf();
273+
274+
$this->quoteAddressFactory->expects($this->any())->method('create')->willReturn($quoteAddress);
275+
255276
$this->quoteRepositoryMock->expects($this->once())->method('create')->willReturn($quoteMock);
256277
$quoteMock->expects($this->any())->method('setStoreId')->with($storeId);
257278

@@ -681,6 +702,7 @@ public function testPlaceOrderIfCustomerIsGuest()
681702
'quoteRepository' => $this->quoteRepositoryMock,
682703
'customerRepository' => $this->customerRepositoryMock,
683704
'customerModelFactory' => $this->customerFactoryMock,
705+
'quoteAddressFactory' => $this->quoteAddressFactory,
684706
'dataObjectHelper' => $this->dataObjectHelperMock,
685707
'storeManager' => $this->storeManagerMock,
686708
'checkoutSession' => $this->checkoutSessionMock,
@@ -737,6 +759,7 @@ public function testPlaceOrder()
737759
'quoteRepository' => $this->quoteRepositoryMock,
738760
'customerRepository' => $this->customerRepositoryMock,
739761
'customerModelFactory' => $this->customerFactoryMock,
762+
'quoteAddressFactory' => $this->quoteAddressFactory,
740763
'dataObjectHelper' => $this->dataObjectHelperMock,
741764
'storeManager' => $this->storeManagerMock,
742765
'checkoutSession' => $this->checkoutSessionMock,

0 commit comments

Comments
 (0)