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

时间:2016-07-12 14:38:31

标签: magento magento2

如何以编程方式更改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'值没有变化。似乎可以使用魔法集函数更改的唯一值是表catalog_category_entity的值。

2 个答案:

答案 0 :(得分:1)

is_active 在商店级别管理

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

尝试按照以下方式保存在管理级别

 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);
    }

答案 1 :(得分:0)

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

创建file.php复制并在下面的代码中通过,然后像(DomainName / file.php)这样运行后在root中导入

<?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;

?>