Skip to content

Commit 24c6f79

Browse files
author
Partica, Cristian
committed
MAGETWO-44615: Add product to guest cart via API shows zero price
- added default billing & shipping address when a guest cart/quote is created
1 parent 02e0378 commit 24c6f79

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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;
1516

1617
/**
1718
* Cart Management class for guest carts.
@@ -35,21 +36,29 @@ class GuestCartManagement implements GuestCartManagementInterface
3536
*/
3637
protected $cartRepository;
3738

39+
/**
40+
* @var \Magento\Quote\Model\Quote\AddressFactory
41+
*/
42+
protected $quoteAddressFactory;
43+
3844
/**
3945
* Initialize dependencies.
4046
*
4147
* @param CartManagementInterface $quoteManagement
4248
* @param QuoteIdMaskFactory $quoteIdMaskFactory
49+
* @param \Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory
4350
* @param CartRepositoryInterface $cartRepository
4451
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
4552
*/
4653
public function __construct(
4754
CartManagementInterface $quoteManagement,
4855
QuoteIdMaskFactory $quoteIdMaskFactory,
56+
\Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory,
4957
CartRepositoryInterface $cartRepository
5058
) {
5159
$this->quoteManagement = $quoteManagement;
5260
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
61+
$this->quoteAddressFactory = $quoteAddressFactory;
5362
$this->cartRepository = $cartRepository;
5463
}
5564

@@ -61,6 +70,15 @@ public function createEmptyCart()
6170
/** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
6271
$quoteIdMask = $this->quoteIdMaskFactory->create();
6372
$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+
6482
$quoteIdMask->setQuoteId($cartId)->save();
6583
return $quoteIdMask->getMaskedId();
6684
}

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

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

27+
/**
28+
* @var \PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
protected $quoteAddressFactory;
31+
2732
/**
2833
* @var \PHPUnit_Framework_MockObject_MockObject
2934
*/
@@ -64,6 +69,15 @@ protected function setUp()
6469
'',
6570
false
6671
);
72+
73+
$this->quoteAddressFactory = $this->getMock(
74+
'Magento\Quote\Model\Quote\AddressFactory',
75+
['create'],
76+
[],
77+
'',
78+
false
79+
);
80+
6781
$this->quoteIdMaskMock = $this->getMock(
6882
'Magento\Quote\Model\QuoteIdMask',
6983
['getQuoteId', 'getMaskedId', 'load', 'save', 'setQuoteId'],
@@ -95,6 +109,7 @@ protected function setUp()
95109
[
96110
'quoteManagement' => $this->quoteManagementMock,
97111
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
112+
'quoteAddressFactory' => $this->quoteAddressFactory,
98113
'cartRepository' => $this->cartRepositoryMock
99114
]
100115
);
@@ -104,12 +119,30 @@ public function testCreateEmptyCart()
104119
{
105120
$maskedCartId = 'masked1cart2id3';
106121
$cartId = 1;
107-
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+
);
108136
$this->quoteIdMaskMock->expects($this->once())->method('setQuoteId')->with($cartId)->willReturnSelf();
109137
$this->quoteIdMaskMock->expects($this->once())->method('save')->willReturnSelf();
110138
$this->quoteIdMaskMock->expects($this->once())->method('getMaskedId')->willreturn($maskedCartId);
111139
$this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock);
112140
$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();
113146

114147
$this->assertEquals($maskedCartId, $this->guestCartManagement->createEmptyCart());
115148
}

0 commit comments

Comments
 (0)