Skip to content

Commit cc6ec2b

Browse files
author
Tang, Yu(ytang1)
committed
Merge pull request #246 from magento-fearless-kiwis/develop
[FearlessKiwis] Added Bundle Product to Catalog Product Data Object
2 parents ddfab8b + 5088fe2 commit cc6ec2b

File tree

40 files changed

+2032
-320
lines changed

40 files changed

+2032
-320
lines changed

app/code/Magento/Bundle/Api/Data/LinkInterface.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99

1010
interface LinkInterface extends \Magento\Framework\Api\ExtensibleDataInterface
1111
{
12+
const PRICE_TYPE_FIXED = 0;
13+
const PRICE_TYPE_PERCENT = 1;
14+
15+
/**
16+
* Get the identifier
17+
*
18+
* @return string|null
19+
*/
20+
public function getId();
21+
22+
/**
23+
* Set id
24+
*
25+
* @param string $id
26+
* @return $this
27+
*/
28+
public function setId($id);
29+
1230
/**
1331
* Get linked product sku
1432
*
@@ -69,21 +87,6 @@ public function getPosition();
6987
*/
7088
public function setPosition($position);
7189

72-
/**
73-
* Get is defined
74-
*
75-
* @return bool|null
76-
*/
77-
public function getIsDefined();
78-
79-
/**
80-
* Set is defined
81-
*
82-
* @param bool $isDefined
83-
* @return $this
84-
*/
85-
public function setIsDefined($isDefined);
86-
8790
/**
8891
* Get is default
8992
*

app/code/Magento/Bundle/Api/ProductLinkManagementInterface.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ interface ProductLinkManagementInterface
1111
/**
1212
* Get all children for Bundle product
1313
*
14-
* @param string $productId
14+
* @param string $productSku
15+
* @param int $optionId
1516
* @return \Magento\Bundle\Api\Data\LinkInterface[]
1617
* @throws \Magento\Framework\Exception\NoSuchEntityException
1718
* @throws \Magento\Framework\Exception\InputException
1819
*/
19-
public function getChildren($productId);
20+
public function getChildren($productSku, $optionId = null);
2021

2122
/**
2223
* Add child product to specified Bundle option by product sku
@@ -31,10 +32,23 @@ public function getChildren($productId);
3132
*/
3233
public function addChildByProductSku($sku, $optionId, \Magento\Bundle\Api\Data\LinkInterface $linkedProduct);
3334

35+
/**
36+
* @param string $sku
37+
* @param \Magento\Bundle\Api\Data\LinkInterface $linkedProduct
38+
* @throws \Magento\Framework\Exception\NoSuchEntityException
39+
* @throws \Magento\Framework\Exception\CouldNotSaveException
40+
* @throws \Magento\Framework\Exception\InputException
41+
* @return bool
42+
*/
43+
public function saveChild(
44+
$sku,
45+
\Magento\Bundle\Api\Data\LinkInterface $linkedProduct
46+
);
47+
3448
/**
3549
* @param \Magento\Catalog\Api\Data\ProductInterface $product
3650
* @param int $optionId
37-
* @param Data\LinkInterface $linkedProduct
51+
* @param \Magento\Bundle\Api\Data\LinkInterface $linkedProduct
3852
* @throws \Magento\Framework\Exception\NoSuchEntityException
3953
* @throws \Magento\Framework\Exception\CouldNotSaveException
4054
* @throws \Magento\Framework\Exception\InputException

app/code/Magento/Bundle/Model/Link.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,34 @@ class Link extends \Magento\Framework\Model\AbstractExtensibleModel implements
1616
/**#@+
1717
* Constants
1818
*/
19+
const KEY_ID = 'id';
1920
const KEY_SKU = 'sku';
2021
const KEY_OPTION_ID = 'option_id';
2122
const KEY_QTY = 'qty';
2223
const KEY_POSITION = 'position';
23-
const KEY_IS_DEFINED = 'is_defined';
2424
const KEY_IS_DEFAULT = 'is_default';
2525
const KEY_PRICE = 'price';
2626
const KEY_PRICE_TYPE = 'price_type';
27-
const KEY_CAN_CHANGE_QUANTITY = 'can_change_quantity';
27+
const KEY_CAN_CHANGE_QUANTITY = 'selection_can_change_quantity';
2828
/**#@-*/
2929

30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getId()
34+
{
35+
return $this->getData(self::KEY_ID);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function setId($id)
42+
{
43+
return $this->setData(self::KEY_ID, $id);
44+
}
45+
46+
3047
/**
3148
* {@inheritdoc}
3249
*/
@@ -59,14 +76,6 @@ public function getPosition()
5976
return $this->getData(self::KEY_POSITION);
6077
}
6178

62-
/**
63-
* {@inheritdoc}
64-
*/
65-
public function getIsDefined()
66-
{
67-
return $this->getData(self::KEY_IS_DEFINED);
68-
}
69-
7079
/**
7180
* {@inheritdoc}
7281
*/
@@ -143,17 +152,6 @@ public function setPosition($position)
143152
return $this->setData(self::KEY_POSITION, $position);
144153
}
145154

