Magento 2 – create customizable options programmatically
In this tutorial I show you how to create customizable options programmatically in Magento 2. We need to do this inside our product import module. It is quite simple to add customizable options to products with different options.
Magento 2 – create customizable options programmatically
Customizable options are used to sell additional attributes for a specific product. You can add for example gift wrapping, accessories or special shipping options. All this things can be optional or required. It is also possible to offer a free text input for your customer. The usage of customizable options depends on your individual product an can be anything you want.
Types
You can use one of the following types:
- textfield (area)
for a free text input - checkbox (checkbox)
If you have many options that can be added individually. If you make this options reqired, the user has to add one. - radio button (radio)
If you have many options, but the use can only choose one. If you make this options required, the user has to add one.
Code example
Here is a small code example of how to add customizable options programmatically to a product:
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 | $valuesArray = array(); //add as many options you want, here we add two $valuesArray[] = array( "title" => "Option1", "price_type" => "fixed", "price" => 9.99, "sku" => $product->getSku(), "sort_order" =>; 1, ); $valuesArray[] = array( "title" => "Option2", "price_type" => "fixed", "price" => 1.99, "sku" => $product->getSku(), "sort_order" => 2, ); //now we create options and set type $options[] = array( "sort_order" => 1, "title" => "Options", "type" => "radio", "is_require" => 1, "values" => $valuesArray, ); $product->unsetOptions(); $product->setHasOptions(1); $product->setCanSaveCustomOptions(true); $product->getResource()->save($product); $product->setOptions(array()); foreach ($options as $arrayOption) { $option = $this->_objectManager->create('\Magento\Catalog\Model\Product\Option') ->setProductId($product->getId()) ->setStoreId($product->getStoreId()) ->addData($arrayOption); $option->save(); $options[] = $option; $product->addOption($option); } |
First we have to build an array of options. There you can set title, price_type, price, sku and sort_order. Sort_order is needed to display all options in correct order. Sku is needed for connection to a product. I get sku directly from a product object, this object is later needed for assigning options to it. Defining options is quite easy, you can add as many as you want to this array. Next we create out options array. A product can have many options where each option has a group of options (like for example radio button groups with different number of radio buttons). For each option group you can define type and if one of them is required. You also have to add a sort_order and a title. Values is the previous generated array of sub options.
After creating out options array, we have to assign it to a product. For this we have to create a option object based on our option array. This can be done with \Magento\Catalog\Model\Product\Option object. Now you have to add this object to your product. You can do it with addOption or setOption. If you do it in a loop, addOption is your choice.
If you create options within a product import, it is a good idea to delete old options if the product already exists. This can be done with unsetOptions() and setOptions(array()). If this does not work, you will add options with each import and your option list gets bigger and bigger.
You can find defined options in you Magento 2 backend at product detail site:
Customizable option in database
Your configures options are stored in database inside catalog_product_option tables. If you encounter any problems this is a good source to debug and find more information for a specific product.
Conclusion
It is really simple to add customizable options programmatically to Magento 2 products. With options you can advance customization of your product. Think about a laptop that can be configured by options for CPU, RAM, hard disk and so on. A customer has full control an can configure his product.
Looks good to us, thank you!
We have imported our products from our old shop system to Magento 2 and we are facing an issue with customizable options. As the major snippet for programmatically editing options already exists her, it might be an interesting job to create a module that basically runs through all our products, looks for specific customizable options and then changes the options of these articles in the database. Finally, an update of the phtml for product view would be required (showing some attributes only when the first attribute is selected).
We would be happy to hear a proposal from somebody đŸ™‚
It would deal with
Thank you for your request, I will send contact for more details.
Hi,
where to save the file? I can’t find the filename/path of the script.
you can create a simple script for it and place it in your Magento root (fast and simple solution), or better: create a new module and put it into a class.
I got error
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
catalog_product_option_type_price, CONSTRAINT
FK_CF381633CBDBD1124AC83B128A5B72A8FOREIGN KEY (
option_type_id) REFERENCES
when use below code in magento 2.2.6Hey, that indicates that there is a data error in your database. Does it also happen for a fresh installed database?
Hello,
Product Option was created successfully via ‘catalog_product_save_after’ event but into the frontend the product options is not appearing
Have you tried reindexing?
Yes I have tried but it is take a too much time to display into the server