Creating a shopping cart price rule in Magento automatically

Creating a shopping cart price rule in Magento automatically

July 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Creating a shopping cart price rule in Magento automatically

Creating a shopping cart price rule in Magento programmatically involves using the built-in API endpoints provided by the platform. This approach allows you to add rules dynamically without directly manipulating the database, which is considered more secure and maintainable.

To achieve this, we’ll create a new file (e.g., CreateCartRule.php) and use Magento’s object manager to retrieve the necessary resources. Here’s a step-by-step guide:

<?php
require_once __DIR__ . '/app/bootstrap.php';

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

/** @var \Magento\SalesRule\Model\ResourceModel\Rule $ruleResource */
$ruleResource = $objectManager->get('Magento\SalesRule\Model\ResourceModel\Rule');
$connection = $ruleResource->getConnection();
$tableName  = $ruleResource->getMainTable();

// Insert the rule into the database
$sql = 'INSERT INTO ' . $tableName . " (name, description, simple_action, discount_amount, from_date, to_date)
        VALUES ('10% off for new users', 'Discount for new signups',
                'by_percent', 10,
                '" . date('Y-m-d H:i:s') . "',
                '" . date('Y-m-d H:i:s', strtotime('+1 year')) . "'
        )";

$connection->query($sql);

This script will insert a new rule with the specified name, description, action type (by percent), and discount amount. The rule is valid for one year from the current date.

To execute this script, simply run php CreateCartRule.php in your Magento installation’s root directory using the command line.

It’s important to note that object manager injection should be used sparingly or avoided altogether in a production environment due to its performance implications and potential security risks. In this context, it’s appropriate for demonstrating the process of creating a rule programmatically without relying on external APIs.

For more advanced use cases involving rule conditions and complex logic, consider implementing an event observer or a custom rule model that extends Magento\SalesRule\Model\Rule. This approach provides better separation of concerns and is recommended for more sophisticated rules.

Remember to clear your cache after running this script using the following command:

php bin/magento cache:clean

In conclusion, using Magento’s built-in resources allows you to create shopping cart price rules programmatically without directly manipulating the database. This approach ensures better performance and security compared to manual database updates. By following best practices such as using object manager injection responsibly and clearing the cache afterward, you can effectively manage your store’s promotions while keeping your codebase maintainable.

For more information on Magento development best practices and advanced customization techniques, I recommend exploring the resources available at Laravel Company.

Checkout Database API Pricing

Our network