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?

1 Answer 1

1

A little late to the party, but I suffered from this myself recently and hope this solution will help the next person.

Essentially, the reason why you're seeing that error is that you don't need to save the product after deleting the option as it's handled within its own repository. Trying to save the product after deleting the option simply makes Magento try to save something that no longer exists

Also, you shouldn't really be using the object manager like that...

I hope the below will enough to point you in the right direction :)

<?php
namespace My\Module\Model\Product\Option;

use Magento\Catalog\{
    Model\ProductRepository,
    Model\Product\Option\Repository as OptionRepository
};
use Magento\Framework\Exception\{
    LocalizedException,
    NoSuchEntityException
};

/**
 * Class Updater
 * @package My\Module\Model\Product\Option
 */
class Updater
{

    /**
     * @var OptionRepository 
     */
    private $optionRepository;

    /**
     * @var ProductRepository 
     */
    private $productRepository;

    /**
     * Updater constructor.
     * @param OptionRepository $optionRepository
     * @param ProductRepository $productRepository
     */
    public function __construct(
        OptionRepository $optionRepository,
        ProductRepository $productRepository
    ) {
        $this->optionRepository = $optionRepository;
        $this->productRepository = $productRepository;
    }

    /**
     * @param $productId
     */
    private function updateProduct($productId)
    {
        try {
            $product = $this->productRepository->getById(
                $productId
            );
        } catch (NoSuchEntityException $e) {
            return;
        }
        if (!$product->hasOptions()) {
            return;
        }

        foreach ($product->getOptions() as $option) {
            try {
                if ($option->getTitle() != 'Custom') {
                    continue;
                }

                $this->optionRepository->delete($option);

            } catch (LocalizedException $e) {
                // Skip the option and do nothing
            }
        }
    }

}

Your Answer

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

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