|
| 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\Data\ProductInterfaceFactory; |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Helper\DefaultCategory; |
| 12 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 13 | +use Magento\Catalog\Model\Product\Type; |
| 14 | +use Magento\Catalog\Model\Product\Visibility; |
| 15 | +use Magento\Store\Api\WebsiteRepositoryInterface; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | + |
| 18 | +$objectManager = Bootstrap::getObjectManager(); |
| 19 | +/** @var WebsiteRepositoryInterface $websiteRepository */ |
| 20 | +$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class); |
| 21 | +$defaultWebsiteId = $websiteRepository->get('base')->getId(); |
| 22 | +/** @var DefaultCategory $defaultCategory */ |
| 23 | +$defaultCategory = $objectManager->get(DefaultCategory::class); |
| 24 | +/** @var ProductRepositoryInterface $productRepository */ |
| 25 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 26 | +/** @var ProductInterfaceFactory $productFactory */ |
| 27 | +$productFactory = $objectManager->get(ProductInterfaceFactory::class); |
| 28 | +$product = $productFactory->create(); |
| 29 | +$productData = [ |
| 30 | + ProductInterface::TYPE_ID => Type::TYPE_SIMPLE, |
| 31 | + ProductInterface::ATTRIBUTE_SET_ID => $product->getDefaultAttributeSetId(), |
| 32 | + ProductInterface::SKU => 'product_disabled', |
| 33 | + ProductInterface::NAME => 'Product with category', |
| 34 | + ProductInterface::PRICE => 10, |
| 35 | + ProductInterface::VISIBILITY => Visibility::VISIBILITY_BOTH, |
| 36 | + ProductInterface::STATUS => Status::STATUS_DISABLED, |
| 37 | + 'website_ids' => [$defaultWebsiteId], |
| 38 | + 'stock_data' => [ |
| 39 | + 'use_config_manage_stock' => 1, |
| 40 | + 'qty' => 100, |
| 41 | + 'is_qty_decimal' => 0, |
| 42 | + 'is_in_stock' => 1, |
| 43 | + ], |
| 44 | + 'category_ids' => [$defaultCategory->getId()], |
| 45 | +]; |
| 46 | +$product->setData($productData); |
| 47 | + |
| 48 | +$productRepository->save($product); |
0 commit comments