Skip to content

Commit e1f2e42

Browse files
committed
Merge branch 'develop' into MAGETWO-61826_63014
2 parents 839cec7 + e82ba2e commit e1f2e42

File tree

18 files changed

+997
-213
lines changed

18 files changed

+997
-213
lines changed

app/code/Magento/Backend/Block/Dashboard/Tab/Products/Viewed.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Backend\Block\Dashboard\Tab\Products;
77

8+
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Pricing\Price\FinalPrice;
10+
811
/**
912
* Adminhtml dashboard most viewed products grid
1013
*
@@ -66,8 +69,14 @@ protected function _prepareCollection()
6669
);
6770

6871
$this->setCollection($collection);
72+
parent::_prepareCollection();
73+
74+
/** @var Product $product */
75+
foreach ($collection as $product) {
76+
$product->setPrice($product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue());
77+
}
6978

70-
return parent::_prepareCollection();
79+
return $this;
7180
}
7281

7382
/**

app/code/Magento/CatalogUrlRewrite/Observer/CategoryUrlPathAutogeneratorObserver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public function execute(\Magento\Framework\Event\Observer $observer)
4848
{
4949
/** @var Category $category */
5050
$category = $observer->getEvent()->getCategory();
51-
if ($category->getUrlKey() !== false) {
51+
$useDefaultAttribute = !$category->isObjectNew() && !empty($category->getData('use_default')['url_key']);
52+
if ($category->getUrlKey() !== false && !$useDefaultAttribute) {
5253
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
5354
if (empty($resultUrlKey)) {
5455
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));

app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/CategoryUrlPathAutogeneratorObserverTest.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,16 @@ protected function setUp()
5252
);
5353
$this->category = $this->getMock(
5454
\Magento\Catalog\Model\Category::class,
55-
['setUrlKey', 'setUrlPath', 'dataHasChangedFor', 'isObjectNew', 'getResource', 'getUrlKey', 'getStoreId'],
55+
[
56+
'setUrlKey',
57+
'setUrlPath',
58+
'dataHasChangedFor',
59+
'isObjectNew',
60+
'getResource',
61+
'getUrlKey',
62+
'getStoreId',
63+
'getData'
64+
],
5665
[],
5766
'',
5867
false
@@ -96,7 +105,7 @@ public function testSetCategoryUrlAndCategoryPath()
96105
$this->category->expects($this->once())->method('setUrlKey')->with('urk_key')->willReturnSelf();
97106
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->willReturn('url_path');
98107
$this->category->expects($this->once())->method('setUrlPath')->with('url_path')->willReturnSelf();
99-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(true);
108+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
100109

101110
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
102111
}
@@ -109,6 +118,26 @@ public function testExecuteWithoutUrlKeyAndUrlPathUpdating()
109118
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
110119
}
111120

121+
/**
122+
* @expectedException \Magento\Framework\Exception\LocalizedException
123+
* @expectedExceptionMessage Invalid URL key
124+
*/
125+
public function testExecuteWithException()
126+
{
127+
$categoryName = 'test';
128+
$categoryData = ['url_key' => 0];
129+
$this->category->expects($this->once())->method('getUrlKey')->willReturn($categoryName);
130+
$this->category->expects($this->once())
131+
->method('getData')
132+
->with('use_default')
133+
->willReturn($categoryData);
134+
$this->categoryUrlPathGenerator->expects($this->once())
135+
->method('getUrlKey')
136+
->with($this->category)
137+
->willReturn(null);
138+
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
139+
}
140+
112141
public function testUrlKeyAndUrlPathUpdating()
113142
{
114143
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlKey')->with($this->category)
@@ -120,7 +149,7 @@ public function testUrlKeyAndUrlPathUpdating()
120149
$this->category->expects($this->once())->method('setUrlKey')->with('url_key')->willReturnSelf();
121150
$this->category->expects($this->once())->method('setUrlPath')->with('url_path')->willReturnSelf();
122151
// break code execution
123-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(true);
152+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
124153

125154
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
126155
}
@@ -134,7 +163,7 @@ public function testUrlPathAttributeNoUpdatingIfCategoryIsNew()
134163
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
135164
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
136165

137-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(true);
166+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
138167
$this->categoryResource->expects($this->never())->method('saveAttribute');
139168

140169
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
@@ -148,7 +177,7 @@ public function testUrlPathAttributeUpdating()
148177
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
149178
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
150179
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
151-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(false);
180+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
152181

