1

I am working on custom script to update product custom options.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->get($sku,true);
$customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product);
if(isset($customOptions) && !empty($customOptions)){
    foreach($customOptions as $customOption):
        if($customOption->getTitle()=='Custom'){                            
            $customOption->delete();                                            
        }                               
    endforeach;                         
}
$_product->save();

When We save product to update it shows error below error -

PHP Fatal error:  Uncaught Magento\Framework\Exception\NoSuchEntityException: No such entity. in /home/dermpro/m2-customer3.dermpro.com/vendor/magento/module-catalog/Model/Product/Option/Repository.php:170
Stack trace:
#0 /home/dermpro/m2-customer3.dermpro.com/vendor/magento/module-catalog/Model/Product/Option/SaveHandler.php(56): Magento\Catalog\Model\Product\Option\Repository->save(Object(Magento\Catalog\Model\Product\Option))
#1 /home/dermpro/m2-customer3.dermpro.com/vendor/magento/framework/EntityManager/Operation/Update/UpdateExtensions.php(49): Magento\Catalog\Model\Product\Option\SaveHandler->execute(Object(Magento\Catalog\Model\Product\Interceptor), Array)
#2 /home/dermpro/m2-customer3.dermpro.com/vendor/magento/framework/EntityManager/Operation/Update.php(109): Magento\Framework\EntityManager\Operation\Update\UpdateExtensions->execute(Object(Magento\Catalog\Model\Product\Interceptor), Array)
#3 /home/dermpro/m2-customer3.dermpro.com/vendor/magento/framework/EntityManager/EntityManager.php(96): Magento\Framework\Entity in /home/dermpro/m2-customer3.dermpro.com/vendor/magento/module-catalog/Model/Product/Option/Repository.php on line 170

Any one go through this type of issue?

Your Answer

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

Browse other questions tagged or ask your own question.