How To

How to Get All Active Catalog Rules Collection using Root Script in Magento 2?

Hello Magento Friends,

In today’s blog, I will explain How to Get All Active Catalog Rules Collection using Root Script in Magento 2.

Magento 2, a powerful and versatile e-commerce platform, allows you to manage and apply catalog rules to control the pricing and visibility of products on your online store. Learn – How to Create a Catalog Price Rule in Magento 2.

If you need to retrieve all active catalog rules programmatically, you can create a root script to fetch and display this information. In this blog post, we’ll guide you through the process step by step.

Steps to Get All Active Catalog Rules Collection using Root Script in Magento 2:

Step 1: Create CatalogruleCollection.php file in the Magento root path and then add the below code.

<?php
 
use Magento\Framework\AppInterface;
 
try {
   require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}

try
{
  $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
 $objectManager = $bootstrap->getObjectManager();
 $appState = $objectManager->get('Magento\Framework\App\State');
 $appState->setAreaCode('frontend');
  
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $ruleCollectionFactory = $objectManager->create('\Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory')->create(); 
 $catalogActiveRule = $ruleCollectionFactory->addFieldToFilter('is_active', 1);
        foreach($catalogActiveRule as $rule) {
            echo "<pre>";
            print_r($rule ->getData());
            echo "</pre>";
 }  
  }
 
catch(\Exception $e){ 
    print_r($e->getMessage());
}

Step 2: After adding the above code, run the URL below to check whether the Catalog Rules Data was retrieved.

https://yourdomain/pub/CatalogruleCollection.php/ 

Conclusion:

Creating a root script to retrieve all active catalog rules in Magento 2 is a straightforward process. This script provides a starting point, and you can customize it based on your specific requirements. Understanding the structure of the script and the Magento 2 components involved will empower you to perform more advanced operations in your e-commerce endeavors.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

10 hours ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

12 hours ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

2 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

5 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 week ago