153182
$this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');
154183

@@ -168,7 +197,7 @@ public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChange
168197
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
169198
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
170199
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
171-
$this->category->expects($this->once())->method('isObjectNew')->willReturn(false);
200+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
172201
// break code execution
173202
$this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);
174203

@@ -183,7 +212,7 @@ public function testChildrenUrlPathAttributeUpdatingForSpecificStore()
183212
$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
184213
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
185214
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
186-
$this->category->expects($this->any())->method('isObjectNew')->willReturn(false);
215+
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
187216
$this->category->expects($this->any())->method('dataHasChangedFor')->willReturn(true);
188217
// only for specific store
189218
$this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(1);

app/code/Magento/Quote/Api/Data/AddressInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ public function getCustomerAddressId();
370370
/**
371371
* Set customer address id
372372
*
373-
* @param int $customerAddressId
373+
* @param int|null $customerAddressId
374374
* @return $this
375375
*/
376376
public function setCustomerAddressId($customerAddressId);

app/code/Magento/Quote/Model/Quote/ShippingAssignment/ShippingAssignmentProcessor.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
*/
66
namespace Magento\Quote\Model\Quote\ShippingAssignment;
77

8+
use Magento\Framework\Exception\LocalizedException;
89
use Magento\Quote\Api\Data\CartInterface;
910
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
1011
use Magento\Framework\Exception\InputException;
1112
use Magento\Quote\Model\ShippingAssignmentFactory;
1213
use Magento\Quote\Model\Quote\Item\CartItemPersister;
14+
use Magento\Customer\Api\AddressRepositoryInterface;
15+
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\Exception\NoSuchEntityException;
1317

1418
class ShippingAssignmentProcessor
1519
{
@@ -28,41 +32,56 @@ class ShippingAssignmentProcessor
2832
*/
2933
protected $cartItemPersister;
3034

35+
/**
36+
* @var AddressRepositoryInterface
37+
*/
38+
private $addressRepository;
39+
3140
/**
3241
* @param ShippingAssignmentFactory $shippingAssignmentFactory
3342
* @param ShippingProcessor $shippingProcessor
3443
* @param CartItemPersister $cartItemPersister
44+
* @param AddressRepositoryInterface $addressRepository
3545
*/
3646
public function __construct(
3747
ShippingAssignmentFactory $shippingAssignmentFactory,
3848
ShippingProcessor $shippingProcessor,
39-
CartItemPersister $cartItemPersister
49+
CartItemPersister $cartItemPersister,
50+
AddressRepositoryInterface $addressRepository = null
4051
) {
4152
$this->shippingAssignmentFactory = $shippingAssignmentFactory;
4253
$this->shippingProcessor = $shippingProcessor;
4354
$this->cartItemPersister = $cartItemPersister;
55+
$this->addressRepository = $addressRepository
56+
?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
4457
}
4558

4659
/**
60+
* Create shipping assignment
61+
*
4762
* @param CartInterface $quote
4863
* @return \Magento\Quote\Api\Data\ShippingAssignmentInterface
4964
*/
5065
public function create(CartInterface $quote)
5166
{
5267
/** @var \Magento\Quote\Model\Quote $quote */
5368
$shippingAddress = $quote->getShippingAddress();
69+
5470
/** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
5571
$shippingAssignment = $this->shippingAssignmentFactory->create();
5672
$shippingAssignment->setItems($quote->getItems());
5773
$shippingAssignment->setShipping($this->shippingProcessor->create($shippingAddress));
74+
5875
return $shippingAssignment;
5976
}
6077

6178
/**
79+
* Save shipping assignment
80+
*
6281
* @param ShippingAssignmentInterface $shippingAssignment
6382
* @param CartInterface $quote
6483
* @return void
65-
* @throws InputException
84+
* @throws InputException|LocalizedException
6685
*/
6786
public function save(CartInterface $quote, ShippingAssignmentInterface $shippingAssignment)
6887
{
@@ -73,6 +92,17 @@ public function save(CartInterface $quote, ShippingAssignmentInterface $shipping
7392
$this->cartItemPersister->save($quote, $item);
7493
}
7594
}
95+
96+
$shippingAddress = $shippingAssignment->getShipping()->getAddress();
97+
98+
if ($shippingAddress->getCustomerAddressId()) {
99+
try {
100+
$this->addressRepository->getById($shippingAddress->getCustomerAddressId());
101+
} catch (NoSuchEntityException $e) {
102+
$shippingAddress->setCustomerAddressId(null);
103+
}
104+
}
105+
76106
$this->shippingProcessor->save($shippingAssignment->getShipping(), $quote);
77107
}
78108
}

