Skip to content

Commit ec3964c

Browse files
committed
MAGETWO-37275: [GitHub] How to load product store wise? #1246
1 parent 649b03a commit ec3964c

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Magento\Framework\Api\ImageProcessorInterface;
1616
use Magento\Framework\Api\SearchCriteriaInterface;
1717
use Magento\Framework\Api\SortOrder;
18-
use Magento\Framework\App\Filesystem\DirectoryList;
1918
use Magento\Framework\Exception\InputException;
2019
use Magento\Framework\Exception\NoSuchEntityException;
2120
use Magento\Framework\Exception\StateException;
@@ -219,6 +218,9 @@ public function get($sku, $editMode = false, $storeId = null, $forceReload = fal
219218
if ($editMode) {
220219
$product->setData('_edit_mode', true);
221220
}
221+
if ($storeId !== null) {
222+
$product->setData('store_id', $storeId);
223+
}
222224
$product->load($productId);
223225
$this->instances[$sku][$cacheKey] = $product;
224226
$this->instancesById[$product->getId()][$cacheKey] = $product;
@@ -234,7 +236,6 @@ public function getById($productId, $editMode = false, $storeId = null, $forceRe
234236
$cacheKey = $this->getCacheKey(func_get_args());
235237
if (!isset($this->instancesById[$productId][$cacheKey]) || $forceReload) {
236238
$product = $this->productFactory->create();
237-
238239
if ($editMode) {
239240
$product->setData('_edit_mode', true);
240241
}

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ protected function setUp()
231231
'contentFactory' => $this->contentFactoryMock,
232232
'mimeTypeExtensionMap' => $this->mimeTypeExtensionMapMock,
233233
'linkTypeProvider' => $this->linkTypeProviderMock,
234-
'imageProcessor' => $this->imageProcessorMock
234+
'imageProcessor' => $this->imageProcessorMock,
235235
]
236236
);
237237
}
@@ -271,6 +271,19 @@ public function testGetProductInEditMode()
271271
$this->assertEquals($this->productMock, $this->model->get('test_sku', true));
272272
}
273273

