Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    magento-hyperlocal-marketplace-guide

    Create custom option programmatically in magento2

    To create custom options in a product programmatically you need to use following code:


    Here,

    $values : is an array holds the values for the select type custom option.

    $options: is an array holds multiple custom options.

    Once this code is executed, the custom options of types: field, drop down and multi select are created in your product.

    Screenshot:

    . . .

    Comments (5)

    Add Your Comment

  • Hardik Makwana
    Without objectmanager how i am create the customizable option programmatically
    • ashutosh srivastava
      without object manager, you need to use the constructor dependency injection for creating the object like this:
      public function __construct(\Magento\Catalog\Model\ProductFactory $productFactory ) {
      $this->_product = $productFactory->create();
      }
  • Gaurav Jain
    Where is this code to be added??
    I want to have custom option applied to all products.
  • Matt Bidas
    Method setHasOptions doesn’t exists in class Product.php on Magento 2.1.1
  • Lucas
    Nice but in which file I should put this code?
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close