How can I programmatically change the value of 'is_active' in Magento 2? What I have tried so far (in an observer) is:
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);
}
}
The 'is_active' value is not changing. It seems that the only values that can be changed with the magical set functions are the values of the table catalog_category_entity.
is_active is managed at store Level
make sure you change store id to 0
for admin level , the value you have saved to store level , you can confirm by changing Store View drop down in admin page for category edit.
try following to save in admin level
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 set category value is active by script It's run from root directory
Create file.php copy and passt below code and import in root after run like (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;
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.