0

I need put a coupon code. But I don't need the products with any discount (catalog price rule). I want to make discount coupon only for products with normal price.

Is this even possible?

1
  • 1
    It is not possible without a customization to the cart rule validation – Rus0 Aug 7 '20 at 1:46
2

Here's how I solved this problem:

  1. Create a new product attribute (e.g. 'catalog_rule_active' type Yes/No) and attach to your attribute set. Make sure you set "Use For Promo Rule Conditions" to Yes in storefront properties.

  2. Create a cron job that runs shortly after the default catalogrule_apply_all cronjob (default run time is 1am) or schedule to your requirements.

  • the apply all cronjob updates the catalogrule_product table in your db. This table stores all the product_ids that Catalog Rules are applied to.
  • your cron job needs to collect the product ids from this table
  • then your cron should retrieve a product collection of all items that have the catalog_rule_active attribute set to Yes
  • then you can use the active product ids to update the attribute to Yes
  • and then you can filter out the expiring product ids, and update the attribute to No

see example - customise logic as required

    public function execute()
    {
     try {
          $connection = $this->resourceConnection->getConnection();
          $tableName = $this->resourceConnection->getTableName('catalogrule_product');
          $date = strtotime($this->dateTime->gmtDate());

          $select = $connection->select()
              ->from($tableName)
              ->where('from_time = 0 or from_time < ?', $date)
              ->where('to_time = 0 or to_time > ?', $date);

          $ruleProducts = $connection->fetchAll($select);

          $productIds = [];
          foreach ($ruleProducts as $ruleProduct) {
            $productIds[] = $ruleProduct['product_id'];
          }
          $productIds = array_unique($productIds);

          $productCollection = $this->productCollection->create()
                              ->addAttributeToFilter('catalog_rule_active', 1);
          $oldProducts = [];
          foreach ($productCollection as $product) {
            $oldProducts[] = $product->getId();
          }

          $productsToRemove = array_diff($oldProducts, $productIds);

          foreach ($productIds as $productId) {
            $product = $this->productModel->load($productId);
            $product->setData('catalog_rule_active', 1);
            $product->save();
          }

          foreach ($productsToRemove as $removals) {
            $product = $this->productModel->load($removals);
            $product->setData('catalog_rule_active', 0);
            $product->save();
          }
    } catch (\Exception $e) {
        $this->_logger->error($e->getMessage());
    }
}
  1. When you set up any Sales Cart Price rule, set your desired conditions (brand, category, etc) and then add a second condition that excludes any product that has this catalog_rule_active attribute set to Yes. conditions page

The added benefit of this is then you can add your new attribute to filters/columns in the admin Product screen so you can easily see any products that are currently captured by a catalog rule :)

Update: I modified the code from this original answer to capture configurable products as well. You can see the fullcode here https://github.com/bernieu2/magento2-catalogruleactive

7
  • is there any full example for this? github code or something? – Medomage May 4 at 18:54
  • here you go, I put the files you'd need to make this here github.com/bernieu2/magento2-catalogruleactive – bernieu2 May 5 at 2:24
  • great, thank you i have install the module.. but it still doesn't update any product Active catalog rule attribute, should i wait for full day so it's could start with cronjob? – Medomage May 5 at 18:15
  • You can alter schedule in crontab.xml to whatever you like. Eg if you want to run it everyday at 4:30pm it would be 30 16 * * * - you can set it for the next minute or so to force it to run, just make sure to clear cache after changing schedule. crontab.guru is helpful for testing your schedule – bernieu2 May 5 at 23:01
  • yes i got it.. but the problem i have that this store have more than 4000 products, so it's take too much time to update the Attribute value for each products... any suggestion ideas? – Medomage May 6 at 12:50

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.