|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +use Magento\Catalog\Api\Data\ProductInterfaceFactory; |
| 8 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 9 | +use Magento\Catalog\Model\Product; |
| 10 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 11 | +use Magento\Catalog\Model\Product\Type; |
| 12 | +use Magento\Catalog\Model\Product\Visibility; |
| 13 | +use Magento\Eav\Model\Config; |
| 14 | +use Magento\Store\Api\WebsiteRepositoryInterface; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\Catalog\Api\Data\ProductLinkInterface; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | + |
| 19 | +$objectManager = Bootstrap::getObjectManager(); |
| 20 | +/** @var WebsiteRepositoryInterface $websiteRepository */ |
| 21 | +$websiteRepository = $objectManager->get(WebsiteRepositoryInterface::class); |
| 22 | +$baseWebsite = $websiteRepository->get('base'); |
| 23 | +/** @var StoreManagerInterface $storeManager */ |
| 24 | +$storeManager = $objectManager->get(StoreManagerInterface::class); |
| 25 | +$defaultAttributeSet = $objectManager->get(Config::class)->getEntityType(Product::ENTITY)->getDefaultAttributeSetId(); |
| 26 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 27 | +/** @var ProductInterfaceFactory $productInterfaceFactory */ |
| 28 | +$productInterfaceFactory = $objectManager->get(ProductInterfaceFactory::class); |
| 29 | + |
| 30 | +/** @var Product $product */ |
| 31 | +$product = $productInterfaceFactory->create(); |
| 32 | +$product->setTypeId(Type::TYPE_SIMPLE) |
| 33 | + ->setAttributeSetId($defaultAttributeSet) |
| 34 | + ->setStoreId($storeManager->getDefaultStoreView()->getId()) |
| 35 | + ->setWebsiteIds([$baseWebsite->getId()]) |
| 36 | + ->setName('Simple Product') |
| 37 | + ->setSku('simple') |
| 38 | + ->setPrice(10) |
| 39 | + ->setWeight(18) |
| 40 | + ->setStockData(['use_config_manage_stock' => 0]) |
| 41 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 42 | + ->setStatus(Status::STATUS_ENABLED); |
| 43 | + |
| 44 | +$simple = $productRepository->save($product); |
| 45 | +$simple->setStoreId($storeManager->getDefaultStoreView()->getId()) |
| 46 | +->setStatus(Status::STATUS_DISABLED); |
| 47 | +$productRepository->save($simple); |
| 48 | +/** @var ProductLinkInterface $productLink */ |
| 49 | +$productLink = $objectManager->create(ProductLinkInterface::class); |
| 50 | +$productLink->setSku('simple_with_related'); |
| 51 | +$productLink->setLinkedProductSku('simple'); |
| 52 | +$productLink->setPosition(1); |
| 53 | +$productLink->setLinkType('related'); |
| 54 | + |
| 55 | +/** @var Product $product */ |
| 56 | +$product = $productInterfaceFactory->create(); |
| 57 | +$product->setTypeId(Type::TYPE_SIMPLE) |
| 58 | + ->setAttributeSetId($defaultAttributeSet) |
| 59 | + ->setStoreId($storeManager->getDefaultStoreView()->getId()) |
| 60 | + ->setWebsiteIds([$baseWebsite->getId()]) |
| 61 | + ->setName('Simple Product With Related Product') |
| 62 | + ->setSku('simple_with_related') |
| 63 | + ->setPrice(10) |
| 64 | + ->setWeight(18) |
| 65 | + ->setProductLinks([$productLink]) |
| 66 | + ->setStockData(['use_config_manage_stock' => 0]) |
| 67 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 68 | + ->setStatus(Status::STATUS_ENABLED); |
| 69 | + |
| 70 | +$productRepository->save($product); |
0 commit comments