Skip to content

Commit ca7c2fc

Browse files
authored
Merge pull request magento#96 from magento-borg/MAGETWO-94218-productlinks-data
MAGETWO-94218 Productlinks data
2 parents b3fd1a7 + ede8248 commit ca7c2fc

File tree

9 files changed

+46
-23
lines changed

9 files changed

+46
-23
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: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
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;
811
use Magento\Framework\Setup\SampleData\Context as SampleDataContext;
912

1013
/**
@@ -23,29 +26,37 @@ class ProductLink
2326
protected $csvReader;
2427

2528
/**
26-
* @var \Magento\Catalog\Model\ProductFactory
29+
* @var ProductFactory
2730
*/
2831
protected $productFactory;
2932

3033
/**
31-
* @var \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks
34+
* @var ProductLinkInterfaceFactory
3235
*/
33-
protected $linksInitializer;
36+
private $productLinkFactory;
37+
38+
/**
39+
* @var ProductLinkRepositoryInterface
40+
*/
41+
private $productLinkRepository;
3442

3543
/**
3644
* @param SampleDataContext $sampleDataContext
37-
* @param \Magento\Catalog\Model\ProductFactory $productFactory
38-
* @param \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linksInitializer
45+
* @param ProductFactory $productFactory
46+
* @param ProductLinkInterfaceFactory $productLinkFactory
47+
* @param ProductLinkRepositoryInterface $productLinkRepository
3948
*/
4049
public function __construct(
4150
SampleDataContext $sampleDataContext,
42-
\Magento\Catalog\Model\ProductFactory $productFactory,
43-
\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linksInitializer
51+
ProductFactory $productFactory,
52+
ProductLinkInterfaceFactory $productLinkFactory,
53+
ProductLinkRepositoryInterface $productLinkRepository
4454
) {
4555
$this->fixtureManager = $sampleDataContext->getFixtureManager();
4656
$this->csvReader = $sampleDataContext->getCsvReader();
4757
$this->productFactory = $productFactory;
48-
$this->linksInitializer = $linksInitializer;
58+
$this->productLinkFactory = $productLinkFactory;
59+
$this->productLinkRepository = $productLinkRepository;
4960
}
5061

5162
/**
@@ -74,22 +85,33 @@ public function install(array $related, array $upsell, array $crosssell)
7485
foreach ($row as $key => $value) {
7586
$data[$header[$key]] = $value;
7687
}
77-
$row = $data;
78-
/** @var \Magento\Catalog\Model\Product $product */
88+
7989
$product = $this->productFactory->create();
80-
$productId = $product->getIdBySku($row['sku']);
90+
$productId = $product->getIdBySku($data['sku']);
8191
if (!$productId) {
8292
continue;
8393
}
8494
$product->setId($productId);
85-
$links = [$linkType => []];
86-
foreach (explode("\n", $row['linked_sku']) as $linkedProductSku) {
95+
$product->setSku($data['sku']);
96+
$links = $this->productLinkRepository->getList($product);
97+
$linkedSkusByType = array_fill_keys(array_keys($linkTypes), []);
98+
foreach ($links as $link) {
99+
$linkedSkusByType[$link->getLinkType()][] = $link->getLinkedProductSku();
100+
}
101+
102+
foreach (explode("\n", $data['linked_sku']) as $linkedProductSku) {
87103
$linkedProductId = $product->getIdBySku($linkedProductSku);
88-
if ($linkedProductId) {
89-
$links[$linkType][$linkedProductId] = [];
104+
if (!$linkedProductId || in_array($linkedProductSku, $linkedSkusByType[$linkType])) {
105+
continue;
90106
}
107+
108+
$productLink = $this->productLinkFactory->create();
109+
$productLink->setSku($data['sku'])
110+
->setLinkedProductSku($linkedProductSku)
111+
->setLinkType($linkType);
112+
$links[] = $productLink;
91113
}
92-
$this->linksInitializer->initializeLinks($product, $links);
114+
$product->setProductLinks($links);
93115
$product->getLinkInstance()->saveProductRelations($product);
94116
}
95117
}

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)