Magento – Magento 2: How to check If custom options exists for product

customcustom-optionsmagento2

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/

Best Answer

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.

Related Topic