以编程方式更改 Magento 2 中类别的 'is_active'

Programmatically change 'is_active' of category in Magento 2

如何以编程方式更改 Magento 2 中 'is_active' 的值?到目前为止(在观察者中)我尝试过的是:

class Observer implements ObserverInterface
{   
    /**
     * @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
     */
    protected $_categoryCollectionFactory;

    /**
     * @var \Magento\Catalog\Api\CategoryRepositoryInterface
     */
    protected $_repository;

    public function __construct(
            \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
            \Magento\Catalog\Api\CategoryRepositoryInterface $repository
    ) {
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        $this->_repository = $repository;
    }

    /**
     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        $categoryCollection = $this->_categoryCollectionFactory->create();
        $categoryCollection->addAttributeToSelect('*');
        $categoryCollection->addAttributeToFilter('name', array('eq' => 'test'));
        $currentCategory = $categoryCollection->getFirstItem();

        $currentCategory->setIsActive(true);
        $this->_repository->save($currentCategory);
    }
}

'is_active' 值没有变化。似乎可以用神奇的设置函数更改的唯一值是 table catalog_category_entity.

的值

is_active 在商店级别管理

确保将管理员级别的商店 ID 更改为 0,您保存到商店级别的值,您可以通过更改 Store View 下拉列表进行确认类别编辑的管理页面。

尝试以下操作以在管理员级别保存

 public function execute(EventObserver $observer)
    {
        $categoryCollection = $this->_categoryCollectionFactory->create();
        $categoryCollection->addAttributeToSelect('*');
        $categoryCollection->addAttributeToFilter('name', array('eq' => 'test'));
        $currentCategory = $categoryCollection->getFirstItem();
        $currentCategory->setStoreId(0);
        $currentCategory->setIsActive(true);
        $this->_repository->save($currentCategory);
    }

Magento 2 设置类别值被脚本激活它是根目录运行

创建 file.php 复制并传递下面的代码并在 运行 之后导入根目录,如 (DomainName/file.php)

<?php
//increase the max execution time
@ini_set('max_execution_time', -1);
//memory_limit
@ini_set('memory_limit', -1);

error_reporting(E_ALL);
ini_set('display_errors', '1');

use \Magento\Framework\App\Bootstrap;

include('app/bootstrap.php');

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

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        

$appState = $objectManager->get('\Magento\Framework\App\State');

$appState->setAreaCode('frontend');

$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

$categories = $categoryCollection->create();

$categories->addAttributeToSelect('*');

$categories->load();

if (count($categories) > 0):

        foreach($categories as $category):

            $catId = $category->getId();

                $category = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->create()->setStoreId(0)->load($catId);
                $CategoryName = $category->getName();

                $category->setIsActive(1);
                $category->save();

        endforeach;
     else: echo "No Results";
    endif;

?>