Skip to content

Commit

Permalink
Merge pull request #246 from magento-fearless-kiwis/develop
Browse files Browse the repository at this point in the history
[FearlessKiwis] Added Bundle Product to Catalog Product Data Object
  • Loading branch information
Tang, Yu(ytang1) committed Apr 22, 2015
2 parents ddfab8b + 5088fe2 commit cc6ec2b
Show file tree
Hide file tree
Showing 40 changed files with 2,032 additions and 320 deletions.
33 changes: 18 additions & 15 deletions app/code/Magento/Bundle/Api/Data/LinkInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@

interface LinkInterface extends \Magento\Framework\Api\ExtensibleDataInterface
{
const PRICE_TYPE_FIXED = 0;
const PRICE_TYPE_PERCENT = 1;

/**
* Get the identifier
*
* @return string|null
*/
public function getId();

/**
* Set id
*
* @param string $id
* @return $this
*/
public function setId($id);

/**
* Get linked product sku
*
Expand Down Expand Up @@ -69,21 +87,6 @@ public function getPosition();
*/
public function setPosition($position);

/**
* Get is defined
*
* @return bool|null
*/
public function getIsDefined();

/**
* Set is defined
*
* @param bool $isDefined
* @return $this
*/
public function setIsDefined($isDefined);

/**
* Get is default
*
Expand Down
20 changes: 17 additions & 3 deletions app/code/Magento/Bundle/Api/ProductLinkManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ interface ProductLinkManagementInterface
/**
* Get all children for Bundle product
*
* @param string $productId
* @param string $productSku
* @param int $optionId
* @return \Magento\Bundle\Api\Data\LinkInterface[]
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\InputException
*/
public function getChildren($productId);
public function getChildren($productSku, $optionId = null);

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

/**
* @param string $sku
* @param \Magento\Bundle\Api\Data\LinkInterface $linkedProduct
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
* @return bool
*/
public function saveChild(
$sku,
\Magento\Bundle\Api\Data\LinkInterface $linkedProduct
);

/**
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @param int $optionId
* @param Data\LinkInterface $linkedProduct
* @param \Magento\Bundle\Api\Data\LinkInterface $linkedProduct
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
Expand Down
40 changes: 19 additions & 21 deletions app/code/Magento/Bundle/Model/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,34 @@ class Link extends \Magento\Framework\Model\AbstractExtensibleModel implements
/**#@+
* Constants
*/
const KEY_ID = 'id';
const KEY_SKU = 'sku';
const KEY_OPTION_ID = 'option_id';
const KEY_QTY = 'qty';
const KEY_POSITION = 'position';
const KEY_IS_DEFINED = 'is_defined';
const KEY_IS_DEFAULT = 'is_default';
const KEY_PRICE = 'price';
const KEY_PRICE_TYPE = 'price_type';
const KEY_CAN_CHANGE_QUANTITY = 'can_change_quantity';
const KEY_CAN_CHANGE_QUANTITY = 'selection_can_change_quantity';
/**#@-*/

/**
* {@inheritdoc}
*/
public function getId()
{
return $this->getData(self::KEY_ID);
}

/**
* {@inheritdoc}
*/
public function setId($id)
{
return $this->setData(self::KEY_ID, $id);
}


/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -59,14 +76,6 @@ public function getPosition()
return $this->getData(self::KEY_POSITION);
}

/**
* {@inheritdoc}
*/
public function getIsDefined()
{
return $this->getData(self::KEY_IS_DEFINED);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -143,17 +152,6 @@ public function setPosition($position)
return $this->setData(self::KEY_POSITION, $position);
}

/**
* Set is defined
*
* @param bool $isDefined
* @return $this
*/
public function setIsDefined($isDefined)
{
return $this->setData(self::KEY_IS_DEFINED, $isDefined);
}

