I tried to add custom option to a product into an observer on catalog_product_save_after event.

I am already successful add custom option for the simple product with this script:

class Productsaveafter implements ObserverInterface
    protected $ObjectManager;
    protected $product;

    public function __construct(
         \Magento\Framework\ObjectManagerInterface $ObjectManager,
         \Magento\Catalog\Model\Product $product

        $this->_objectManager = $ObjectManager;
        $this->_product = $product;

    public function execute(
        \Magento\Framework\Event\Observer $observer

         $_product = $observer->getProduct();  
         $productId = $_product->getId();
         $personalisatie = $_product->getData('personalisatie_toevoegen');

         if(!$_product->getHasOptions() && $personalisatie==1 ){

                $customOption = $this->_objectManager->create('Magento\Catalog\Api\Data\ProductCustomOptionInterface');
                $var = NULL;

                $customOptions[] = $customOption;   


            } catch (\Exception $e) {




But for the configurable product is not working at all.

What i see is that !$_product->getHasOptions() is not true. so the code is not executed for configurable product.

If I remove !$_product->getHasOptions() form if statement when i press save of the produc my backend page is loading for few minutes then and page is blocked and the custom options is not added.

Is there another way to add custom option for the configurable products?


1 Answer 1


You need to use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface::save method to save custom options.





where optionRepository is Magento\Catalog\Api\ProductCustomOptionRepositoryInterface instance


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.