Skip to content

Commit

Permalink
Merge pull request #96 from magento-borg/MAGETWO-94218-productlinks-data
Browse files Browse the repository at this point in the history
MAGETWO-94218 Productlinks data
  • Loading branch information
cpartica authored Sep 2, 2018
2 parents b3fd1a7 + ede8248 commit ca7c2fc
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/CmsSampleData/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<module name="Magento_CatalogSampleData"/>
<module name="Magento_BundleSampleData"/>
<module name="Magento_ConfigurableSampleData"/>
<module name="Magento_GroupedSampleData"/>
<module name="Magento_GroupedProductSampleData"/>
<module name="Magento_GiftCardSampleData"/>
<module name="Magento_ThemeSampleData"/>
</sequence>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function install()
$this->productLinkSetup->install(
['Magento_ConfigurableSampleData::fixtures/Links/related.csv'],
['Magento_ConfigurableSampleData::fixtures/Links/upsell.csv'],
['Magento_ConfigurableSampleData::fixtures/Links/crossell.csv']
['Magento_ConfigurableSampleData::fixtures/Links/crosssell.csv']
);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/ConfigurableSampleData/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<module name="Magento_CatalogSampleData"/>
<module name="Magento_DownloadableSampleData"/>
<module name="Magento_BundleSampleData"/>
<module name="Magento_GroupedProductSampleData"/>
</sequence>
</module>
</config>
54 changes: 38 additions & 16 deletions app/code/Magento/ProductLinksSampleData/Model/ProductLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\ProductLinksSampleData\Model;

use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
use Magento\Catalog\Api\ProductLinkRepositoryInterface;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\Setup\SampleData\Context as SampleDataContext;

/**
Expand All @@ -23,29 +26,37 @@ class ProductLink
protected $csvReader;

/**
* @var \Magento\Catalog\Model\ProductFactory
* @var ProductFactory
*/
protected $productFactory;

/**
* @var \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks
* @var ProductLinkInterfaceFactory
*/
protected $linksInitializer;
private $productLinkFactory;

/**
* @var ProductLinkRepositoryInterface
*/
private $productLinkRepository;

/**
* @param SampleDataContext $sampleDataContext
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linksInitializer
* @param ProductFactory $productFactory
* @param ProductLinkInterfaceFactory $productLinkFactory
* @param ProductLinkRepositoryInterface $productLinkRepository
*/
public function __construct(
SampleDataContext $sampleDataContext,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linksInitializer
ProductFactory $productFactory,
ProductLinkInterfaceFactory $productLinkFactory,
ProductLinkRepositoryInterface $productLinkRepository
) {
$this->fixtureManager = $sampleDataContext->getFixtureManager();
$this->csvReader = $sampleDataContext->getCsvReader();
$this->productFactory = $productFactory;
$this->linksInitializer = $linksInitializer;
$this->productLinkFactory = $productLinkFactory;
$this->productLinkRepository = $productLinkRepository;
}

/**
Expand Down Expand Up @@ -74,22 +85,33 @@ public function install(array $related, array $upsell, array $crosssell)
foreach ($row as $key => $value) {
$data[$header[$key]] = $value;
}
$row = $data;
/** @var \Magento\Catalog\Model\Product $product */

$product = $this->productFactory->create();
$productId = $product->getIdBySku($row['sku']);
$productId = $product->getIdBySku($data['sku']);
if (!$productId) {
continue;
}
$product->setId($productId);
$links = [$linkType => []];
foreach (explode("\n", $row['linked_sku']) as $linkedProductSku) {
$product->setSku($data['sku']);
$links = $this->productLinkRepository->getList($product);
$linkedSkusByType = array_fill_keys(array_keys($linkTypes), []);
foreach ($links as $link) {
$linkedSkusByType[$link->getLinkType()][] = $link->getLinkedProductSku();
}

foreach (explode("\n", $data['linked_sku']) as $linkedProductSku) {
$linkedProductId = $product->getIdBySku($linkedProductSku);
if ($linkedProductId) {
$links[$linkType][$linkedProductId] = [];
if (!$linkedProductId || in_array($linkedProductSku, $linkedSkusByType[$linkType])) {
continue;
}

$productLink = $this->productLinkFactory->create();
$productLink->setSku($data['sku'])
->setLinkedProductSku($linkedProductSku)
->setLinkType($linkType);
$links[] = $productLink;
}
$this->linksInitializer->initializeLinks($product, $links);
$product->setProductLinks($links);
$product->getLinkInstance()->saveProductRelations($product);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function install()
$this->productLink->install(
['Magento_ProductLinksSampleData::fixtures/related.csv'],
['Magento_ProductLinksSampleData::fixtures/upsell.csv'],
['Magento_ProductLinksSampleData::fixtures/crossell.csv']
['Magento_ProductLinksSampleData::fixtures/crosssell.csv']
);
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/ProductLinksSampleData/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<module name="Magento_CatalogSampleData"/>
<module name="Magento_BundleSampleData"/>
<module name="Magento_ConfigurableSampleData"/>
<module name="Magento_GroupedSampleData"/>
<module name="Magento_GroupedProductSampleData"/>
<module name="Magento_GiftCardSampleData"/>
</sequence>
</module>
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/ReviewSampleData/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<module name="Magento_CatalogSampleData"/>
<module name="Magento_BundleSampleData"/>
<module name="Magento_ConfigurableSampleData"/>
<module name="Magento_GroupedSampleData"/>
<module name="Magento_GroupedProductSampleData"/>
<module name="Magento_GiftCardSampleData"/>
</sequence>
</module>
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/SalesSampleData/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<module name="Magento_CatalogSampleData"/>
<module name="Magento_BundleSampleData"/>
<module name="Magento_ConfigurableSampleData"/>
<module name="Magento_GroupedSampleData"/>
<module name="Magento_GroupedProductSampleData"/>
<module name="Magento_GiftCardSampleData"/>
</sequence>
</module>
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/WishlistSampleData/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<module name="Magento_CatalogSampleData"/>
<module name="Magento_BundleSampleData"/>
<module name="Magento_ConfigurableSampleData"/>
<module name="Magento_GroupedSampleData"/>
<module name="Magento_GroupedProductSampleData"/>
<module name="Magento_GiftCardSampleData"/>
</sequence>
</module>
Expand Down

0 comments on commit ca7c2fc

Please sign in to comment.