I am inserting custom options programmatically for multiple products from some template where I add options and assign them to products But when I edit those options and assigned it to more products then it double added for previously assigned products. How can I check if that custom option is already assigned or exists for any product so it will only edit for that product.

I can't add whole code here and yes, I am referring below link but my options are dynamic. https://webkul.com/blog/create-custom-option-programmatically-in-magento2/

1 Answer 1


Based in the code of webkul.com you have these options:

Load Product

$product = $_objectManager->get('\Magento\Catalog\Model\Product')->load($id);

Custom Options

$customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);

Configurable Options

$productTypeInstance = $_objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable');
         $productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);

The last step is check based in Custom or Configurable Options if your option is created in product before and keep it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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