/**
  * Get product links list
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @return \Magento\Catalog\Api\Data\ProductLinkInterface[]
  */
 public function getList(\Magento\Catalog\Api\Data\ProductInterface $product)
 {
     $output = [];
     $linkTypes = $this->linkTypeProvider->getLinkTypes();
     foreach (array_keys($linkTypes) as $linkTypeName) {
         $collection = $this->entityCollectionProvider->getCollection($product, $linkTypeName);
         foreach ($collection as $item) {
             /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
             $productLink = $this->productLinkFactory->create();
             $productLink->setSku($product->getSku())->setLinkType($linkTypeName)->setLinkedProductSku($item['sku'])->setLinkedProductType($item['type'])->setPosition($item['position']);
             if (isset($item['custom_attributes'])) {
                 $productLinkExtension = $productLink->getExtensionAttributes();
                 if ($productLinkExtension === null) {
                     $productLinkExtension = $this->productLinkExtensionFactory->create();
                 }
                 foreach ($item['custom_attributes'] as $option) {
                     $name = $option['attribute_code'];
                     $value = $option['value'];
                     $setterName = 'set' . ucfirst($name);
                     // Check if setter exists
                     if (method_exists($productLinkExtension, $setterName)) {
                         call_user_func([$productLinkExtension, $setterName], $value);
                     }
                 }
                 $productLink->setExtensionAttributes($productLinkExtension);
             }
             $output[] = $productLink;
         }
     }
     return $output;
 }
 /**
  * Initialize grouped product links
  *
  * @param \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $subject
  * @param \Magento\Catalog\Model\Product $product
  * @param array $links
  *
  * @return \Magento\Catalog\Model\Product
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function beforeInitializeLinks(\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $subject, \Magento\Catalog\Model\Product $product, array $links)
 {
     if ($product->getTypeId() === TypeGrouped::TYPE_CODE && !$product->getGroupedReadonly()) {
         $links = isset($links[self::TYPE_NAME]) ? $links[self::TYPE_NAME] : $product->getGroupedLinkData();
         if (!is_array($links)) {
             $links = [];
         }
         if ($product->getGroupedLinkData()) {
             $links = array_merge($links, $product->getGroupedLinkData());
         }
         $newLinks = [];
         $existingLinks = $product->getProductLinks();
         foreach ($links as $linkRaw) {
             /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
             $productLink = $this->productLinkFactory->create();
             if (!isset($linkRaw['id'])) {
                 continue;
             }
             $productId = $linkRaw['id'];
             if (!isset($linkRaw['qty'])) {
                 $linkRaw['qty'] = 0;
             }
             $linkedProduct = $this->productRepository->getById($productId);
             $productLink->setSku($product->getSku())->setLinkType(self::TYPE_NAME)->setLinkedProductSku($linkedProduct->getSku())->setLinkedProductType($linkedProduct->getTypeId())->setPosition($linkRaw['position'])->getExtensionAttributes()->setQty($linkRaw['qty']);
             if (isset($linkRaw['custom_attributes'])) {
                 $productLinkExtension = $productLink->getExtensionAttributes();
                 if ($productLinkExtension === null) {
                     $productLinkExtension = $this->productLinkExtensionFactory->create();
                 }
                 foreach ($linkRaw['custom_attributes'] as $option) {
                     $name = $option['attribute_code'];
                     $value = $option['value'];
                     $setterName = 'set' . ucfirst($name);
                     // Check if setter exists
                     if (method_exists($productLinkExtension, $setterName)) {
                         call_user_func([$productLinkExtension, $setterName], $value);
                     }
                 }
                 $productLink->setExtensionAttributes($productLinkExtension);
             }
             $newLinks[] = $productLink;
         }
         $existingLinks = $this->removeUnExistingLinks($existingLinks, $newLinks);
         $product->setProductLinks(array_merge($existingLinks, $newLinks));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getLinkedItemsByType($sku, $type)
 {
     $output = [];
     $product = $this->productRepository->get($sku);
     try {
         $collection = $this->entityCollectionProvider->getCollection($product, $type);
     } catch (NoSuchEntityException $e) {
         throw new NoSuchEntityException(__('Unknown link type: %1', (string) $type));
     }
     foreach ($collection as $item) {
         /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
         $productLink = $this->productLinkFactory->create();
         $productLink->setProductSku($product->getSku())->setLinkType($type)->setLinkedProductSku($item['sku'])->setLinkedProductType($item['type'])->setPosition($item['position']);
         if (isset($item['custom_attributes'])) {
             foreach ($item['custom_attributes'] as $option) {
                 $productLink->getExtensionAttributes()->setQty($option['value']);
             }
         }
         $output[] = $productLink;
     }
     return $output;
 }
 /**
  * Setting product links
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Catalog\Model\Product
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function setProductLinks(\Magento\Catalog\Model\Product $product)
 {
     $links = (array) $this->request->getParam('links', []);
     $product->setProductLinks([]);
     $product = $this->productLinks->initializeLinks($product, $links);
     $productLinks = $product->getProductLinks();
     $linkTypes = ['related' => $product->getRelatedReadonly(), 'upsell' => $product->getUpsellReadonly(), 'crosssell' => $product->getCrosssellReadonly()];
     foreach ($linkTypes as $linkType => $readonly) {
         if (isset($links[$linkType]) && !$readonly) {
             foreach ((array) $links[$linkType] as $linkData) {
                 if (empty($linkData['id'])) {
                     continue;
                 }
                 $linkProduct = $this->productRepository->getById($linkData['id']);
                 $link = $this->productLinkFactory->create();
                 $link->setSku($product->getSku())->setLinkedProductSku($linkProduct->getSku())->setLinkType($linkType)->setPosition(isset($linkData['position']) ? (int) $linkData['position'] : 0);
                 $productLinks[] = $link;
             }
         }
     }
     return $product->setProductLinks($productLinks);
 }
Beispiel #5
0
 /**
  * Initialize product before saving
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Catalog\Model\Product
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function initialize(\Magento\Catalog\Model\Product $product)
 {
     $productData = $this->request->getPost('product');
     unset($productData['custom_attributes']);
     unset($productData['extension_attributes']);
     if ($productData) {
         $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : [];
         $productData['stock_data'] = $this->stockFilter->filter($stockData);
     }
     foreach (['category_ids', 'website_ids'] as $field) {
         if (!isset($productData[$field])) {
             $productData[$field] = [];
         }
     }
     $wasLockedMedia = false;
     if ($product->isLockedAttribute('media')) {
         $product->unlockAttribute('media');
         $wasLockedMedia = true;
     }
     $dateFieldFilters = [];
     $attributes = $product->getAttributes();
     foreach ($attributes as $attrKey => $attribute) {
         if ($attribute->getBackend()->getType() == 'datetime') {
             if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') {
                 $dateFieldFilters[$attrKey] = $this->dateFilter;
             }
         }
     }
     $inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData);
     $productData = $inputFilter->getUnescaped();
     $product->addData($productData);
     if ($wasLockedMedia) {
         $product->lockAttribute('media');
     }
     if ($this->storeManager->hasSingleStore()) {
         $product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
     }
     /**
      * Check "Use Default Value" checkboxes values
      */
     $useDefaults = $this->request->getPost('use_default');
     if ($useDefaults) {
         foreach ($useDefaults as $attributeCode) {
             $product->setData($attributeCode, false);
         }
     }
     $links = $this->request->getPost('links');
     $links = is_array($links) ? $links : [];
     $linkTypes = ['related', 'upsell', 'crosssell'];
     foreach ($linkTypes as $type) {
         if (isset($links[$type])) {
             $links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]);
         }
     }
     $product = $this->productLinks->initializeLinks($product, $links);
     $productLinks = $product->getProductLinks();
     $linkTypes = ['related' => $product->getRelatedReadonly(), 'upsell' => $product->getUpsellReadonly(), 'crosssell' => $product->getCrosssellReadonly()];
     foreach ($linkTypes as $linkType => $readonly) {
         if (isset($links[$linkType]) && !$readonly) {
             foreach ($links[$linkType] as $linkId => $linkData) {
                 $linkProduct = $this->productRepository->getById($linkId);
                 $link = $this->productLinkFactory->create();
                 $link->setSku($product->getSku())->setLinkedProductSku($linkProduct->getSku())->setLinkType($linkType)->setPosition(isset($linkData['position']) ? (int) $linkData['position'] : 0);
                 $productLinks[] = $link;
             }
         }
     }
     $product->setProductLinks($productLinks);
     /**
      * Initialize product options
      */
     if (isset($productData['options']) && !$product->getOptionsReadonly()) {
         // mark custom options that should to fall back to default value
         $options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default'));
         $customOptions = [];
         foreach ($options as $customOptionData) {
             if (!(bool) $customOptionData['is_delete']) {
                 $customOption = $this->customOptionFactory->create(['data' => $customOptionData]);
                 $customOption->setProductSku($product->getSku());
                 $customOption->setOptionId(null);
                 $customOptions[] = $customOption;
             }
         }
         $product->setOptions($customOptions);
     }
     $product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly());
     return $product;
 }