Skip to content

Commit 02b20cc

Browse files
author
Leonid Poluyanov
committed
MAGETWO-50450: Bundle product has no products in sample data
1 parent e5923c0 commit 02b20cc

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

app/code/Magento/BundleSampleData/Model/Product.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
namespace Magento\BundleSampleData\Model;
77

88
use Magento\Framework\Setup\SampleData\Context as SampleDataContext;
9+
use Magento\Bundle\Api\Data\OptionInterfaceFactory as OptionFactory;
10+
use Magento\Bundle\Api\Data\LinkInterfaceFactory as LinkFactory;
11+
use Magento\Catalog\Api\ProductRepositoryInterface as ProductRepository;
912

1013
/**
1114
* Setup bundle product
@@ -17,6 +20,21 @@ class Product extends \Magento\CatalogSampleData\Model\Product
1720
*/
1821
protected $productType = \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE;
1922

23+
/**
24+
* @var OptionFactory
25+
*/
26+
protected $optionFactory;
27+
28+
/**
29+
* @var LinkFactory
30+
*/
31+
protected $linkFactory;
32+
33+
/**
34+
* @var ProductRepository
35+
*/
36+
protected $productRepository;
37+
2038
/**
2139
* Product constructor.
2240
* @param SampleDataContext $sampleDataContext
@@ -34,7 +52,10 @@ public function __construct(
3452
\Magento\BundleSampleData\Model\Product\Converter $converter,
3553
\Magento\CatalogSampleData\Model\Product\Gallery $gallery,
3654
\Magento\Store\Model\StoreManagerInterface $storeManager,
37-
\Magento\Eav\Model\Config $eavConfig
55+
\Magento\Eav\Model\Config $eavConfig,
56+
OptionFactory $optionFactory,
57+
LinkFactory $linkFactory,
58+
ProductRepository $productRepository
3859
) {
3960
$this->eavConfig = $eavConfig;
4061
parent::__construct(
@@ -46,6 +67,9 @@ public function __construct(
4667
$storeManager,
4768
$eavConfig
4869
);
70+
$this->optionFactory = $optionFactory;
71+
$this->linkFactory = $linkFactory;
72+
$this->productRepository = $productRepository;
4973
}
5074

5175
/**
@@ -57,6 +81,34 @@ protected function prepareProduct($product, $data)
5781
->setCanSaveConfigurableAttributes(true)
5882
->setCanSaveBundleSelections(true)
5983
->setPriceType(0);
84+
$bundleOptionsData = $product->getBundleOptionsData();
85+
$options = [];
86+
foreach ($bundleOptionsData as $key => $optionData) {
87+
88+
$option = $this->optionFactory->create(['data' => $optionData]);
89+
$option->setSku($product->getSku());
90+
$option->setOptionId(null);
91+
92+
$links = [];
93+
$bundleLinks = $product->getBundleSelectionsData();
94+
foreach ($bundleLinks[$key] as $linkData) {
95+
$linkProduct = $this->productRepository->getById($linkData['product_id']);
96+
$link = $this->linkFactory->create(['data' => $linkData]);
97+
$link->setSku($linkProduct->getSku());
98+
$link->setQty($linkData['selection_qty']);
99+
100+
if (array_key_exists('selection_can_change_qty', $linkData)) {
101+
$link->setCanChangeQuantity($linkData['selection_can_change_qty']);
102+
}
103+
$links[] = $link;
104+
}
105+
$option->setProductLinks($links);
106+
$options[] = $option;
107+
}
108+
109+
$extension = $product->getExtensionAttributes();
110+
$extension->setBundleProductOptions($options);
111+
$product->setExtensionAttributes($extension);
60112

61113
return $this;
62114
}

app/code/Magento/CatalogSampleData/Model/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ protected function installGallery($product)
166166
}
167167

168168
/**
169-
* @param \Magento\Framework\Model\AbstractModel $product
169+
* @param \Magento\Catalog\Model\Product $product
170170
* @param array $data
171171
* @return $this
172172
* @SuppressWarnings(PHPMD.UnusedFormalParameter)

0 commit comments

Comments
 (0)