How to Create Catalog Price Rule in Magento 2 Programmatically?

How to Create Catalog Price Rule in Magento 2 Programmatically

Hello Magento Friends,

Creating catalog price rules programmatically in Magento 2 can be a valuable skill, especially for developers who need to automate pricing adjustments based on specific conditions. Catalog price rules allow store owners to offer discounts across a range of products without the need for coupon codes. In this guide, we’ll walk through how to create catalog price rules programmatically in Magento 2 using PHP.

Steps to Create Catalog Price Rule in Magento 2 Programmatically:

Step 1: Create root script in your module.

<?php
try
{
	error_reporting(E_ALL);
	ini_set('display_errors', 1);

	use Magento\Framework\App\Bootstrap;
	require __DIR__ . '/app/bootstrap.php';

	$bootstrap = Bootstrap::create(BP, $_SERVER);

	$obj = $bootstrap->getObjectManager();

	$state = $obj->get(Magento\Framework\App\State::class);
	$state->setAreaCode('adminhtml');

	$ruleFactory = $obj->create(\Magento\CatalogRule\Model\RuleFactory::class);
	$catalogRuleRepository = $obj->create(\Magento\CatalogRule\Api\CatalogRuleRepositoryInterface::class);

	$rule = $ruleFactory->create();

	$rule->setName('Programmatic Rule')
	   ->setDescription('Created Programmatically Object Manager')
	   ->setIsActive(1)
	   ->setCustomerGroupIds([0, 1, 2, 3]) // Assuming these groups exist
	   ->setWebsiteIds([1]) // Assuming this website exists
	   ->setSimpleAction('by_percent')
	   ->setDiscountAmount(10)
	   ->setStopRulesProcessing(0);

	// Define conditions here
	$conditions = [
	  'type' => \Magento\CatalogRule\Model\Rule\Condition\Combine::class,
	  'aggregator' => 'all',
	  'value' => 1, // 1 means TRUE, i.e., ALL conditions have to be TRUE.
	  'new_child' => '',
	  'conditions' => [
	     [
	      'type' => \Magento\CatalogRule\Model\Rule\Condition\Product::class,
	      'attribute' => 'category_ids',
	      'operator' => '==',
	      'value' => '10' // Category ID to which the rule applies
	     ]
	   ]
	];

	$rule->getConditions()->loadArray($conditions);

	$catalogRuleRepository->save($rule);
}
catch(\Exception $e)
{
      print_r($e->getMessage());
}

Output:

catalog price rule

Conclusion:

Creating catalog price rules programmatically in Magento 2 is an effective way to automate pricing updates and implement discounts for specific products or customer segments. By following these steps, you can streamline your promotional campaigns and improve the shopping experience for your customers.

Related Tutorials – 

How to Create a Catalog Price Rule in Magento 2

How to Add Custom Field in Catalog Price Rule Form in Magento 2?

Happy Coding!

Previous Article

Top 10 Tips to Hire Shopify Developers

Next Article

Magento Open Source 2.4.8-Beta Release Notes

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