How to Create Custom Options Programmatically in Magento 2
Magento 2 is a popular CMS used widely used in E-commerce stores selling varied products. For some of the products, customers require to specify custom options while placing the order.
For example, an online food delivery store must have options to allow customers to customize their pizza toppings
The custom options allow customers to configure the product as per the need. To create custom options programmatically in Magento 2, one needs to follow the below method.
Method to Create Custom Options Programmatically in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of Object manager $productId = 50; $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId); $values = array( array( 'title'=>'Red', 'price'=>10, 'price_type'=>"fixed", 'sku'=>"Red1", 'sort_order'=>1, 'is_delete'=>0, 'option_type_id'=>-1, ), array( 'title'=>'White', 'price'=>10, 'price_type'=>"fixed", 'sku'=>"White1", 'sort_order'=>1, 'is_delete'=>0, 'option_type_id'=>-1, ), array( 'title'=>'Black', 'price'=>10, 'price_type'=>"fixed", 'sku'=>"black1", 'sort_order'=>1, 'is_delete'=>0, 'option_type_id'=>-1, ) ); $options = array( array( "sort_order" => 1, "title" => "Field Option", "price_type" => "fixed", "price" => "", "type" => "field", "is_require" => 0 ), array( "sort_order" => 2, "title" => "Color", "price_type" => "fixed", "price" => "", "type" => "drop_down", "is_require" => 0, "values" => $values ), array( "sort_order" => 2, "title" => "Multiple Option", "price_type" => "fixed", "price" => "", "type" => "multiple", "values" => $values, "is_require" => 0 ) ); foreach ($options as $arrayOption) { $product->setHasOptions(1); $product->getResource()->save($product); $option = $objectManager->create('\Magento\Catalog\Model\Product\Option') ->setProductId($productId) ->setStoreId($product->getStoreId()) ->addData($arrayOption); $option->save(); $product->addOption($option); } |
$values is an array to hold the values for the select type custom option, and,
$options is an array to hold multiple custom options
When you execute the above code, the custom options of types: field, drop down and multi-select will be created in your product.
Comment us to help you out if you face any issue while creating custom options programmatically!
Please rate the post with 5 stars if it helped you create advanced customization in your products
Happy Coding
Also read:
- How to Get Product Custom Options Value From Cart and Order in Magento 2
- SQL query to get all products with custom options in Magento 2.
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
Prev
How to Add Tracking Number to the Current Order Shipment in Magento 2
How to Get Product Options in Magento 2
Next