Type
magento2-module
API Methods for generating a coupon code for a given cart price rule
magento2-module
MIT
None
None
None
None
None
This module adds three API endpoints that provide methods for generating coupon codes for an existing Magento 2 Cart Price Rule from an external source. It has been tested with the REST API, although it should work equally well with the SOAP/XML API as well. Usage documentation is for REST usage only.
Well, although Magento 2 provides a built-in method for generating a list of coupon codes for a promotional rule, in order to use these with another system (like a marketing email platform, or for contest systems, or even CRM systems like SalesForce), you have to generate a set number of coupon codes, and then export them via CSV. For on-demand generation, it makes more sense to generate codes as they're needed.
Installation is available via composer. The package name is bangerkuwranger/magento-2-coupon-code-api. Just run these commands at your Magento root:
composer require bangerkuwranger/magento-2-coupon-code-api
php bin/magento module:enable Bangerkuwranger_Couponcodeapi
php bin/magento setup:upgrade
Make sure to familiarize yourself with the Magento API system first.
In order to use the endpoints properly, you'll need to create a Cart Price Rule that has the Coupon field set to Auto. The other settings are up to you. Make sure that your rule is working on your system before setting up the coupon generation logic.
This module allows you to check to make sure that a Magento customer can actually use the generated code by including a customer id when generating a new code. Magento will see which customer group the customer belongs to, and then verify that that group is allowed in your Cart Price Rule. It's optional, but useful if you are generating codes for specific customers. Setting the customer id parameter to zero will just generate the code without checking.
Anyhow, once installed, there will be three endpoints available:
The third one is the only really necessary endpoint; it's what actually generates the code for your Cart Price Rule. The other two are useful if you want to verify the Cart Rule's ID and the customer ID. All three methods require authentication of some sort; make sure to familiarize yourself with the Magento API Authentication process so your logic incorporates the proper authentication steps. Once that's take care of, you can generate the code by making an API request to the getCouponCode endpoint, and you'll get a new coupon code back.
Given a cart rule id, this method returns an array of values from the cart rule if the rule exists, or an error if it does not exist or something else went wrong with the API transaction.
curl -X GET "https://magento.host/index.php/rest/V1//bangerkuwranger/couponcode/getCartRule/?ruleId=20" -H “Content-Type: application/json” -H "Authorization: Bearer vbnf3hjklp5iuytre"
GET
int $ruleId unique identifier for magento cart discount rule
[
[0] (string): name,
[1] (string): description,
[2] (string): fromDate,
[3] (string): toDate,
[4] (int): usesPerCust,
[5] (int): usesPerCoupon,
[6] (bool): isActive,
[7] (array):
groupIds [
(string) groupId,
...additional groupIds...
]
]
Bad Request
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}
Not Authorized / Invalid Token
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}
Server Error / Local Exception
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}
Given an email address, this method returns a unique identifier for the magento customer, or an error if it does not exist or something else went wrong with the API transaction.
curl -X POST "https://magento.host/index.php/rest/V1//bangerkuwranger/couponcode/getCustIdByEmail/" -H “Content-Type: application/json” -H "Authorization: Bearer vbnf3hjklp5iuytre" -d '{"email":"[email protected]"}'
POST
string $email email address to search for customer with
(string) “custId”
Bad Request
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}
Not Authorized / Invalid Token
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}
Server Error / Local Exception
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}
Given a cart rule id and customer id (use the number zero if customer id is not being used), this method returns a unique coupon code for the magento cart rule, or an error if it does not exist or something else went wrong with the API transaction. If customer id is being used, this will check if customer belongs to a group that can use the coupon code first, returning an error if customer is not authorized.
curl -X POST "https://magento.host/index.php/rest/V1//bangerkuwranger/couponcode/getCouponCode/" -H “Content-Type: application/json” -H "Authorization: Bearer vbnf3hjklp5iuytre" -d '{"ruleId":31,”custId”:1055}'
POST
int $ruleId unique identifier for magento cart discount rule
int $custId unique identifier for magento customer
(string) “couponCode”
Bad Request
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}
Not Authorized / Invalid Token
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}
Server Error / Local Exception
error-response {
message (string): Error message ,
errors (error-errors, optional),
code (integer, optional): Error code ,
parameters (error-parameters, optional),
trace (string, optional): Stack trace
}
error-errors [
error-errors-item
]
error-parameters [
error-parameters-item
]
error-errors-item {
message (string, optional): Error message ,
parameters (error-parameters, optional)
}
error-parameters-item {
resources (string, optional): ACL resource ,
fieldName (string, optional): Missing or invalid field name ,
fieldValue (string, optional): Incorrect field value
}