Magento 2でis_activeの値をプログラムで変更するにはどうすればよいですか?私がこれまでに(オブザーバーで)試したことは次のとおりです。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テーブルの値です。
を保存するには、次の試してくださいあなたはそれが 'admin_system_config_changed_%'イベントであるこの – Shine
ために、使用している場合には、コレクションをロードする必要はありません。 – hapablap