Creating a custom condition for a shopping cart price rule

Creating a custom condition for a shopping cart price rule

August 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Creating a custom condition for a shopping cart price rule

Creating Custom Shopping Cart Price Rules in Magento: A Step-by-Step Guide

As a senior developer working on customizing an existing Magento installation, you’re tasked with implementing complicated conditions for shopping cart price rules that aren’t covered by the built-in options. This often involves writing your own PHP code to determine whether a rule should be applied or not.

Understanding the Problem: The Built-In Limitations of Shopping Cart Price Rules

The default Magento setup provides several predefined conditions for shopping cart price rules, such as minimum order amount, product attributes, and customer groups. However, there are scenarios where these built-in options fall short, requiring you to create custom solutions tailored to your client’s specific requirements.

The Goal: Integrating Custom PHP Code into Shopping Cart Price Rules

Your objective is clear - you need a way to incorporate your own PHP function that evaluates complex business logic to determine if a price rule should be applied. This involves writing the necessary code and integrating it within the Magento framework, ensuring it works seamlessly with the existing pricing engine.

The Solution: Creating a Custom Magento Module for Price Rules

To achieve this, you’ll need to create a custom module that extends the functionality of the built-in shopping cart price rules system. Here’s an outline of the steps involved:

  1. Create a new module: Start by creating a new module in your custom extension directory (app/code/Company/CustomPriceRules). Make sure to follow Magento’s naming conventions and folder structure.
mkdir app/code/Company/CustomPriceRules
  1. Define the module configuration: In etc/module.xml, specify the necessary information about your module:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_CustomPriceRules" setup_version="1.0.0">
        <sequence>
            <module name="Magento_PromotionRules"/>
        </sequence>
    </module>
</config>
  1. Create the custom condition model: In etc/condition.xml, define your new condition type:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_PromotionRules:etc/condition.xsd">
    <type name="Company_CustomPriceRules/Condition/ComplicatedCondition">
        <label translate="true">Your Custom Condition Label</label>
        <attributes>
            <attribute name="some_condition" sort_order="10"
                label="Some Condition" type="text"/>
        </attributes>
    </type>
</config>
  1. Implement the custom condition model: In app/code/Company/CustomPriceRules/etc/di.xml, configure the dependency injection for your custom condition class:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Rule\Model\Condition\Context">
        <arguments>
            <argument name="registry" xsi:type="object">Company_CustomPriceRules\etc\di.xml</argument>
        </arguments>
    </type>
</config>
  1. Create the PHP function for evaluating custom conditions: In app/code/Company/CustomPriceRules/Model/Condition/ComplicatedCondition.php, implement your custom PHP logic to determine if a rule should be applied or not.
namespace Company\CustomPriceRules\Model\Condition;

class ComplicatedCondition extends \Magento\Rule\Model\Condition\AbstractCondition
{
    public function validate(Observer $observer)
    {
        // Your complex business logic goes here
        // ...
        return true; // Return true if the rule should be applied
        // or false if it shouldn't
    }
}
  1. Enable the custom condition in the admin interface: In app/code/Company/CustomPriceRules/etc/adminhtml/system.xml, configure the admin settings to make your new condition available:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/system_file.xsd">
    <system>
        <tab id="promotion_rules">
            <section id="promotion_rules_condition" show_in_default="1" show_in_main_menu="1" sort_order="40" translate="label, comments">
                <label>Custom Price Rules Conditions</label>
                <resource>Magento_PromotionRules::config_rule_conditions</resource>
                <group id="custom_condition_group">
                    <field id="company_custompricerules_condition_type"
                            translate="label"
                            sort_order="10"
                            show_in_default="1"
                            show_in_main_menu="1"
                            component="Magento\Rule\Block/Adminhtml\Promotion\Condition\Type">
                        <label>Custom Condition</label>
                    </field>
                </group>
            </section>
        </tab>
    </system>
</config>
  1. Compile and install the module: Compile your custom module using a Magento 2 compiler tool and then install it in your Magento instance.

Conclusion: Integrating Custom PHP Logic into Shopping Cart Price Rules

Creating custom conditions for shopping cart price rules in Magento involves creating a custom module, defining a new condition type, implementing the PHP logic to evaluate complex business rules, and enabling the new condition in the admin interface. By following this step-by-step guide, you can seamlessly integrate your own PHP code into the existing pricing engine, providing your client with the functionality they require.

For more information on customizing Magento modules and building extensions, check out our comprehensive guides at Laravel Company.

Remember, it’s essential to thoroughly test your custom conditions in various scenarios, ensuring they behave as expected under different circumstances. Properly documenting your code and maintaining good coding practices will make it easier for you or other developers to maintain and extend the functionality in the future.

Don’t hesitate to reach out to our team at Laravel Company if you have any questions or need further assistance with Magento customization projects. We’re always ready to help!

Checkout Catalog Extensions Themes

Our network