Reading list Switch to dark mode

    How to add custom options of product in Magento 2

    Updated 23 February 2024

    Here we learn, how to add custom options of product in Magento 2.

    Note: We also have a separate module (Magento 2 Dependent Custom Options) that gives the ability to admin to create dependent custom options for the products. These options will be presented on the product page and will be dependent on one another. For more information, you can check it on our store.

    I have created Drop-down Type custom option here.

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productId = 12490;
    $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
    $productOptions = [
        [
            "is_delete" => '',
            "sort_order" => 1,
            "title" => "Size",
            "type" => "drop_down",
            "is_require" => 0,
            "values" => [
                [
                    "sort_order" => 0,
                    "is_delete" => '',
                    "title" => 'M',
                    "price" => 5,
                    "price_type" => 'fixed',
                    "sku" => ''
                ],
                [
                    "sort_order" => 1,
                    "is_delete" => '',
                    "title" => 'L',
                    "price" => 5,
                    "price_type" => 'fixed',
                    "sku" => ''
                ]
            ]
        ]
    ];
    /**
     * Initialize product options
     */
    if ($productOptions && !$product->getOptionsReadonly()) {
    // mark custom options that should to fall back to default value
        $customOptions = [];
        foreach ($productOptions as $customOptionData) {
            if (empty($customOptionData['is_delete'])) {
                if (isset($customOptionData['values'])) {
                    $customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
                        return empty($valueData['is_delete']);
                    });
                }
                $customOptionFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
                    'Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory'
                );
                $customOption = $customOptionFactory->create(['data' => $customOptionData]);
                $customOption->setProductSku($product->getSku());
                $customOption->setOptionId(null);
                $customOptions[] = $customOption;
            }
        }
        $product->setOptions($customOptions);
    }
    $product->save();

    That’s all.

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • RAJNIKANT
    • Mahesh Singh (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home