Skip to content

Commit 257791c

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-45173' into BugFestW6
2 parents 1efc75b + 59beee6 commit 257791c

File tree

10 files changed

+89
-52
lines changed

10 files changed

+89
-52
lines changed

app/code/Magento/Bundle/Model/LinkManagement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ public function addChild(
254254

255255
try {
256256
$selectionModel->save();
257+
$resource->addProductRelations($product->getId(), [$linkProductModel->getId()]);
257258
} catch (\Exception $e) {
258259
throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
259260
}
@@ -294,7 +295,7 @@ public function removeChild($sku, $optionId, $childSku)
294295
/* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */
295296
$resource = $this->bundleFactory->create();
296297
$resource->dropAllUnneededSelections($product->getId(), $excludeSelectionIds);
297-
$resource->saveProductRelations($product->getId(), array_unique($usedProductIds));
298+
$resource->removeProductRelations($product->getId(), array_unique($usedProductIds));
298299

299300
return true;
300301
}

app/code/Magento/Bundle/Model/OptionRepository.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,22 @@ public function save(
170170
$option->setParentId($product->getId());
171171

172172
$optionId = $option->getOptionId();
173-
$linksToAdd = [];
174173
if (!$optionId) {
174+
$linksToAdd = [];
175175
$option->setDefaultTitle($option->getTitle());
176176
if (is_array($option->getProductLinks())) {
177177
$linksToAdd = $option->getProductLinks();
178178
}
179+
try {
180+
$this->optionResource->save($option);
181+
} catch (\Exception $e) {
182+
throw new CouldNotSaveException(__('Could not save option'), $e);
183+
}
184+
185+
/** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */
186+
foreach ($linksToAdd as $linkedProduct) {
187+
$this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
188+
}
179189
} else {
180190
$optionCollection = $this->type->getOptionsCollection($product);
181191

@@ -188,19 +198,13 @@ public function save(
188198

189199
$option->setData(array_merge($existingOption->getData(), $option->getData()));
190200
$this->updateOptionSelection($product, $option);
201+
try {
202+
$this->optionResource->save($option);
203+
} catch (\Exception $e) {
204+
throw new CouldNotSaveException(__('Could not save option'), $e);
205+
}
191206
}
192-
193-
try {
194-
$this->optionResource->save($option);
195-
} catch (\Exception $e) {
196-
throw new CouldNotSaveException(__('Could not save option'), $e);
197-
}
198-
199-
/** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */
200-
foreach ($linksToAdd as $linkedProduct) {
201-
$this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
202-
}
203-
207+
$product->setIsRelationsChanged(true);
204208
return $option->getOptionId();
205209
}
206210

app/code/Magento/Bundle/Model/ResourceModel/Bundle.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,30 @@ public function saveProductRelations($parentId, $childIds)
142142

143143
return $this;
144144
}
145+
146+
/**
147+
* Add product relations
148+
*
149+
* @param int $parentId
150+
* @param array $childIds
151+
* @return $this
152+
*/
153+
public function addProductRelations($parentId, $childIds)
154+
{
155+
$this->_productRelation->addRelations($parentId, $childIds);
156+
return $this;
157+
}
158+
159+
/**
160+
* Remove product relations
161+
*
162+
* @param int $parentId
163+
* @param array $childIds
164+
* @return $this
165+
*/
166+
public function removeProductRelations($parentId, $childIds)
167+
{
168+
$this->_productRelation->removeRelations($parentId, $childIds);
169+
return $this;
170+
}
145171
}

app/code/Magento/Bundle/Test/Unit/Model/LinkManagementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ public function testRemoveChild()
868868
$this->product->expects($this->any())->method('getId')->will($this->returnValue(3));
869869

870870
$bundle->expects($this->once())->method('dropAllUnneededSelections')->with(3, []);
871-
$bundle->expects($this->once())->method('saveProductRelations')->with(3, []);
871+
$bundle->expects($this->once())->method('removeProductRelations')->with(3, []);
872872
//Params come in lowercase to method
873873
$this->assertTrue($this->model->removeChild($productSku, $optionId, $childSku));
874874
}

app/code/Magento/Bundle/Test/Unit/Model/OptionRepositoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ public function testSaveExistingOption()
408408
->method('getProductLinks')
409409
->willReturn([$productLinkUpdate, $productLinkNew]);
410410

411-
$this->optionResourceMock->expects($this->once())->method('save')->with($optionMock)->willReturnSelf();
412411
$this->linkManagementMock->expects($this->once())
413412
->method('addChild')
414413
->with($productMock, $optionId, $productLinkNew);

app/code/Magento/Bundle/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
<plugin name="bundle" type="Magento\Bundle\Model\Plugin\PriceBackend" sortOrder="100" />
7979
</type>
8080
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
81-
<plugin name="bundleSaveOptions" type="\Magento\Bundle\Model\Plugin\BundleSaveOptions"/>
81+
<plugin name="bundleSaveOptions" type="\Magento\Bundle\Model\Plugin\BundleSaveOptions" sortOrder="100"/>
8282
</type>
8383
<type name="Magento\Catalog\Model\Product">
8484
<plugin name="bundleLoadOptions" type="\Magento\Bundle\Model\Plugin\BundleLoadOptions"/>

