Skip to content

Commit 6b62a79

Browse files
author
Yaroslav Onischenko
committed
Merge remote-tracking branch 'origin/MAGETWO-50450' into develop
2 parents 1239318 + 4866aad commit 6b62a79

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
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;
12+
use \Magento\Framework\App\ObjectManager;
913

1014
/**
1115
* Setup bundle product
@@ -17,6 +21,11 @@ class Product extends \Magento\CatalogSampleData\Model\Product
1721
*/
1822
protected $productType = \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE;
1923

24+
/**
25+
* @var OptionFactory
26+
*/
27+
private $optionFactory;
28+
2029
/**
2130
* Product constructor.
2231
* @param SampleDataContext $sampleDataContext
@@ -48,6 +57,16 @@ public function __construct(
4857
);
4958
}
5059

60+
/**
61+
* @var LinkFactory
62+
*/
63+
private $linkFactory;
64+
65+
/**
66+
* @var ProductRepository
67+
*/
68+
private $productRepository;
69+
5170
/**
5271
* @inheritdoc
5372
*/
@@ -57,7 +76,88 @@ protected function prepareProduct($product, $data)
5776
->setCanSaveConfigurableAttributes(true)
5877
->setCanSaveBundleSelections(true)
5978
->setPriceType(0);
79+
$bundleOptionsData = $product->getBundleOptionsData();
80+
$options = [];
81+
foreach ($bundleOptionsData as $key => $optionData) {
82+
$option = $this->getOptionFactory()->create(['data' => $optionData]);
83+
$option->setSku($product->getSku());
84+
$option->setOptionId(null);
85+
86+
$links = [];
87+
$bundleLinks = $product->getBundleSelectionsData();
88+
foreach ($bundleLinks[$key] as $linkData) {
89+
$linkProduct = $this->getProductRepository()->getById($linkData['product_id']);
90+
$link = $this->getLinkFactory()->create(['data' => $linkData]);
91+
$link->setSku($linkProduct->getSku());
92+
$link->setQty($linkData['selection_qty']);
93+
94+
if (array_key_exists('selection_can_change_qty', $linkData)) {
95+
$link->setCanChangeQuantity($linkData['selection_can_change_qty']);
96+
}
97+
$links[] = $link;
98+
}
99+
$option->setProductLinks($links);
100+
$options[] = $option;
101+
}
102+
103+
$extension = $product->getExtensionAttributes();
104+
$extension->setBundleProductOptions($options);
105+
$product->setExtensionAttributes($extension);
60106

61107
return $this;
62108
}
109+
110+
/**
111+
* Get option interface factory
112+
*
113+
* @deprecated
114+
* @return \Magento\Bundle\Api\Data\OptionInterfaceFactory
115+
*/
116+
private function getOptionFactory()
117+
{
118+
119+
if (!($this->optionFactory)) {
120+
return ObjectManager::getInstance()->get(
121+
'\Magento\Bundle\Api\Data\OptionInterfaceFactory'
122+
);
123+
} else {
124+
return $this->optionFactory;
125+
}
126+
}
127+
128+
/**
129+
* Get bundle link interface factory
130+
*
131+
* @deprecated
132+
* @return \Magento\Bundle\Api\Data\LinkInterfaceFactory
133+
*/
134+
private function getLinkFactory()
135+
{
136+
137+
if (!($this->linkFactory)) {
138+
return ObjectManager::getInstance()->get(
139+
'\Magento\Bundle\Api\Data\LinkInterfaceFactory'
140+
);
141+
} else {
142+
return $this->linkFactory;
143+
}
144+
}
145+
146+
/**
147+
* Get product repository
148+
*
149+
* @deprecated
150+
* @return \Magento\Catalog\Api\ProductRepositoryInterface
151+
*/
152+
private function getProductRepository()
153+
{
154+
155+
if (!($this->productRepository)) {
156+
return ObjectManager::getInstance()->get(
157+
'\Magento\Catalog\Api\ProductRepositoryInterface'
158+
);
159+
} else {
160+
return $this->productRepository;
161+
}
162+
}
63163
}

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)