0

I want to Delete products custom options using bulk remove functionality. How I can archive that?

2 Answers 2

0

You need to do this by custom script

//$products is your products collection where custom options needs to be deleted
foreach ($products as $product) {
    $product = $this->productModel->load($product->getId()); //$this->productModel is the object of \Magento\Catalog\Model\Product
    if ($product->getOptions()) {
        foreach ($product->getOptions() as $opt) {
            $opt->delete();
        }
        echo $product->getId()." Options deleted";
    } else {
        echo $product->getId()." has no option";
    }
}

You can either use it in custom script, or in admin controller, where ever you need.

0

Run Script in your magento2 root directory

ob_start();
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
ini_set('memory_limit', '1024M');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $products = $objectManager->get('\Magento\Catalog\Model\Product')->getCollection();
    foreach ($products as $product) {
        $product = $objectManager->get('\Magento\Catalog\Model\Product')->load($product->getId());
        if($product->getOptions() != ”){
           foreach ($product->getOptions() as $opt){
                       $opt->delete();
                   }
           $product->setHasOptions(0)->save();
      }
    }

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.