magento2 - Programmatically change 'is_active' of category in Magento 2 -
how can programmatically change value of 'is_active' in magento 2? have tried far (in 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 not changing. seems values can changed magical set functions values of table catalog_category_entity.
is_active managed @ store level
make sure change store id 0
admin level , value have saved store level , can confirm changing store view drop down in admin page category edit.
try following 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); }
Comments
Post a Comment