11 use Magento\Downloadable\Api\Data\SampleInterfaceFactory;
68 private $sampleTypeHandler;
73 private $metadataPool;
108 $product = $this->productRepository->get($sku);
120 $sample = $this->sampleDataObjectFactory->create();
134 $dataObject->setId($resourceData->getId());
135 $storeTitle = $resourceData->getStoreTitle();
136 $title = $resourceData->getTitle();
137 if (!empty($storeTitle)) {
138 $dataObject->setTitle($storeTitle);
140 $dataObject->setTitle(
$title);
142 $dataObject->setSortOrder($resourceData->getSortOrder());
143 $dataObject->setSampleType($resourceData->getSampleType());
144 $dataObject->setSampleFile($resourceData->getSampleFile());
145 $dataObject->setSampleUrl($resourceData->getSampleUrl());
178 $isGlobalScopeContent =
true 180 $product = $this->productRepository->get($sku,
true);
182 $sampleId = $sample->
getId();
184 return $this->updateSample(
$product, $sample, $isGlobalScopeContent);
188 __(
'The product needs to be the downloadable type. Verify the product and try again.')
192 if (!$this->contentValidator->isValid($sample, $validateSampleContent)) {
194 __(
'The sample information is invalid. Verify the information and try again.')
198 if (!in_array($sample->
getSampleType(), [
'url',
'file'],
true)) {
199 throw new InputException(
__(
'The sample type is invalid. Verify the sample type and try again.'));
204 throw new InputException(
__(
'The sample title is empty. Enter the title and try again.'));
207 return $this->
saveSample($product, $sample, $isGlobalScopeContent);
220 $isGlobalScopeContent
223 'sample_id' => (int)$sample->
getId(),
231 $sampleData[
'file'] = $this->jsonEncoder->encode(
240 $sampleData[
'file'] = $this->jsonEncoder->encode(
252 if ($isGlobalScopeContent) {
256 return $product->getLastAddedSampleId();
269 protected function updateSample(
272 $isGlobalScopeContent
274 $sampleId = $sample->
getId();
276 $existingSample = $this->sampleFactory->create()->load($sampleId);
278 if (!$existingSample->getId()) {
280 __(
'No downloadable sample with the provided ID was found. Verify the ID and try again.')
283 $linkFieldValue =
$product->getData(
284 $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()
286 if ($existingSample->getProductId() != $linkFieldValue) {
287 throw new InputException(
288 __(
"The downloadable sample isn't related to the product. Verify the link and try again.")
293 if (!$this->contentValidator->isValid($sample, $validateFileContent)) {
294 throw new InputException(
__(
'The sample information is invalid. Verify the information and try again.'));
296 if ($isGlobalScopeContent) {
302 if ($isGlobalScopeContent) {
303 throw new InputException(
__(
'The sample title is empty. Enter the title and try again.'));
306 $existingSample->setTitle(
null);
308 $existingSample->setTitle($sample->
getTitle());
314 $this->
saveSample($product, $sample, $isGlobalScopeContent);
315 return $existingSample->getId();
321 public function delete(
$id)
324 $sample = $this->sampleFactory->create()->load(
$id);
325 if (!$sample->
getId()) {
326 throw new NoSuchEntityException(
327 __(
'No downloadable sample with the provided ID was found. Verify the ID and try again.')
332 }
catch (\Exception $exception) {
333 throw new StateException(
__(
'The sample with "%1" ID can\'t be deleted.', $sample->
getId()), $exception);
344 private function getMetadataPool()
346 if (!$this->metadataPool) {
350 return $this->metadataPool;
359 private function getSampleTypeHandler()
361 if (!$this->sampleTypeHandler) {
365 return $this->sampleTypeHandler;
buildSample($resourceData)
getSamplesByProduct(\Magento\Catalog\Api\Data\ProductInterface $product)
elseif(isset( $params[ 'redirect_parent']))
setSampleFile($sampleFile)
__construct(ProductRepositoryInterface $productRepository, Type $downloadableType, SampleInterfaceFactory $sampleDataObjectFactory, ContentValidator $contentValidator, ContentUploaderInterface $fileContentUploader, EncoderInterface $jsonEncoder, SampleFactory $sampleFactory)
saveSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent)
setBasicFields($resourceData, $dataObject)
save( $sku, SampleInterface $sample, $isGlobalScopeContent=true)