How to Disable Empty Category Programmatically using Root Script in Magento2?
Are you tired of seeing empty categories cluttering up your Magento 2 store? Do you want to streamline your site and improve its user experience? If so, you’re in luck! In this blog post, we’ll show you how to disable empty categories programmatically using a root script in Magento 2. With just a few simple steps, you can clean up your store and provide a more organized browsing experience for your customers. So let’s dive in and learn how to simplify your category structure today using our Magento development company!
What is Magento 2?
Magento 2 is the next generation of the popular eCommerce platform Magento. It offers a variety of new features and improvements over its predecessor, Magento 1. x. In this article, we’ll show you how to disable empty categories programmatically using the root script in Magento 2.
What is Programmatically Disable Empty Categories in Magento 2?
Steps to Disable Empty Category Programmatically using Root Script in Magento 2:
Category position before running the script
Step 1: Create a file in your Magento root directory at the below path.
magento_root_directory\Disablecategory.php
Then add the code as below
<?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(‘frontend’);
$Collectionfactory = $objectManager->get(‘\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory’);
$Categoryfactory = $objectManager->get(‘\Magento\Catalog\Model\CategoryFactory’);
$Categorydisable=false;
$categories=$Collectionfactory->create()->addAttributeToSelect(‘*’);
foreach ($categories as $category)
{
$products = $category->getProductsPosition();
if(empty($products))
{
$category = $Categoryfactory->create()->setStoreId(0)->load($category->getId());
$category->setIsActive(0);
$category->save();
$Categorydisable=true;
}
}
if($Categorydisable)
{
echo “Category disable successfull”;
}
else
{
echo “Category disable failed!”;
}
}
catch(\Exception $e)
{
echo “Error : “.$e->getMessage();
}
?>
Step 2: After the above step, you will need to run the below-given URL.
https://yourdomain.com/Disablecategory.php
How can our Magento company help?
Seedcart, a Magento development company, can help you disable empty categories programmatically using a root script. This can be done by adding a few lines of code to your script. Seedcart has a team of experienced Magento developers who can help you with this and other Magento development needs.
Seedcart can also help you with any customization, integration, or migration of Magento stores. They are experts in the Magento framework and provide solutions tailored to your business needs. You can trust them for quality services at affordable rates.
Conclusion
Disabling empty categories programmatically using root scripts in Magento 2 can be a great way to optimize your store’s structure. By doing so, you can avoid having unnecessary clutter on the front end of your site and ensure that shoppers are able to quickly find what they are looking for. With the help of root script commands like “setIsActive” and “save”, you can easily disable any empty categories from appearing on your website while also ensuring that the content associated with them is not lost. So go ahead and give it a try – you’ll definitely notice an improvement in your store’s performance!