/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param LinkInterface $link * @param bool $isGlobalScopeContent * @return int */ protected function saveLink(\Magento\Catalog\Api\Data\ProductInterface $product, LinkInterface $link, $isGlobalScopeContent) { $linkData = ['link_id' => $link->getid() === null ? 0 : $link->getid(), 'is_delete' => 0, 'type' => $link->getLinkType(), 'sort_order' => $link->getSortOrder(), 'title' => $link->getTitle(), 'price' => $link->getPrice(), 'number_of_downloads' => $link->getNumberOfDownloads(), 'is_shareable' => $link->getIsShareable()]; if ($link->getLinkType() == 'file' && $link->getLinkFile() === null) { $linkData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($link->getLinkFileContent(), 'link_file')]); } elseif ($link->getLinkType() === 'url') { $linkData['link_url'] = $link->getLinkUrl(); } else { //existing link file $linkData['file'] = $this->jsonEncoder->encode([['file' => $link->getLinkFile(), 'status' => 'old']]); } if ($link->getSampleType() == 'file' && $link->getSampleFile() === null) { $linkData['sample']['type'] = 'file'; $linkData['sample']['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($link->getSampleFileContent(), 'link_sample_file')]); } elseif ($link->getSampleType() == 'url') { $linkData['sample']['type'] = 'url'; $linkData['sample']['url'] = $link->getSampleUrl(); } $downloadableData = ['link' => [$linkData]]; $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $this->downloadableType->save($product); return $product->getLastAddedLinkId(); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function save($productSku, SampleContentInterface $sampleContent, $sampleId = null, $isGlobalScopeContent = false) { $product = $this->productRepository->get($productSku, true); if ($sampleId) { /** @var $sample \Magento\Downloadable\Model\Sample */ $sample = $this->sampleFactory->create()->load($sampleId); if (!$sample->getId()) { throw new NoSuchEntityException(__('There is no downloadable sample with provided ID.')); } if ($sample->getProductId() != $product->getId()) { throw new InputException(__('Provided downloadable sample is not related to given product.')); } if (!$this->contentValidator->isValid($sampleContent)) { throw new InputException(__('Provided sample information is invalid.')); } if ($isGlobalScopeContent) { $product->setStoreId(0); } $title = $sampleContent->getTitle(); if (empty($title)) { if ($isGlobalScopeContent) { throw new InputException(__('Sample title cannot be empty.')); } // use title from GLOBAL scope $sample->setTitle(null); } else { $sample->setTitle($sampleContent->getTitle()); } $sample->setProductId($product->getId())->setStoreId($product->getStoreId())->setSortOrder($sampleContent->getSortOrder())->save(); return $sample->getId(); } else { if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { throw new InputException(__('Product type of the product must be \'downloadable\'.')); } if (!$this->contentValidator->isValid($sampleContent)) { throw new InputException(__('Provided sample information is invalid.')); } if (!in_array($sampleContent->getSampleType(), ['url', 'file'])) { throw new InputException(__('Invalid sample type.')); } $title = $sampleContent->getTitle(); if (empty($title)) { throw new InputException(__('Sample title cannot be empty.')); } $sampleData = ['sample_id' => 0, 'is_delete' => 0, 'type' => $sampleContent->getSampleType(), 'sort_order' => $sampleContent->getSortOrder(), 'title' => $sampleContent->getTitle()]; if ($sampleContent->getSampleType() == 'file') { $sampleData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($sampleContent->getSampleFile(), 'sample')]); } else { $sampleData['sample_url'] = $sampleContent->getSampleUrl(); } $downloadableData = ['sample' => [$sampleData]]; $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $product->save(); return $product->getLastAddedSampleId(); } }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param SampleInterface $sample * @param bool $isGlobalScopeContent * @return int */ protected function saveSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent) { $sampleData = ['sample_id' => (int) $sample->getid(), 'is_delete' => 0, 'type' => $sample->getSampleType(), 'sort_order' => $sample->getSortOrder(), 'title' => $sample->getTitle()]; if ($sample->getSampleType() === 'file' && $sample->getSampleFile() === null) { $sampleData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($sample->getSampleFileContent(), 'sample')]); } elseif ($sample->getSampleType() === 'url') { $sampleData['sample_url'] = $sample->getSampleUrl(); } else { //existing file $sampleData['file'] = $this->jsonEncoder->encode([['file' => $sample->getSampleFile(), 'status' => 'old']]); } $downloadableData = ['sample' => [$sampleData]]; if ($isGlobalScopeContent) { $product->setStoreId(0); } $this->getSampleTypeHandler()->save($product, $downloadableData); return $product->getLastAddedSampleId(); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save($sku, LinkContentInterface $linkContent, $linkId = null, $isGlobalScopeContent = false) { $product = $this->productRepository->get($sku, true); if ($linkId) { /** @var $link \Magento\Downloadable\Model\Link */ $link = $this->linkFactory->create()->load($linkId); if (!$link->getId()) { throw new NoSuchEntityException(__('There is no downloadable link with provided ID.')); } if ($link->getProductId() != $product->getId()) { throw new InputException(__('Provided downloadable link is not related to given product.')); } if (!$this->contentValidator->isValid($linkContent)) { throw new InputException(__('Provided link information is invalid.')); } if ($isGlobalScopeContent) { $product->setStoreId(0); } $title = $linkContent->getTitle(); if (empty($title)) { if ($isGlobalScopeContent) { throw new InputException(__('Link title cannot be empty.')); } // use title from GLOBAL scope $link->setTitle(null); } else { $link->setTitle($linkContent->getTitle()); } $link->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId())->setProductWebsiteIds($product->getWebsiteIds())->setSortOrder($linkContent->getSortOrder())->setPrice($linkContent->getPrice())->setIsShareable($linkContent->isShareable())->setNumberOfDownloads($linkContent->getNumberOfDownloads())->save(); return $link->getId(); } else { $product = $this->productRepository->get($sku, true); if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { throw new InputException(__('Product type of the product must be \'downloadable\'.')); } if (!$this->contentValidator->isValid($linkContent)) { throw new InputException(__('Provided link information is invalid.')); } if (!in_array($linkContent->getLinkType(), ['url', 'file'])) { throw new InputException(__('Invalid link type.')); } $title = $linkContent->getTitle(); if (empty($title)) { throw new InputException(__('Link title cannot be empty.')); } $linkData = ['link_id' => 0, 'is_delete' => 0, 'type' => $linkContent->getLinkType(), 'sort_order' => $linkContent->getSortOrder(), 'title' => $linkContent->getTitle(), 'price' => $linkContent->getPrice(), 'number_of_downloads' => $linkContent->getNumberOfDownloads(), 'is_shareable' => $linkContent->isShareable()]; if ($linkContent->getLinkType() == 'file') { $linkData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($linkContent->getLinkFile(), 'link_file')]); } else { $linkData['link_url'] = $linkContent->getLinkUrl(); } if ($linkContent->getSampleType() == 'file') { $linkData['sample']['type'] = 'file'; $linkData['sample']['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($linkContent->getSampleFile(), 'link_sample_file')]); } elseif ($linkContent->getSampleType() == 'url') { $linkData['sample']['type'] = 'url'; $linkData['sample']['url'] = $linkContent->getSampleUrl(); } $downloadableData = ['link' => [$linkData]]; $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $product->save(); return $product->getLastAddedLinkId(); } }