Discount API Basics
You can manage discounts from the Applications menu or with REST APIs. Call the headless-admin-commerce-pricing services to create and manage discounts.
Adding a Discount
Start a new Liferay DXP instance by running
Sign in to Liferay at http://localhost:8080 using the email address test@liferay.com and the password test. When prompted, change the password to learn.
Once Liferay is running,
-
Download and unzip Discount API Basics.
-
Discounts are scoped to an instance. Use the cURL script to add a new discount. On the command line, navigate to the
curl
folder. Execute theDiscount_POST_ToInstance.sh
script.The JSON response shows a new discount was added:
-
To verify the discount addition, open the Global Menu (
) and navigate to Commerce → Discounts. The new discount appears.
-
Alternatively, call the REST service using the Java client. Navigate into the
java
folder and compile the source files: -
Run the
Discount_POST_ToInstance
class.
Adding a Coupon Code
To add a coupon code to the discount, set useCouponCode
to true
and set a value in the couponCode
field. To restrict the coupon code’s usage, use the settings described in the table below.
Restriction Type | Settings | Description |
---|---|---|
Number of uses | "limitationType" : "limited for total" "limitationTimes" : 5 (any integer) | The coupon code can be used a total of 5 times. |
Number of uses per account | "limitationType": "limited for account" "limitationTimesPerAccount" : 5 (any integer) | Each account can use the coupon code up to 5 times. |
Number of uses and by account | "limitationType" : "limited for account and total" "limitationTimesPerAccount": 2 (any integer)"limitationTimes" : 5 (any integer) | Each account can use the coupon code up to 2 times, with total usage capped at 5. |
Examine the cURL Command
The Discount_POST_ToInstance.sh
script calls the REST service with a cURL command.
Here are the command’s arguments:
Arguments | Description |
---|---|
-H "Content-Type: application/json" | Set the request body format to JSON. |
-X POST | Set the HTTP method to invoke at the specified endpoint. |
"http://localhost:8080/o/headless-commerce-admin-pricing/v2.0/discounts" | Specify the REST service endpoint. |
-d "{\"level\": \"L1\", \"limitationType\": \"unlimited\", \"target\": \"products\", \"title\": \"Foo\", \"usePercentage\": true}" | Enter the data to post. |
-u "test@liferay.com:learn" | Enter basic authentication credentials. |
Basic authentication is used here for demonstration purposes. For production, you should authorize users via OAuth2. See Using OAuth2 to Authorize Users for a sample React application using OAuth2.
The other cURL commands use similar JSON arguments.
Examine the Java Class
The Discount_POST_ToInstance.java
class adds a discount by calling the DiscountResource
service.
This class invokes the REST service using only three lines of code:
Line (abbreviated) | Description |
---|---|
DiscountResource.Builder builder = ... | Get a Builder for generating a DiscountResource service instance. |
DiscountResource discountResource = builder.authentication(...).build(); | Use basic authentication and generate a DiscountResource service instance. |
discountResource.postDiscount(...); | Call the discountResource.postDiscount method and pass the data to post. |
Note that the project includes the com.liferay.headless.commerce.admin.pricing.client.jar
file as a dependency. You can find client JAR dependency information for all REST applications in the API Explorer in your installation at /o/api
(e.g., http://localhost:8080/o/api).
The main
method’s comment demonstrates running the class.
The remaining example Java classes call different DiscountResource
methods.
See DiscountResource for service details.
Below are examples of calling other Discount
REST services using cURL and Java.
Get Discounts from Instance
List all the discounts in your Liferay instance with a cURL or Java command.
Discounts_GET_FromInstance.sh
Command:
Code:
Discounts_GET_FromInstance.java
Command:
Code:
The instance’s Discount
objects are formatted in JSON.
Get a Discount
Get a specific discount with cURL or Java get
commands. Replace 1234
with the discount’s ID.
Use Discounts_GET_FromInstance.[java|sh]
to get a list of all discounts, and note the id
of the discount you want specifically.
Discount_GET_ById.sh
Command:
Code:
Discount_GET_ById.java
Command:
Code:
The Discount
fields are listed in JSON.
Patch a Discount
Update an existing discount with cURL and Java patch
commands. Replace 1234
with your discount’s ID.
Discount_PATCH_ById.sh
Command:
Code:
Discount_PATCH_ById.java
Command:
Code:
Delete a Discount
Delete an existing discount with cURL and Java delete
commands. Replace 1234
with your discount’s ID.
Discount_DELETE_ById.sh
Command:
Code:
Discount_DELETE_ById.java
Command
Code:
The API Explorer shows the Discount
services and schemas and has an interface to test each service.