Skip to content

Commit 9442e61

Browse files
committed
MAGETWO-94218: Data from module ProductLinksSampleData is not installed
1 parent cd346e0 commit 9442e61

File tree

9 files changed

+52
-28
lines changed

9 files changed

+52
-28
lines changed

app/code/Magento/CmsSampleData/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<module name="Magento_CatalogSampleData"/>
1414
<module name="Magento_BundleSampleData"/>
1515
<module name="Magento_ConfigurableSampleData"/>
16-
<module name="Magento_GroupedSampleData"/>
16+
<module name="Magento_GroupedProductSampleData"/>
1717
<module name="Magento_GiftCardSampleData"/>
1818
<module name="Magento_ThemeSampleData"/>
1919
</sequence>

app/code/Magento/ConfigurableSampleData/Setup/Installer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function install()
5858
$this->productLinkSetup->install(
5959
['Magento_ConfigurableSampleData::fixtures/Links/related.csv'],
6060
['Magento_ConfigurableSampleData::fixtures/Links/upsell.csv'],
61-
['Magento_ConfigurableSampleData::fixtures/Links/crossell.csv']
61+
['Magento_ConfigurableSampleData::fixtures/Links/crosssell.csv']
6262
);
6363
}
6464
}

app/code/Magento/ConfigurableSampleData/etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<module name="Magento_CatalogSampleData"/>
1414
<module name="Magento_DownloadableSampleData"/>
1515
<module name="Magento_BundleSampleData"/>
16+
<module name="Magento_GroupedProductSampleData"/>
1617
</sequence>
1718
</module>
1819
</config>

app/code/Magento/ProductLinksSampleData/Model/ProductLink.php

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
namespace Magento\ProductLinksSampleData\Model;
77

8+
use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
9+
use Magento\Catalog\Api\ProductLinkRepositoryInterface;
10+
use Magento\Catalog\Model\ProductFactory;
11+
use Magento\Catalog\Model\ProductRepository;
12+
use Magento\Framework\Exception\NoSuchEntityException;
813
use Magento\Framework\Setup\SampleData\Context as SampleDataContext;
914

1015
/**
@@ -23,29 +28,45 @@ class ProductLink
2328
protected $csvReader;
2429

2530
/**
26-
* @var \Magento\Catalog\Model\ProductFactory
31+
* @var ProductFactory
2732
*/
2833
protected $productFactory;
2934

3035
/**
31-
* @var \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks
36+
* @var ProductLinkInterfaceFactory
3237
*/
33-
protected $linksInitializer;
38+
private $productLinkFactory;
39+
40+
/**
41+
* @var ProductRepository
42+
*/
43+
private $productRepository;
44+
45+
/**
46+
* @var ProductLinkRepositoryInterface
47+
*/
48+
private $productLinkRepository;
3449

3550
/**
3651
* @param SampleDataContext $sampleDataContext
37-
* @param \Magento\Catalog\Model\ProductFactory $productFactory
38-
* @param \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linksInitializer
52+
* @param ProductFactory $productFactory
53+
* @param ProductRepository $productRepository
54+
* @param ProductLinkRepositoryInterface $productLinkRepository
55+
* @param ProductLinkInterfaceFactory $productLinkFactory
3956
*/
4057
public function __construct(
4158
SampleDataContext $sampleDataContext,
42-
\Magento\Catalog\Model\ProductFactory $productFactory,
43-
\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linksInitializer
59+
ProductFactory $productFactory,
60+
ProductRepository $productRepository,
61+
ProductLinkRepositoryInterface $productLinkRepository,
62+
ProductLinkInterfaceFactory $productLinkFactory
4463
) {
4564
$this->fixtureManager = $sampleDataContext->getFixtureManager();
4665
$this->csvReader = $sampleDataContext->getCsvReader();
4766
$this->productFactory = $productFactory;
48-
$this->linksInitializer = $linksInitializer;
67+
$this->productRepository = $productRepository;
68+
$this->productLinkRepository = $productLinkRepository;
69+
$this->productLinkFactory = $productLinkFactory;
4970
}
5071

5172
/**
@@ -74,23 +95,25 @@ public function install(array $related, array $upsell, array $crosssell)
7495
foreach ($row as $key => $value) {
7596
$data[$header[$key]] = $value;
7697
}
77-
$row = $data;
78-
/** @var \Magento\Catalog\Model\Product $product */
79-
$product = $this->productFactory->create();
80-
$productId = $product->getIdBySku($row['sku']);
81-
if (!$productId) {
98+
99+
try {
100+
$product = $this->productRepository->get($data['sku']);
101+
} catch (NoSuchEntityException $e) {
82102
continue;
83103
}
84-
$product->setId($productId);
85-
$links = [$linkType => []];
86-
foreach (explode("\n", $row['linked_sku']) as $linkedProductSku) {
87-
$linkedProductId = $product->getIdBySku($linkedProductSku);
88-
if ($linkedProductId) {
89-
$links[$linkType][$linkedProductId] = [];
104+
105+
$linkedProductSkus = explode("\n", $data['linked_sku']);
106+
foreach ($linkedProductSkus as $linkedProductSku) {
107+
$productLink = $this->productLinkFactory->create();
108+
$productLink->setSku($product->getSku())
109+
->setLinkedProductSku($linkedProductSku)
110+
->setLinkType($linkType);
111+
try {
112+
$this->productLinkRepository->save($productLink);
113+
} catch (NoSuchEntityException $e) {
114+
continue;
90115
}
91116
}
92-
$this->linksInitializer->initializeLinks($product, $links);
93-
$product->getLinkInstance()->saveProductRelations($product);
94117
}
95118
}
96119
}

app/code/Magento/ProductLinksSampleData/Setup/Installer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function install()
3131
$this->productLink->install(
3232
['Magento_ProductLinksSampleData::fixtures/related.csv'],
3333
['Magento_ProductLinksSampleData::fixtures/upsell.csv'],
34-
['Magento_ProductLinksSampleData::fixtures/crossell.csv']
34+
['Magento_ProductLinksSampleData::fixtures/crosssell.csv']
3535
);
3636
}
3737
}

app/code/Magento/ProductLinksSampleData/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<module name="Magento_CatalogSampleData"/>
1313
<module name="Magento_BundleSampleData"/>
1414
<module name="Magento_ConfigurableSampleData"/>
15-
<module name="Magento_GroupedSampleData"/>
15+
<module name="Magento_GroupedProductSampleData"/>
1616
<module name="Magento_GiftCardSampleData"/>
1717
</sequence>
1818
</module>

app/code/Magento/ReviewSampleData/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<module name="Magento_CatalogSampleData"/>
1414
<module name="Magento_BundleSampleData"/>
1515
<module name="Magento_ConfigurableSampleData"/>
16-
<module name="Magento_GroupedSampleData"/>
16+
<module name="Magento_GroupedProductSampleData"/>
1717
<module name="Magento_GiftCardSampleData"/>
1818
</sequence>
1919
</module>

app/code/Magento/SalesSampleData/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<module name="Magento_CatalogSampleData"/>
1515
<module name="Magento_BundleSampleData"/>
1616
<module name="Magento_ConfigurableSampleData"/>
17-
<module name="Magento_GroupedSampleData"/>
17+
<module name="Magento_GroupedProductSampleData"/>
1818
<module name="Magento_GiftCardSampleData"/>
1919
</sequence>
2020
</module>

app/code/Magento/WishlistSampleData/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<module name="Magento_CatalogSampleData"/>
1515
<module name="Magento_BundleSampleData"/>
1616
<module name="Magento_ConfigurableSampleData"/>
17-
<module name="Magento_GroupedSampleData"/>
17+
<module name="Magento_GroupedProductSampleData"/>
1818
<module name="Magento_GiftCardSampleData"/>
1919
</sequence>
2020
</module>

0 commit comments

Comments
 (0)