app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
namespace Magento\Quote\Model\QuoteRepository;
77

88
use Magento\Quote\Api\Data\CartInterface;
9+
use Magento\Customer\Api\AddressRepositoryInterface;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\NoSuchEntityException;
912
use Magento\Framework\Exception\InputException;
1013

1114
class SaveHandler
@@ -30,38 +33,48 @@ class SaveHandler
3033
*/
3134
private $shippingAssignmentPersister;
3235

36+
/**
37+
* @var AddressRepositoryInterface
38+
*/
39+
private $addressRepository;
40+
3341
/**
3442
* @param \Magento\Quote\Model\ResourceModel\Quote $quoteResource
3543
* @param \Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister
3644
* @param \Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister
3745
* @param \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister
46+
* @param AddressRepositoryInterface $addressRepository
3847
*/
3948
public function __construct(
4049
\Magento\Quote\Model\ResourceModel\Quote $quoteResource,
4150
\Magento\Quote\Model\Quote\Item\CartItemPersister $cartItemPersister,
4251
\Magento\Quote\Model\Quote\Address\BillingAddressPersister $billingAddressPersister,
43-
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister
52+
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentPersister $shippingAssignmentPersister,
53+
AddressRepositoryInterface $addressRepository = null
4454
) {
4555
$this->quoteResourceModel = $quoteResource;
4656
$this->cartItemPersister = $cartItemPersister;
4757
$this->billingAddressPersister = $billingAddressPersister;
4858
$this->shippingAssignmentPersister = $shippingAssignmentPersister;
59+
$this->addressRepository = $addressRepository
60+
?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class);
4961
}
5062

5163
/**
64+
* Process and save quote data
65+
*
5266
* @param CartInterface $quote
5367
* @return CartInterface
54-
*
5568
* @throws InputException
5669
* @throws \Magento\Framework\Exception\CouldNotSaveException
5770
* @throws \Magento\Framework\Exception\LocalizedException
58-
* @throws \Magento\Framework\Exception\NoSuchEntityException
5971
*/
6072
public function save(CartInterface $quote)
6173
{
6274
/** @var \Magento\Quote\Model\Quote $quote */
6375
// Quote Item processing
6476
$items = $quote->getItems();
77+
6578
if ($items) {
6679
foreach ($items as $item) {
6780
/** @var \Magento\Quote\Model\Quote\Item $item */
@@ -73,17 +86,28 @@ public function save(CartInterface $quote)
7386

7487
// Billing Address processing
7588
$billingAddress = $quote->getBillingAddress();
89+
7690
if ($billingAddress) {
91+
if ($billingAddress->getCustomerAddressId()) {
92+
try {
93+
$this->addressRepository->getById($billingAddress->getCustomerAddressId());
94+
} catch (NoSuchEntityException $e) {
95+
$billingAddress->setCustomerAddressId(null);
96+
}
97+
}
98+
7799
$this->billingAddressPersister->save($quote, $billingAddress);
78100
}
79101

80102
$this->processShippingAssignment($quote);
81-
82103
$this->quoteResourceModel->save($quote->collectTotals());
104+
83105
return $quote;
84106
}
85107

86108
/**
109+
* Process shipping assignment
110+
*
87111
* @param \Magento\Quote\Model\Quote $quote
88112
* @return void
89113
* @throws InputException
@@ -92,11 +116,14 @@ private function processShippingAssignment($quote)
92116
{
93117
// Shipping Assignments processing
94118
$extensionAttributes = $quote->getExtensionAttributes();
119+
95120
if (!$quote->isVirtual() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
96121
$shippingAssignments = $extensionAttributes->getShippingAssignments();
122+
97123
if (count($shippingAssignments) > 1) {
98-
throw new InputException(__("Only 1 shipping assignment can be set"));
124+
throw new InputException(__('Only 1 shipping assignment can be set'));
99125
}
126+
100127
$this->shippingAssignmentPersister->save($quote, $shippingAssignments[0]);
101128
}
102129
}

0 commit comments

Comments
 (0)