Magento2 自动重新索引后无法应用Magento 2目录规则
我在Magento 2.3.5中编写了创建目录规则的代码,保存后,我有代码自动对其重新编制索引Magento2 自动重新索引后无法应用Magento 2目录规则,magento2,Magento2,我在Magento 2.3.5中编写了创建目录规则的代码,保存后,我有代码自动对其重新编制索引 /** @var \Magento\CatalogRule\Model\Rule $model */ $ruleRepository = $this->_objectManager->get( \Magento\CatalogRule\Api\CatalogRuleRepositoryInterface::class
/** @var \Magento\CatalogRule\Model\Rule $model */
$ruleRepository = $this->_objectManager->get(
\Magento\CatalogRule\Api\CatalogRuleRepositoryInterface::class
);
$catalogRule = $this->_objectManager->create(\Magento\CatalogRule\Model\Rule::class);
$ruleByBoRefId = $this->getRuleByBoRefId($boRefId);
if (isset($ruleByBoRefId["rule_id"]) && $ruleId = $ruleByBoRefId["rule_id"]) {
$catalogRule = $ruleRepository->get($ruleId);
}
$catalogRule->loadPost($catalogRuleData);
//$catalogRule->save();
$ruleRepository->save($catalogRule);
$this->reindexCatalogRule($catalogRule);
代码重新索引:
public function reindexCatalogRule($catalogRule){
if($catalogRule->isRuleBehaviorChanged()){
$this->_objectManager
->create(\Magento\CatalogRule\Model\Flag::class)
->loadSelf()
->setState(0)
->save();
}
$collection = $this->_indexerCollectionFactory->create()->getAllIds();
foreach ($collection as $id) {
$indexer = $this->_indexerFactory->create()->load($id);
$indexer->getState()
->setStatus(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID)
->save();
$indexer->reindexAll();
}
}
但重新编制索引后,该规则仍不适用于该产品。但是,在我转到管理员并编辑以前保存的目录规则=>单击Save=>之后,我运行了magento的Indexer:reindex命令,该规则将正常应用。
我不明白为什么会这样。请帮助我如何将规则自动应用于产品?谢谢