/**
* Set is default
*
Expand Down
129 changes: 105 additions & 24 deletions app/code/Magento/Bundle/Model/LinkManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function getChildren($productId)
public function getChildren($productSku, $optionId = null)
{
$product = $this->productRepository->get($productId);
$product = $this->productRepository->get($productSku);
if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
throw new InputException(__('Only implemented for bundle product'));
}

$childrenList = [];
foreach ($this->getOptions($product) as $option) {
if ($optionId !== null && $option->getOptionId() != $optionId) {
continue;
}
/** @var \Magento\Catalog\Model\Product $selection */
foreach ($option->getSelections() as $selection) {
$childrenList[] = $this->buildLink($selection, $product);
Expand All @@ -107,6 +110,93 @@ public function addChildByProductSku($sku, $optionId, \Magento\Bundle\Api\Data\L
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function saveChild(
$sku,
\Magento\Bundle\Api\Data\LinkInterface $linkedProduct
) {
$product = $this->productRepository->get($sku);
if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
throw new InputException(
__('Product with specified sku: "%1" is not a bundle product', [$product->getSku()])
);
}

/** @var \Magento\Catalog\Model\Product $linkProductModel */
$linkProductModel = $this->productRepository->get($linkedProduct->getSku());
if ($linkProductModel->isComposite()) {
throw new InputException(__('Bundle product could not contain another composite product'));
}

if (!$linkedProduct->getId()) {
throw new InputException(__('Id field of product link is required'));
}

/** @var \Magento\Bundle\Model\Selection $selectionModel */
$selectionModel = $this->bundleSelection->create();
$selectionModel->load($linkedProduct->getId());
if (!$selectionModel->getId()) {
throw new InputException(__('Can not find product link with id "%1"', [$linkedProduct->getId()]));
}

$selectionModel = $this->mapProductLinkToSelectionModel(
$selectionModel,
$linkedProduct,
$linkProductModel->getId(),
$product->getId()
);

try {
$selectionModel->save();
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
}

return true;
}

/**
* @param \Magento\Bundle\Model\Selection $selectionModel
* @param \Magento\Bundle\Api\Data\LinkInterface $productLink
* @param string $linkedProductId
* @param string $parentProductId
* @return \Magento\Bundle\Model\Selection
*/
protected function mapProductLinkToSelectionModel(
\Magento\Bundle\Model\Selection $selectionModel,
\Magento\Bundle\Api\Data\LinkInterface $productLink,
$linkedProductId,
$parentProductId
) {
$selectionModel->setProductId($linkedProductId);
$selectionModel->setParentProductId($parentProductId);
if (($productLink->getOptionId() !== null)) {
$selectionModel->setOptionId($productLink->getOptionId());
}
if ($productLink->getPosition() !== null) {
$selectionModel->setPosition($productLink->getPosition());
}
if ($productLink->getQty() !== null) {
$selectionModel->setSelectionQty($productLink->getQty());
}
if ($productLink->getPriceType() !== null) {
$selectionModel->setSelectionPriceType($productLink->getPriceType());
}
if ($productLink->getPrice() !== null) {
$selectionModel->setSelectionPriceValue($productLink->getPrice());
}
if ($productLink->getCanChangeQuantity() !== null) {
$selectionModel->setSelectionCanChangeQty($productLink->getCanChangeQuantity());
}
if ($productLink->getIsDefault() !== null) {
$selectionModel->setIsDefault($productLink->getIsDefault());
}

return $selectionModel;
}

/**
* {@inheritdoc}
*/
public function addChild(
\Magento\Catalog\Api\Data\ProductInterface $product,
$optionId,
Expand All @@ -119,17 +209,10 @@ public function addChild(
}

$options = $this->optionCollection->create();
$options->setProductIdFilter($product->getId())->joinValues($this->storeManager->getStore()->getId());
$isNewOption = true;
/** @var \Magento\Bundle\Model\Option $option */
foreach ($options as $option) {
if ($option->getOptionId() == $optionId) {
$isNewOption = false;
break;
}
}
$options->setIdFilter($optionId);
$existingOption = $options->getFirstItem();

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

$selectionModel = $this->bundleSelection->create();
$selectionModel->setOptionId($optionId)
->setPosition($linkedProduct->getPosition())
->setSelectionQty($linkedProduct->getQty())
->setSelectionPriceType($linkedProduct->getPriceType())
->setSelectionPriceValue($linkedProduct->getPrice())
->setSelectionCanChangeQty($linkedProduct->getCanChangeQuantity())
->setProductId($linkProductModel->getId())
->setParentProductId($product->getId())
->setIsDefault($linkedProduct->getIsDefault())
->setWebsiteId($this->storeManager->getStore()->getWebsiteId());
$selectionModel = $this->mapProductLinkToSelectionModel(
$selectionModel,
$linkedProduct,
$linkProductModel->getId(),
$product->getId()
);
$selectionModel->setOptionId($optionId);

try {
$selectionModel->save();
Expand Down Expand Up @@ -242,16 +322,17 @@ private function buildLink(\Magento\Catalog\Model\Product $selection, \Magento\C
'\Magento\Bundle\Api\Data\LinkInterface'
);
$link->setIsDefault($selection->getIsDefault())
->setId($selection->getSelectionId())
->setQty($selection->getSelectionQty())
->setIsDefined($selection->getSelectionCanChangeQty())
->setCanChangeQuantity($selection->getSelectionCanChangeQty())
->setPrice($selectionPrice)
->setPriceType($selectionPriceType);
return $link;
}

/**
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @return \Magento\Bundle\Api\Data\OptionTypeInterface[]
* @return \Magento\Bundle\Api\Data\OptionInterface[]
*/
private function getOptions(\Magento\Catalog\Api\Data\ProductInterface $product)
{
Expand Down
Loading

0 comments on commit cc6ec2b

Please sign in to comment.