Edit in GitHubLog an issue
Thanks to Ziffity for contributing this topic!

Extend catalog rule conditions

Catalog price rules can be used to offer products to buyers at a discounted price, based on a set of defined conditions. Catalog price rules do not use coupon codes. The discounts are applied to the final product price in the product listing and the product description page even before adding the product to the shopping cart. For more information about catalog price rules, refer to Catalog Rules.

Default conditions#

In the Conditions tab, Adobe Commerce and Magento Open Source have the following product attributes in the add conditions section.

Catalog Rule Conditions

Implementation of existing conditions#

In the app/code/Magento/CatalogRule/etc/di.xml configuration, the following type configuration is defined:

Copied to your clipboard
1<type name="Magento\CatalogRule\Model\Rule\Condition\Combine">
2 <arguments>
3 <argument name="data" xsi:type="array">
4 <item name="form_name" xsi:type="string">catalog_rule_form</item>
5 </argument>
6 </arguments>
7</type>
8<type name="Magento\CatalogRule\Model\Rule\Condition\Product">
9 <arguments>
10 <argument name="data" xsi:type="array">
11 <item name="form_name" xsi:type="string">catalog_rule_form</item>
12 </argument>
13 </arguments>
14</type>

In the above configuration, the catalog_rule_form is a UI component form. It is defined in app/code/Magento/CatalogRule/view/adminhtml/ui_component/catalog_rule_form.xml.

The Magento\CatalogRule\Model\Rule\Condition\Combine and the Magento\CatalogRule\Model\Rule\Condition\Product classes contain the list of conditions and validations used for creating extended conditions.

The getNewChildSelectOptions method in app/code/Magento/CatalogRule/Model/Rule/Condition/Combine.php is responsible for the listed conditions. It returns an array of the lists of valid conditions.

Below is the definition of getNewChildSelectOptions method:

Copied to your clipboard
1/**
2 * @return array
3 */
4public function getNewChildSelectOptions()
5{
6 $productAttributes = $this->_productFactory->create()->loadAttributeOptions()->getAttributeOption();
7 $attributes = [];
8 foreach ($productAttributes as $code => $label) {
9 $attributes[] = [
10 'value' => 'Magento\CatalogRule\Model\Rule\Condition\Product|' . $code,
11 'label' => $label,
12 ];
13 }
14 $conditions = parent::getNewChildSelectOptions();
15 $conditions = array_merge_recursive(
16 $conditions,
17 [
18 [
19 'value' => \Magento\CatalogRule\Model\Rule\Condition\Combine::class,
20 'label' => __('Conditions Combination'),
21 ],
22 ['label' => __('Product Attribute'), 'value' => $attributes]
23 ]
24 );
25 return $conditions;
26}

In the above example, the $conditions array will contain the list of valid conditions. Each item in the array will have the value and the label key with the appropriate values.

The validate method from the app/code/Magento/CatalogRule/Model/Rule/Condition/Product.php file is responsible for the validations of the conditions defined in the catalog price rules.

Below is the definition of the validate method:

Copied to your clipboard
1<?php
2/**
3 * Copyright © Magento, Inc. All rights reserved.
4 * See COPYING.txt for license details.
5 */
6
7use Magento\Catalog\Model\Product;
8use Magento\Framework\Model\AbstractModel;
9
10/**
11 * Validate product attribute value for condition
12 *
13 * @param Product|AbstractModel $model
14 * @return bool
15 */
16public function validate(AbstractModel $model): bool
17{
18 $attrCode = $this->getAttribute();
19 if ('category_ids' === $attrCode) {
20 return parent::validate($model);
21 }
22
23 $oldAttrValue = $model->getData($attrCode);
24 if ($oldAttrValue === null) {
25 if ($this->getOperator() === '<=>') {
26 return true;
27 }
28 return false;
29 }
30
31 $this->_setAttributeValue($model);
32
33 $result = $this->validateAttribute($model->getData($attrCode));
34 $this->_restoreOldAttrValue($model, $oldAttrValue);
35
36 return (bool)$result;
37}

In the above example, the validate method defines the logic to validate the specified conditions in the catalog price rule.

Note that the app/code/Magento/CatalogRule/Model/Rule/Condition/Product.php class extends from the app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php abstract class.

The app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php abstract class extends from the app/code/Magento/Rule/Model/Condition/AbstractCondition.php class.

Steps to extend catalog rule conditions#

  • Create the after plugin for the getNewChildSelectOptions method and add your custom condition to it.
  • Add the type configuration in <custom_module_dir>/etc/di.xml.
  • Create a class that extends the Magento\Rule\Model\Condition\AbstractCondition class. Then define the validate method with your custom logic.
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.