/** * {@inheritdoc} */ public function create($productSku, DownloadableSampleContent $sampleContent, $isGlobalScopeContent = false) { $product = $this->productRepository->get($productSku, 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($sampleContent)) { throw new InputException('Provided sample information is invalid.'); } if (!in_array($sampleContent->getSampleType(), array('url', 'file'))) { throw new InputException('Invalid sample type.'); } $title = $sampleContent->getTitle(); if (empty($title)) { throw new InputException('Sample title cannot be empty.'); } $sampleData = array('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(array($this->fileContentUploader->upload($sampleContent->getSampleFile(), 'sample'))); } else { $sampleData['sample_url'] = $sampleContent->getSampleUrl(); } $downloadableData = array('sample' => array($sampleData)); $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $product->save(); return $product->getLastAddedSampleId(); }
/** * {@inheritdoc} */ public function create($productSku, DownloadableLinkContent $linkContent, $isGlobalScopeContent = false) { $product = $this->productRepository->get($productSku, true); if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { throw new InputException('Product type of the product must be \'downloadable\'.'); } if (!$this->linkContentValidator->isValid($linkContent)) { throw new InputException('Provided link information is invalid.'); } if (!in_array($linkContent->getLinkType(), array('url', 'file'))) { throw new InputException('Invalid link type.'); } $title = $linkContent->getTitle(); if (empty($title)) { throw new InputException('Link title cannot be empty.'); } $linkData = array('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(array($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(array($this->fileContentUploader->upload($linkContent->getSampleFile(), 'link_sample_file'))); } elseif ($linkContent->getSampleType() == 'url') { $linkData['sample']['type'] = 'url'; $linkData['sample']['url'] = $linkContent->getSampleUrl(); } $downloadableData = array('link' => array($linkData)); $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $product->save(); return $product->getLastAddedLinkId(); }