1

I know we have Delete Rule option when we edit any Catalog Price Rule. But I need to delete all Calalog Price Rules programmatically because there is no option to delete all rules.

Solutions may be :

  • What if I truncate all the tables related to catalogrule and run indexer:reindex
  • Programmatically delete all rules and run indexer:reindex
2

Custom action to delete all the rules

Try following way:

use Magento\CatalogRule\Api\CatalogRuleRepositoryInterface;
use Magento\CatalogRule\Model\ResourceModel\Rule\Collection as RuleCollection;
use Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory;

Di

public function __construct(
    CatalogRuleRepositoryInterface $catalogRuleRepository,
    RuleCollectionFactory $ruleCollectionFactory
) {
    $this->catalogRuleRepository = $catalogRuleRepository;
    $this->ruleCollectionFactory = $ruleCollectionFactory;
}

Now:

/** @var RuleCollection $ruleCollection */
$ruleCollection = $this->ruleCollectionFactory->create();
foreach ($ruleCollection as $rule) {
    $this->catalogRuleRepository->deleteById($rule->getId());
}

OR

Truncate all Catalog Price Rule tables

/** @var array List of all the Catalog Price Rule tables */
public $catalogrule_tables = [
    "catalogrule",
    "catalogrule_customer_group",
    "catalogrule_group_website",
    "catalogrule_group_website_replica",
    "catalogrule_product_price",
    "catalogrule_product_price_replica",
    "catalogrule_product",
    "catalogrule_product_replica",
    "catalogrule_website"
];
  • Thanks Sohel Rana. What if we programmatically truncate all the catalogrule tables ? Will it be fast as well ? – Sudhanshu Bajaj Apr 25 at 7:41
  • If any thirdparty module uses any event then this way well be safe. – Sohel Rana Apr 25 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.