|
| 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 | +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 9 | + |
| 10 | +/** @var $product \Magento\Catalog\Model\Product */ |
| 11 | +$product = $objectManager->create(\Magento\Catalog\Model\Product::class); |
| 12 | + |
| 13 | +$product->setTypeId('simple') |
| 14 | + ->setAttributeSetId($product->getDefaultAttributeSetId()) |
| 15 | + ->setWebsiteIds([1]) |
| 16 | + ->setName('Simple Product With File Custom Option') |
| 17 | + ->setSku('simple-with-file-option') |
| 18 | + ->setPrice(10) |
| 19 | + ->setMetaTitle('meta title') |
| 20 | + ->setMetaKeyword('meta keyword') |
| 21 | + ->setMetaDescription('meta description') |
| 22 | + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) |
| 23 | + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) |
| 24 | + ->setCanSaveCustomOptions(true) |
| 25 | + ->setStockData( |
| 26 | + [ |
| 27 | + 'qty' => 100, |
| 28 | + 'is_in_stock' => 1, |
| 29 | + ] |
| 30 | + ) |
| 31 | + ->setHasOptions(true); |
| 32 | + |
| 33 | +$option = [ |
| 34 | + 'title' => 'file option', |
| 35 | + 'type' => 'file', |
| 36 | + 'is_require' => true, |
| 37 | + 'sort_order' => 3, |
| 38 | + 'price' => 30.0, |
| 39 | + 'price_type' => 'fixed', |
| 40 | + 'sku' => 'file option sku', |
| 41 | + 'file_extension' => 'jpg,png,gif', |
| 42 | + 'image_size_x' => 2000, |
| 43 | + 'image_size_y' => 2000, |
| 44 | +]; |
| 45 | + |
| 46 | +$customOptions = []; |
| 47 | + |
| 48 | +/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory $customOptionFactory */ |
| 49 | +$customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class); |
| 50 | + |
| 51 | +/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface $customOption */ |
| 52 | +$customOption = $customOptionFactory->create(['data' => $option]); |
| 53 | +$customOption->setProductSku($product->getSku()); |
| 54 | +$customOptions[] = $customOption; |
| 55 | + |
| 56 | +$product->setOptions($customOptions); |
| 57 | + |
| 58 | +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryFactory */ |
| 59 | +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 60 | +$productRepository->save($product); |
0 commit comments