How to configure Magento 2 custom options?

Custom options are a power feature included with Magento 2. Custom Magento 2 product options allow merchants to customize elements, enabling customers to select preferences ranging from personalization text fields to dropdown menus for sizing and color options. Other custom options include the Magento 2 Dependent Custom Options extension. This feature helps improve product customization by making product custom options dependent on each other.

Magento customizable options typically include four types of data:

  1. Text: A text box on a product page allowing customers to enter text for personalization or request product variations.
  2. File: This option allows customers to upload a file from their device—for example, a custom photo to use in product personalization.
  3. Select: This customization option allows businesses to create a list of options. Customers select their desired choices from the options list, typically in a dropdown menu.
  4. Date: This feature lets customers choose the date, time, or both date and time for product delivery.

5 steps to configure custom options

  • Step 1: Choose the product to add or configure customizable options by navigating to the catalog, then products.
  • Step 2: Scroll to the customizable options section, click the add option button, and choose the desired custom option for your product.
  • Step 3: Set option values—title, price, and price type.
  • Step 4: Enter a SKU value for each customizable option.
  • Step 5: Double-check your choices and save

How to configure custom options for Magento 2 programmatically?

Here is a sample code that imports magento 2 product custom options from an array. The code includes the basic and required fields. Additional fields can be added as needed, making it versatile for importing various types of custom options.  

Sample array data:

$datas = array(
        array(
            "sku" => "951391-1-1",
            "attributes" => '[{"title":"Add \"I\'m a pro\" on the back title", "type":"checkbox", "is_require":"0", "sort_order":"1", "values":[{"title":"Add \"I\'m a pro\" on the back", "price":"2.000000", "price_type":"fixed", "sku":null, "sort_order":"1"}, {"title":"Add my name", "price":"6.000000", "price_type":"fixed", "sku":null, "sort_order":"2"}]}, {"title":"Add my name title", "type":"field", "is_require":"0", "sort_order":"2", "price":"0.000000", "price_type":"fixed"}, {"title":"Test area title", "type":"area", "is_require":"0", "sort_order":"3", "price":"16.000000", "price_type":"fixed"}, {"title":"Test file title", "type":"file", "is_require":"0", "sort_order":"5", "price":"4.000000", "price_type":"fixed"}, {"title":"Test dropdown title", "type":"drop_down", "is_require":"0", "sort_order":"6", "values":[{"title":"test dropdown 1", "price":"7.000000", "price_type":"fixed", "sku":null, "sort_order":"1"}, {"title":"test dropdown 2", "price":"8.000000", "price_type":"fixed", "sku":null, "sort_order":"2"}]}, {"title":"Test radio title", "type":"radio", "is_require":"0", "sort_order":"7", "values":[{"title":"Test radio1", "price":"9.000000", "price_type":"fixed", "sku":null, "sort_order":"1"}, {"title":"Test radio2", "price":"10.000000", "price_type":"fixed", "sku":null, "sort_order":"2"}]}, {"title":"Test multiselect title", "type":"multiple", "is_require":"0", "sort_order":"8", "values":[{"title":"Test m-select1", "price":"11.000000", "price_type":"fixed", "sku":null, "sort_order":"1"}, {"title":"Test m-select2", "price":"12.000000", "price_type":"fixed", "sku":null, "sort_order":"2"}]}, {"title":"test date title", "type":"date", "is_require":"0", "sort_order":"9", "price":"13.000000", "price_type":"fixed"}, {"title":"Test data-time title", "type":"date_time", "is_require":"0", "sort_order":"10", "price":"14.000000", "price_type":"fixed"}, {"title":"Test time title", "type":"time", "is_require":"0", "sort_order":"11", "price":"15.000000", "price_type":"fixed"}]'
        )
    );

Code:

$options = array();
    foreach ($datas as $data) {
        $_product = $this->_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->get($data['sku'], true);
        $options = json_decode($data['attributes'], true);
        foreach ($options as $arrayOption) {
            $isRequire = $arrayOption['is_require'] == 0 ? true : false;
            $customOption = $this->_objectManager->create('Magento\Catalog\Api\Data\ProductCustomOptionInterface');
            $customOption->setTitle($arrayOption['title'])
                    ->setType($arrayOption['type'])
                    ->setIsRequire($isRequire)
                    ->setSortOrder($arrayOption['sort_order']);
            if (!in_array($arrayOption['type'], array('drop_down', 'checkbox', 'radio', 'multiple'))) {
                $customOption->setPrice($arrayOption['price'])
                        ->setPriceType($arrayOption['price_type']);
            } else {
                $customOption->setValues($arrayOption['values']);
            }
            $customOption->setProductSku($_product->getSku());
            $customOptions[] = $customOption;
            $_product->setOptions($customOptions)->save();
        }
    }

Magento 2 enables you to add custom options to your products. Alternatively, you have the option to enhance this functionality by leveraging extensions like Product Option Template.

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.