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 Answers 2

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"
];
2
  • Thanks Sohel Rana. What if we programmatically truncate all the catalogrule tables ? Will it be fast as well ? Apr 25, 2019 at 7:41
  • If any thirdparty module uses any event then this way well be safe.
    – Sohel Rana
    Apr 25, 2019 at 8:52
0
SET FOREIGN_KEY_CHECKS = 0;

TRUNCATE TABLE `catalogrule`;
TRUNCATE TABLE `catalogrule_customer_group`;
TRUNCATE TABLE `catalogrule_group_website`;
TRUNCATE TABLE `catalogrule_group_website_replica`;
TRUNCATE TABLE `catalogrule_product_price`;
TRUNCATE TABLE `catalogrule_product_price_replica`;
TRUNCATE TABLE `catalogrule_product`;
TRUNCATE TABLE `catalogrule_product_replica`;
TRUNCATE TABLE `catalogrule_website`;

SET FOREIGN_KEY_CHECKS = 1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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