|
| 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\ProductTierPriceExtensionFactory; |
| 9 | +use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory; |
| 10 | +use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Model\Product; |
| 13 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 14 | +use Magento\Catalog\Model\Product\Type; |
| 15 | +use Magento\Catalog\Model\Product\Visibility; |
| 16 | +use Magento\Store\Api\WebsiteRepositoryInterface; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | + |
| 19 | +/** @var \Magento\TestFramework\ObjectManager $objectManager */ |
| 20 | +$objectManager = Bootstrap::getObjectManager(); |
| 21 | + |
| 22 | +/** @var ProductTierPriceInterfaceFactory $tierPriceFactory */ |
| 23 | +$tierPriceFactory = $objectManager->get(ProductTierPriceInterfaceFactory::class); |
| 24 | +/** @var $tpExtensionAttributes */ |
| 25 | +$tpExtensionAttributesFactory = $objectManager->get(ProductTierPriceExtensionFactory::class); |
| 26 | +/** @var $productExtensionAttributes */ |
| 27 | +$productExtensionAttributesFactory = $objectManager->get(ProductExtensionInterfaceFactory::class); |
| 28 | + |
| 29 | +$adminWebsite = $objectManager->get(WebsiteRepositoryInterface::class)->get('admin'); |
| 30 | +$tierPriceExtensionAttributes1 = $tpExtensionAttributesFactory->create() |
| 31 | + ->setWebsiteId($adminWebsite->getId()); |
| 32 | +$productExtensionAttributesWebsiteIds = $productExtensionAttributesFactory->create( |
| 33 | + ['website_ids' => $adminWebsite->getId()] |
| 34 | +); |
| 35 | + |
| 36 | +$tierPrice = $tierPriceFactory->create( |
| 37 | + [ |
| 38 | + 'data' => [ |
| 39 | + 'customer_group_id' => 2, |
| 40 | + 'qty' => 1, |
| 41 | + 'value' => 5, |
| 42 | + ], |
| 43 | + ] |
| 44 | +)->setExtensionAttributes($tierPriceExtensionAttributes1); |
| 45 | + |
| 46 | +/** @var $product Product */ |
| 47 | +$product = $objectManager->create(Product::class); |
| 48 | +$product->isObjectNew(true); |
| 49 | +$product->setTypeId(Type::TYPE_SIMPLE) |
| 50 | + ->setAttributeSetId($product->getDefaultAttributeSetId()) |
| 51 | + ->setWebsiteIds([$adminWebsite->getId()]) |
| 52 | + ->setName('Simple Product') |
| 53 | + ->setSku('simple') |
| 54 | + ->setPrice(10) |
| 55 | + ->setWeight(1) |
| 56 | + ->setShortDescription("Short description") |
| 57 | + ->setTaxClassId(0) |
| 58 | + ->setTierPrices([$tierPrice]) |
| 59 | + ->setDescription('Description with <b>html tag</b>') |
| 60 | + ->setExtensionAttributes($productExtensionAttributesWebsiteIds) |
| 61 | + ->setMetaTitle('meta title') |
| 62 | + ->setMetaKeyword('meta keyword') |
| 63 | + ->setMetaDescription('meta description') |
| 64 | + ->setVisibility(Visibility::VISIBILITY_BOTH) |
| 65 | + ->setStatus(Status::STATUS_ENABLED) |
| 66 | + ->setStockData( |
| 67 | + [ |
| 68 | + 'use_config_manage_stock' => 1, |
| 69 | + 'qty' => 100, |
| 70 | + 'is_qty_decimal' => 0, |
| 71 | + 'is_in_stock' => 1, |
| 72 | + ] |
| 73 | + )->setCanSaveCustomOptions(true) |
| 74 | + ->setHasOptions(true); |
| 75 | + |
| 76 | +/** @var ProductRepositoryInterface $productRepository */ |
| 77 | +$productRepository = $objectManager->create(ProductRepositoryInterface::class); |
| 78 | +$productRepository->save($product); |
0 commit comments