|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 9 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 10 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 11 | +use Magento\Catalog\Model\Product\Visibility; |
| 12 | +use Magento\Downloadable\Api\Data\LinkInterface; |
| 13 | +use Magento\Downloadable\Api\LinkRepositoryInterface; |
| 14 | +use Magento\Downloadable\Helper\Download; |
| 15 | +use Magento\Downloadable\Model\Link; |
| 16 | +use Magento\Downloadable\Model\Product\Type; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | + |
| 19 | +/** @var ProductRepositoryInterface $productRepository */ |
| 20 | +$productRepository = Bootstrap::getObjectManager() |
| 21 | + ->get(ProductRepositoryInterface::class); |
| 22 | +/** @var LinkRepositoryInterface $linkRepository */ |
| 23 | +$linkRepository = Bootstrap::getObjectManager() |
| 24 | + ->create(LinkRepositoryInterface::class); |
| 25 | +/** @var ProductInterface $product */ |
| 26 | +$product = Bootstrap::getObjectManager() |
| 27 | + ->create(ProductInterface::class); |
| 28 | +/** @var LinkInterface $downloadableProductLink */ |
| 29 | +$downloadableProductLink = Bootstrap::getObjectManager() |
| 30 | + ->create(LinkInterface::class); |
| 31 | + |
| 32 | +$downloadableProductLink |
| 33 | +// ->setId(null) |
| 34 | + ->setLinkType(Download::LINK_TYPE_URL) |
| 35 | + ->setTitle('Downloadable Product Link') |
| 36 | + ->setIsShareable(Link::LINK_SHAREABLE_CONFIG) |
| 37 | + ->setLinkUrl('http://example.com/downloadable.txt') |
| 38 | + ->setNumberOfDownloads(100) |
| 39 | + ->setSortOrder(1) |
| 40 | + ->setPrice(0); |
| 41 | + |
| 42 | +$downloadableProductLinks[] = $downloadableProductLink; |
| 43 | + |
| 44 | +$product |
| 45 | + ->setId(1) |
| 46 | + ->setTypeId(Type::TYPE_DOWNLOADABLE) |
| 47 | + ->setExtensionAttributes( |
| 48 | + $product->getExtensionAttributes() |
| 49 | + ->setDownloadableProductLinks($downloadableProductLinks) |
| 50 | + ) |
| 51 | + ->setSku('downloadable-product') |
| 52 | + ->setAttributeSetId(4) |
| 53 | + ->setWebsiteIds([1]) |
| 54 | + ->setName('Downloadable Product Limited') |
| 55 | + ->setPrice(10) |
| 56 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 57 | + ->setStatus(Status::STATUS_ENABLED) |
| 58 | + ->setLinksPurchasedSeparately(true) |
| 59 | + ->setStockData( |
| 60 | + [ |
| 61 | + 'qty' => 100, |
| 62 | + 'is_in_stock' => 1, |
| 63 | + 'manage_stock' => 1, |
| 64 | + ] |
| 65 | + ); |
| 66 | + |
| 67 | +$productRepository->save($product); |
0 commit comments