app/code/Magento/Catalog/Model/ResourceModel/Product/Relation.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,52 @@ public function processRelations($parentId, $childIds)
4444
$insert = array_diff($new, $old);
4545
$delete = array_diff($old, $new);
4646

47-
if (!empty($insert)) {
47+
$this->addRelations($parentId, $insert);
48+
$this->removeRelations($parentId, $delete);
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* Add Relations
55+
*
56+
* @param int $parentId
57+
* @param int[] $childIds
58+
* @return $this
59+
* @throws \Magento\Framework\Exception\LocalizedException
60+
*/
61+
public function addRelations($parentId, $childIds)
62+
{
63+
if (!empty($childIds)) {
4864
$insertData = [];
49-
foreach ($insert as $childId) {
65+
foreach ($childIds as $childId) {
5066
$insertData[] = ['parent_id' => $parentId, 'child_id' => $childId];
5167
}
5268
$this->getConnection()->insertMultiple($this->getMainTable(), $insertData);
5369
}
54-
if (!empty($delete)) {
70+
return $this;
71+
}
72+
73+
/**
74+
* Remove Relations
75+
*
76+
* @param int $parentId
77+
* @param int[] $childIds
78+
* @return $this
79+
* @throws \Magento\Framework\Exception\LocalizedException
80+
*/
81+
public function removeRelations($parentId, $childIds)
82+
{
83+
if (!empty($childIds)) {
5584
$where = join(
5685
' AND ',
5786
[
5887
$this->getConnection()->quoteInto('parent_id = ?', $parentId),
59-
$this->getConnection()->quoteInto('child_id IN(?)', $delete)
88+
$this->getConnection()->quoteInto('child_id IN(?)', $childIds)
6089
]
6190
);
6291
$this->getConnection()->delete($this->getMainTable(), $where);
6392
}
64-
6593
return $this;
6694
}
6795
}

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public function aroundSave(
2222
\Closure $proceed,
2323
\Magento\Framework\Model\AbstractModel $product
2424
) {
25-
$result = $proceed($product);
26-
$this->reindexRow($product->getId());
27-
return $result;
25+
$productResource->addCommitCallback(function () use ($product) {
26+
$this->reindexRow($product->getId());
27+
});
28+
return $proceed($product);
2829
}
2930

3031
/**
@@ -41,8 +42,9 @@ public function aroundDelete(
4142
\Closure $proceed,
4243
\Magento\Framework\Model\AbstractModel $product
4344
) {
44-
$result = $proceed($product);
45-
$this->reindexRow($product->getId());
46-
return $result;
45+
$productResource->addCommitCallback(function () use ($product) {
46+
$this->reindexRow($product->getId());
47+
});
48+
return $proceed($product);
4749
}
4850
}

app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/Fulltext/Plugin/ProductTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ protected function setUp()
7070

7171
public function testAfterSaveNonScheduled()
7272
{
73-
$this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(false));
74-
$this->indexerMock->expects($this->once())->method('reindexRow')->with(1);
75-
$this->prepareIndexer();
76-
77-
$this->productMock->expects($this->once())->method('getId')->will($this->returnValue(1));
78-
7973
$this->assertEquals(
8074
$this->subjectMock,
8175
$this->model->aroundSave($this->subjectMock, $this->proceed, $this->productMock)
@@ -84,12 +78,6 @@ public function testAfterSaveNonScheduled()
8478

8579
public function testAfterSaveScheduled()
8680
{
87-
$this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(true));
88-
$this->indexerMock->expects($this->never())->method('reindexRow');
89-
$this->prepareIndexer();
90-
91-
$this->productMock->expects($this->once())->method('getId')->will($this->returnValue(1));
92-
9381
$this->assertEquals(
9482
$this->subjectMock,
9583
$this->model->aroundSave($this->subjectMock, $this->proceed, $this->productMock)
@@ -98,12 +86,6 @@ public function testAfterSaveScheduled()
9886

9987
public function testAfterDeleteNonScheduled()
10088
{
101-
$this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(false));
102-
$this->indexerMock->expects($this->once())->method('reindexRow')->with(1);
103-
$this->prepareIndexer();
104-
105-
$this->productMock->expects($this->once())->method('getId')->will($this->returnValue(1));
106-
10789
$this->assertEquals(
10890
$this->subjectMock,
10991
$this->model->aroundDelete($this->subjectMock, $this->proceed, $this->productMock)
@@ -112,12 +94,6 @@ public function testAfterDeleteNonScheduled()
11294

11395
public function testAfterDeleteScheduled()
11496
{
115-
$this->indexerMock->expects($this->once())->method('isScheduled')->will($this->returnValue(true));
116-
$this->indexerMock->expects($this->never())->method('reindexRow');
117-
$this->prepareIndexer();
118-
119-
$this->productMock->expects($this->once())->method('getId')->will($this->returnValue(1));
120-
12197
$this->assertEquals(
12298
$this->subjectMock,
12399
$this->model->aroundDelete($this->subjectMock, $this->proceed, $this->productMock)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CatalogUrlRewrite\Observer;
77

88
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Catalog\Model\Product\Visibility;
910
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
1011
use Magento\Framework\App\RequestInterface;
1112
use Magento\Framework\Event\ObserverInterface;

0 commit comments

Comments
 (0)