Magento 2 Catalog Price Rule in REST API

Magento 2 Catalog Price Rule is not exposed as a REST API End Point. Catalog Price Rule API feature is an important feature for those who work in Creating Mobile App and Third Party Integration. Though this feature is not available we can extend magento core with our custom code to expose this API End Point

Lets see how to achieve this with our custom code.

Step 1 : Create and enable our new custom module

app/code/Mydons/CatalogRuleApi/registration.php

app/code/Mydons/CatalogRuleApi/etc/module.xml

Step 2:- Once after creating the module.xml and registration.php we need to create an webapi.xml file to define the REST API End Points.

The Core Magento 2 CatalogRule module already contains the necessary code for carrying out the API Operation in the below files, only drawback is those methods were not exposed as webapi end points. Lets define the REST API route path as “catalogRules”.

Though the main methods are all available in magento core, the “getList” method is not available by default and we will implement it separately. For this demo i am skipping the ACL Part and defining the “resource ref” as anonymous

Magento 2 Core Files Reference:-

Magento\CatalogRule\Api\interface\CatalogRuleRepositoryInterface

Magento\CatalogRule\Model\Rule.php

File:- app/code/Mydons/CatalogRuleApi/etc/webapi.xml

Step 3:- In the above webapi.xml for getting the list of all catalogRules we need to implement our custom “getList” method in

File:- Mydons\CatalogRuleApi\Api\CatalogRuleRepositoryInterface

Step 4:- We need to create the di.xml to specify the preference implementation classes for both our custom interface and core module interface.

File: app/code/Mydons/CatalogRuleApi/etc/di.xml

Step 5:- At this point now we can execute the below REST API methods V1/catalogRules/:ruleId GET, DELETE, POST, PUT. But the problem here is the GET Method returns data without website_ids and customer_group_ids as show in the below screenshot.

To resolve this problem we can add the website_ids and customer_group_ids in extension_attributes.xml file and hook the GET Method Using Plugin to add the missing data.

File: app/code/Mydons/CatalogRuleApi/etc/extension_attributes.xml

File: app/code/Mydons/CatalogRuleApi/Data/RuleExtensionInterface.php

Now we need to add the plugin specification in the existing di.xml file to retrieve and save the website_ids and customer_group_ids properly

File: app/code/Mydons/CatalogRuleApi/etc/di.xml

File: app/code/Mydons/CatalogRuleApi/Plugin/CatalogRuleRepositoryPlugin

Step 6: Now we need to add our custom getList Method implementation to Fetch all the Catalog Price Rules.

File: app/code/Mydons/CatalogRuleApi/Model/CatalogRuleManagement

Now We can try hitting the postman with searchCriteria as shown below
rest/V1/catalogRules/search?searchCriteria[filter_groups][0][filters][0][field]=rule_id&searchCriteria[filter_groups][0][filters][0][value]=1&searchCriteria[filter_groups][0][filters][0][condition_type]=eq&searchCriteria[pageSize]=10

Sample Request Response Format

Method:- PUT
URL:- rest/V1/catalogRules/:ruleId
Sample Request Format:-
(Pass only the parameters which needs to be modified)

{
“rule”: {
“name”: “15% off all Women’s and Men’s Pants”,
“description”: “15% off all Women’s and Men’s Pants”,
“is_active”: 1,
“rule_condition”: {
“type”: “Magento\CatalogRule\Model\Rule\Condition\Combine”,
“attribute”: “”,
“operator”: “”,
“value”: “1”,
“is_value_parsed”: false,
“aggregator”: “all”,
“conditions”: [
{
“type”: “Magento\CatalogRule\Model\Rule\Condition\Product”,
“attribute”: “category_ids”,
“operator”: “()”,
“value”: “27”,
“is_value_parsed”: false,
“aggregator”: “”
}
]
},
“stop_rules_processing”: 1,
“sort_order”: 0,
“simple_action”: “by_percent”,
“discount_amount”: 15,
“extension_attributes”: {
“website_ids”: [
“1”
],
“customer_group_ids”: [
“0”,
“2”
]
}
}
}



Method:- POST
URL:- rest/V1/catalogRules/:ruleId
Sample Request Format:-

(Pass only the parameters which needs to be modified)
{
“rule”: {
“name”: “15% off all Women’s and Men’s Pants”,
“description”: “15% off all Women’s and Men’s Pants”,
“is_active”: 1,
“rule_condition”: {
“type”: “Magento\CatalogRule\Model\Rule\Condition\Combine”,
“attribute”: “”,
“operator”: “”,
“value”: “1”,
“is_value_parsed”: false,
“aggregator”: “all”,
“conditions”: [
{
“type”: “Magento\CatalogRule\Model\Rule\Condition\Product”,
“attribute”: “category_ids”,
“operator”: “()”,
“value”: “27”,
“is_value_parsed”: false,
“aggregator”: “”
}
]
},
“stop_rules_processing”: 1,
“sort_order”: 0,
“simple_action”: “by_percent”,
“discount_amount”: 15,
“extension_attributes”: {
“website_ids”: [
“1”
],
“customer_group_ids”: [
“0”,
“2”
]
}
}
}

I am Working on the ACL part of the above module, if anyone is interested in the above module drop your email in the comment, i’ll provide the source code.

Leave a Comment

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

10 − 3 =