Ejemplo n.º 1
0
 /**
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param int $optionId
  * @return \Magento\Bundle\Api\Data\LinkInterface[]
  */
 public function getItems(\Magento\Catalog\Api\Data\ProductInterface $product, $optionId)
 {
     $selectionCollection = $this->type->getSelectionsCollection([$optionId], $product);
     $productLinks = [];
     /** @var \Magento\Catalog\Model\Product $selection */
     foreach ($selectionCollection as $selection) {
         $selectionPriceType = $product->getPriceType() ? $selection->getSelectionPriceType() : null;
         $selectionPrice = $product->getPriceType() ? $selection->getSelectionPriceValue() : null;
         /** @var \Magento\Bundle\Api\Data\LinkInterface $productLink */
         $productLink = $this->linkFactory->create();
         $this->dataObjectHelper->populateWithArray($productLink, $selection->getData(), '\\Magento\\Bundle\\Api\\Data\\LinkInterface');
         $productLink->setIsDefault($selection->getIsDefault())->setId($selection->getSelectionId())->setQty($selection->getSelectionQty())->setCanChangeQuantity($selection->getSelectionCanChangeQty())->setPrice($selectionPrice)->setPriceType($selectionPriceType);
         $productLinks[] = $productLink;
     }
     return $productLinks;
 }
Ejemplo n.º 2
0
 /**
  * @param \Magento\Catalog\Model\Product $selection
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Bundle\Api\Data\LinkInterface
  */
 private function buildLink(\Magento\Catalog\Model\Product $selection, \Magento\Catalog\Model\Product $product)
 {
     $selectionPriceType = $selectionPrice = null;
     /** @var \Magento\Bundle\Model\Selection $product */
     if ($product->getPriceType()) {
         $selectionPriceType = $selection->getSelectionPriceType();
         $selectionPrice = $selection->getSelectionPriceValue();
     }
     /** @var \Magento\Bundle\Api\Data\LinkInterface $link */
     $link = $this->linkFactory->create();
     $this->dataObjectHelper->populateWithArray($link, $selection->getData(), '\\Magento\\Bundle\\Api\\Data\\LinkInterface');
     $link->setIsDefault($selection->getIsDefault())->setQty($selection->getSelectionQty())->setIsDefined($selection->getSelectionCanChangeQty())->setPrice($selectionPrice)->setPriceType($selectionPriceType);
     return $link;
 }
 /**
  * @param \Magento\Catalog\Model\Product $product
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function processBundleOptionsData(\Magento\Catalog\Model\Product $product)
 {
     $bundleOptionsData = $product->getBundleOptionsData();
     if (!$bundleOptionsData) {
         return;
     }
     $options = [];
     foreach ($bundleOptionsData as $key => $optionData) {
         if ((bool) $optionData['delete']) {
             continue;
         }
         $option = $this->optionFactory->create(['data' => $optionData]);
         $option->setSku($product->getSku());
         $option->setOptionId(null);
         $links = [];
         $bundleLinks = $product->getBundleSelectionsData();
         if (empty($bundleLinks[$key])) {
             continue;
         }
         foreach ($bundleLinks[$key] as $linkData) {
             if ((bool) $linkData['delete']) {
                 continue;
             }
             $link = $this->linkFactory->create(['data' => $linkData]);
             if ((int) $product->getPriceType() !== \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC) {
                 if (array_key_exists('selection_price_value', $linkData)) {
                     $link->setPrice($linkData['selection_price_value']);
                 }
                 if (array_key_exists('selection_price_type', $linkData)) {
                     $link->setPriceType($linkData['selection_price_type']);
                 }
             }
             $linkProduct = $this->productRepository->getById($linkData['product_id']);
             $link->setSku($linkProduct->getSku());
             $link->setQty($linkData['selection_qty']);
             if (array_key_exists('selection_can_change_qty', $linkData)) {
                 $link->setCanChangeQuantity($linkData['selection_can_change_qty']);
             }
             $links[] = $link;
         }
         $option->setProductLinks($links);
         $options[] = $option;
     }
     $extension = $product->getExtensionAttributes();
     $extension->setBundleProductOptions($options);
     $product->setExtensionAttributes($extension);
     return;
 }