146-
/**
147-
* Set is defined
148-
*
149-
* @param bool $isDefined
150-
* @return $this
151-
*/
152-
public function setIsDefined($isDefined)
153-
{
154-
return $this->setData(self::KEY_IS_DEFINED, $isDefined);
155-
}
156-
157155
/**
158156
* Set is default
159157
*

app/code/Magento/Bundle/Model/LinkManagement.php

Lines changed: 105 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,18 @@ public function __construct(
7575
/**
7676
* {@inheritdoc}
7777
*/
78-
public function getChildren($productId)
78+
public function getChildren($productSku, $optionId = null)
7979
{
80-
$product = $this->productRepository->get($productId);
80+
$product = $this->productRepository->get($productSku);
8181
if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
8282
throw new InputException(__('Only implemented for bundle product'));
8383
}
8484

8585
$childrenList = [];
8686
foreach ($this->getOptions($product) as $option) {
87+
if ($optionId !== null && $option->getOptionId() != $optionId) {
88+
continue;
89+
}
8790
/** @var \Magento\Catalog\Model\Product $selection */
8891
foreach ($option->getSelections() as $selection) {
8992
$childrenList[] = $this->buildLink($selection, $product);
@@ -107,6 +110,93 @@ public function addChildByProductSku($sku, $optionId, \Magento\Bundle\Api\Data\L
107110
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
108111
* @SuppressWarnings(PHPMD.NPathComplexity)
109112
*/
113+
public function saveChild(
114+
$sku,
115+
\Magento\Bundle\Api\Data\LinkInterface $linkedProduct
116+
) {
117+
$product = $this->productRepository->get($sku);
118+
if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
119+
throw new InputException(
120+
__('Product with specified sku: "%1" is not a bundle product', [$product->getSku()])
121+
);
122+
}
123+
124+
/** @var \Magento\Catalog\Model\Product $linkProductModel */
125+
$linkProductModel = $this->productRepository->get($linkedProduct->getSku());
126+
if ($linkProductModel->isComposite()) {
127+
throw new InputException(__('Bundle product could not contain another composite product'));
128+
}
129+
130+
if (!$linkedProduct->getId()) {
131+
throw new InputException(__('Id field of product link is required'));
132+
}
133+
134+
/** @var \Magento\Bundle\Model\Selection $selectionModel */
135+
$selectionModel = $this->bundleSelection->create();
136+
$selectionModel->load($linkedProduct->getId());
137+
if (!$selectionModel->getId()) {
138+
throw new InputException(__('Can not find product link with id "%1"', [$linkedProduct->getId()]));
139+
}
140+
141+
$selectionModel = $this->mapProductLinkToSelectionModel(
142+
$selectionModel,
143+
$linkedProduct,
144+
$linkProductModel->getId(),
145+
$product->getId()
146+
);
147+
148+
try {
149+
$selectionModel->save();
150+
} catch (\Exception $e) {
151+
throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
152+
}
153+
154+
return true;
155+
}
156+
157+
/**
158+
* @param \Magento\Bundle\Model\Selection $selectionModel
159+
* @param \Magento\Bundle\Api\Data\LinkInterface $productLink
160+
* @param string $linkedProductId
161+
* @param string $parentProductId
162+
* @return \Magento\Bundle\Model\Selection
163+
*/
164+
protected function mapProductLinkToSelectionModel(
165+
\Magento\Bundle\Model\Selection $selectionModel,
166+
\Magento\Bundle\Api\Data\LinkInterface $productLink,
167+
$linkedProductId,
168+
$parentProductId
169+
) {
170+
$selectionModel->setProductId($linkedProductId);
171+
$selectionModel->setParentProductId($parentProductId);
172+
if (($productLink->getOptionId() !== null)) {
173+
$selectionModel->setOptionId($productLink->getOptionId());
174+
}
175+
if ($productLink->getPosition() !== null) {
176+
$selectionModel->setPosition($productLink->getPosition());
177+
}
178+
if ($productLink->getQty() !== null) {
179+
$selectionModel->setSelectionQty($productLink->getQty());
180+
}
181+
if ($productLink->getPriceType() !== null) {
182+
$selectionModel->setSelectionPriceType($productLink->getPriceType());
183+
}
184+
if ($productLink->getPrice() !== null) {
185+
$selectionModel->setSelectionPriceValue($productLink->getPrice());
186+
}
187+
if ($productLink->getCanChangeQuantity() !== null) {
188+
$selectionModel->setSelectionCanChangeQty($productLink->getCanChangeQuantity());
189+
}
190+
if ($productLink->getIsDefault() !== null) {
191+
$selectionModel->setIsDefault($productLink->getIsDefault());
192+
}
193+
194+
return $selectionModel;
195+
}
196+
197+
/**
198+
* {@inheritdoc}
199+
*/
110200
public function addChild(
111201
\Magento\Catalog\Api\Data\ProductInterface $product,
112202
$optionId,
@@ -119,17 +209,10 @@ public function addChild(
119209
}
120210

121211
$options = $this->optionCollection->create();
122-
$options->setProductIdFilter($product->getId())->joinValues($this->storeManager->getStore()->getId());
123-
$isNewOption = true;
124-
/** @var \Magento\Bundle\Model\Option $option */
125-
foreach ($options as $option) {
126-
if ($option->getOptionId() == $optionId) {
127-
$isNewOption = false;
128-
break;
129-
}
130-
}
212+
$options->setIdFilter($optionId);
213+
$existingOption = $options->getFirstItem();
131214

132-
if ($isNewOption) {
215+
if (!$existingOption->getId()) {
133216
throw new InputException(
134217
__(
135218
'Product with specified sku: "%1" does not contain option: "%2"',
@@ -161,16 +244,13 @@ public function addChild(
161244
}
162245

163246
$selectionModel = $this->bundleSelection->create();
164-
$selectionModel->setOptionId($optionId)
165-
->setPosition($linkedProduct->getPosition())
166-
->setSelectionQty($linkedProduct->getQty())
167-
->setSelectionPriceType($linkedProduct->getPriceType())
168-
->setSelectionPriceValue($linkedProduct->getPrice())
169-
->setSelectionCanChangeQty($linkedProduct->getCanChangeQuantity())
170-
->setProductId($linkProductModel->getId())
171-
->setParentProductId($product->getId())
172-
->setIsDefault($linkedProduct->getIsDefault())
173-
->setWebsiteId($this->storeManager->getStore()->getWebsiteId());
247+
$selectionModel = $this->mapProductLinkToSelectionModel(
248+
$selectionModel,
249+
$linkedProduct,
250+
$linkProductModel->getId(),
251+
$product->getId()
252+
);
253+
$selectionModel->setOptionId($optionId);
174254

175255
try {
176256
$selectionModel->save();
@@ -242,16 +322,17 @@ private function buildLink(\Magento\Catalog\Model\Product $selection, \Magento\C
242322
'\Magento\Bundle\Api\Data\LinkInterface'
243323
);
244324
$link->setIsDefault($selection->getIsDefault())
325+
->setId($selection->getSelectionId())
245326
->setQty($selection->getSelectionQty())
246-
->setIsDefined($selection->getSelectionCanChangeQty())
327+
->setCanChangeQuantity($selection->getSelectionCanChangeQty())
247328
->setPrice($selectionPrice)
248329
->setPriceType($selectionPriceType);
249330
return $link;
250331
}
251332

252333
/**
253334
* @param \Magento\Catalog\Api\Data\ProductInterface $product
254-
* @return \Magento\Bundle\Api\Data\OptionTypeInterface[]
335+
* @return \Magento\Bundle\Api\Data\OptionInterface[]
255336
*/
256337
private function getOptions(\Magento\Catalog\Api\Data\ProductInterface $product)
257338
{

0 commit comments

Comments
 (0)