274+
public function testGetWithSetStoreId()
275+
{
276+
$productId = 123;
277+
$sku = 'test-sku';
278+
$storeId = 7;
279+
$this->productFactoryMock->expects($this->once())->method('create')->willReturn($this->productMock);
280+
$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($sku)->willReturn($productId);
281+
$this->productMock->expects($this->once())->method('setData')->with('store_id', $storeId);
282+
$this->productMock->expects($this->once())->method('load')->with($productId);
283+
$this->productMock->expects($this->once())->method('getId')->willReturn($productId);
284+
$this->assertSame($this->productMock, $this->model->get($sku, false, $storeId));
285+
}
286+
274287
/**
275288
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
276289
* @expectedExceptionMessage Requested product doesn't exist
@@ -729,8 +742,8 @@ public function saveExistingWithOptionsDataProvider()
729742
[ //new option value
730743
"title" => "DropdownOptions_3",
731744
"price" => 4,
732-
]
733-
]
745+
],
746+
],
734747
],
735748
[//new option
736749
"type" => "checkbox",
@@ -739,7 +752,7 @@ public function saveExistingWithOptionsDataProvider()
739752
"title" => "CheckBoxValue2",
740753
"price" => 5,
741754
],
742-
]
755+
],
743756
],
744757
];
745758

@@ -819,16 +832,16 @@ public function saveExistingWithOptionsDataProvider()
819832
"price" => 6,
820833
"is_delete" => 1,
821834
],
822-
]
835+
],
823836
],
824837
[
825838
"type" => "checkbox",
826839
"values" => [
827840
[
828841
"title" => "CheckBoxValue2",
829842
"price" => 5,
830-
]
831-
]
843+
],
844+
],
832845
],
833846
[
834847
"option_id" => 11,
@@ -945,32 +958,65 @@ public function saveWithLinksDataProvider()
945958
// Scenario 1
946959
// No existing, new links
947960
$data['scenario_1'] = [
948-
'newLinks' => ["product_sku" => "Simple Product 1", "link_type" => "associated", "linked_product_sku" =>
949-
"Simple Product 2", "linked_product_type" => "simple", "position" => 0, "qty" => 1],
961+
'newLinks' => [
962+
"product_sku" => "Simple Product 1",
963+
"link_type" => "associated",
964+
"linked_product_sku" => "Simple Product 2",
965+
"linked_product_type" => "simple",
966+
"position" => 0,
967+
"qty" => 1,
968+
],
950969
'existingLinks' => [],
951-
'expectedData' => [["product_sku" => "Simple Product 1", "link_type" => "associated", "linked_product_sku" =>
952-
"Simple Product 2", "linked_product_type" => "simple", "position" => 0, "qty" => 1]]
953-
];
970+
'expectedData' => [[
971+
"product_sku" => "Simple Product 1",
972+
"link_type" => "associated",
973+
"linked_product_sku" => "Simple Product 2",
974+
"linked_product_type" => "simple",
975+
"position" => 0,
976+
"qty" => 1,
977+
]],
978+
];
954979

955980
// Scenario 2
956981
// Existing, no new links
957982
$data['scenario_2'] = [
958983
'newLinks' => [],
959-
'existingLinks' => ["product_sku" => "Simple Product 1", "link_type" => "related", "linked_product_sku" =>
960-
"Simple Product 2", "linked_product_type" => "simple", "position" => 0],
961-
'expectedData' => []
984+
'existingLinks' => [
985+
"product_sku" => "Simple Product 1",
986+
"link_type" => "related",
987+
"linked_product_sku" => "Simple Product 2",
988+
"linked_product_type" => "simple",
989+
"position" => 0,
990+
],
991+
'expectedData' => [],
962992
];
963993

964994
// Scenario 3
965995
// Existing and new links
966996
$data['scenario_3'] = [
967-
'newLinks' => ["product_sku" => "Simple Product 1", "link_type" => "related", "linked_product_sku" =>
968-
"Simple Product 2", "linked_product_type" => "simple", "position" => 0],
969-
'existingLinks' => ["product_sku" => "Simple Product 1", "link_type" => "related", "linked_product_sku" =>
970-
"Simple Product 3", "linked_product_type" => "simple", "position" => 0],
997+
'newLinks' => [
998+
"product_sku" => "Simple Product 1",
999+
"link_type" => "related",
1000+
"linked_product_sku" => "Simple Product 2",
1001+
"linked_product_type" => "simple",
1002+
"position" => 0,
1003+
],
1004+
'existingLinks' => [
1005+
"product_sku" => "Simple Product 1",
1006+
"link_type" => "related",
1007+
"linked_product_sku" => "Simple Product 3",
1008+
"linked_product_type" => "simple",
1009+
"position" => 0,
1010+
],
9711011
'expectedData' => [
972-
["product_sku" => "Simple Product 1", "link_type" => "related", "linked_product_sku" =>
973-
"Simple Product 2", "linked_product_type" => "simple", "position" => 0]]
1012+
[
1013+
"product_sku" => "Simple Product 1",
1014+
"link_type" => "related",
1015+
"linked_product_sku" => "Simple Product 2",
1016+
"linked_product_type" => "simple",
1017+
"position" => 0,
1018+
],
1019+
],
9741020
];
9751021

9761022
return $data;
@@ -1143,7 +1189,6 @@ public function testSaveExistingWithMediaGalleryEntries()
11431189
->method('setMediaAttribute')
11441190
->with($this->initializedProductMock, ['image', 'small_image'], 'filename1');
11451191

1146-
11471192
$this->model->save($this->productMock);
11481193
$this->assertEquals($expectedResult, $this->initializedProductMock->getMediaGallery('images'));
11491194
}

0 commit comments

Comments
 (0)