I have added the custom option programatically following this . Not everything worked but I made few changes and successfully added the custom option programatically. But I couldn't get that custom option displayed on frontend. On further checking I figure out there is a problem while setting the hasOptions().
I tried below codes to set has options
$_products = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$_products->setHasOptions(1);
$_products->save();
Also tried this
$_products = $this->_objectManager->create('\Magento\Catalog\Model\Product')->load($productId);
$_products->setHasOptions(1);
$_products->getResource()->save();
But I couldn't get it working.
Can anyone help.