0

I have tried to disable categories in Magento 2

I have tried to achieve this via PHP script like below

<?php 

use \Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()                              
    ->addAttributeToSelect('*'); //categories from current store will be fetched
echo count($categories);
foreach ($categories as $category){
    $category_id = $category->getId();
    echo $category_id."<br>";

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $collection = $objectManager ->get('Magento\Catalog\Model\Category')->load($category_id);
    $collection->setData('is_active', 0);
    $collection->save();
    echo "success";
}
?>

But I got following error

Fatal error: Uncaught Magento\Framework\Exception\LocalizedException: Invalid URL key in /var/www/html/vkm/vendor/magento/module-catalog-url-rewrite/Observer/CategoryUrlPathAutogeneratorObserver.php:93

Please provide a solution to disable categories in Magento 2

0

Hi I have tried to edit directly in database like below and I have achieved what I want based on Disable All Children of Disabled Category for Store View

UPDATE catalog_category_entity_int SET value = '0' WHERE entity_id IN ({{add category id's by comma separate }}) AND attribute_id=(SELECT attribute_id FROM eav_attribute WHERE attribute_code = "is_active");

| improve this answer | |

Your Answer

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

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