How To

How to Remove Customizable Options of All Products Programmatically in Magento 2

Hello Magento Friends,

Today’s learning guide is about How to Remove Customizable Options of All Products Programmatically in Magento 2.

Magento 2 catalog maintenance requires various manipulations from the store owner’s side. To increase the user experience you add a number of custom options for products. There comes a point where you need to delete all the custom options applied on Magento 2 products.

What if you have a huge number of products? Removing customizable options for each product is difficult. Hence, with the help of the below code, you can Remove Customizable Options of All Products Programmatically in Magento 2.

Let’s begin,

Steps to Remove Customizable Options of All Products Programmatically in Magento 2:

Step 1: Create remove_product_option.php in the Magento root directory and add the below code.

<?php
use Magento\Framework\AppInterface;

try
{
    require_once __DIR__ . '/app/bootstrap.php';
}
catch (\Exception $e)
{
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try
{
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('global');
    $product_colletion = $objectManager->get('\Magento\Catalog\Model\Product')->getCollection();
    $product_count = 0 ;
    echo 'All Product Custom Option Remove Starting !! <br/>';
    
    foreach ($product_colletion as $product)
    {
        $product = $objectManager->get('\Magento\Catalog\Model\ProductFactory')->create()->load($product->getId());
        if ($product->getHasOptions())
        {
            $option_temp = [];
            $product->setOptions($option_temp);
            $product->setHasOptions(0)->save();
            echo "Update Your Product Name : ".$product->getName()." And Product ID :".$product->getId()." <br/>";
            $product_count++;
        }
    }
    echo 'Product Custom Option Remove Successfully.';
}
catch(\Exception $e)
{
 print_r($e->getMessage());
}

After adding the above code, run the below URL in the browser.

https://yourdomain/remove_product_option.php

Conclusion:

Hence, this was about How to Remove Customizable Options of All Products Programmatically in Magento 2. For any doubts or difficulties, mention in the comment part without any hesitation. Spread the article amongst your friends and colleagues.

Happy Coding!

Click to rate this post!
[Total: 7 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • This worked perfectly. I had to do one minor change for my M2 configuration to the line:
    require_once __DIR__ . '/app/bootstrap.php';

    The script tries to call for a file that is in the /app directory instead of /pub. However, my Magento will always resolve the /pub directory since this is how the Magento 2 app works.

    So I changed the script to:
    require_once(dirname(__FILE__).'/../app/bootstrap.php')

    Fixed!

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago