In this tutorial, I’ll show you how to apply coupon to your products and calculate the discounted price.
Note: This tutorial refers to the latest develop branch which features may not be in the master branch yet. These features will eventually be ported to develop-v0.2 branch, which supports PHP 5.4.
Overview of RedminPortal
RedminPortal is a content management system for developers. It only offers backend system for end users and leave the front end solely to the developer. Hence, in many cases, it is up to the developers to design their business logic for their front end.
In this tutorial, I’ll be showing you one way of using the coupon. However, it is up to your imagination and creativity to come up with many other ways. Treat this as a guiding ground but keeping in mind that this is not the only way.
How to create a new order?
Let’s assume we want to have a Cart class that creates a new order and add products into it. Here’s the code:
Now, to create a new cart in your code, simply do:
How to add products to an order?
Most (if not all) of the models in Redminportal use Eloquent relationships. To add any product into an order, you just need to save the Product object into Order object. I’ll create a new function in Cart class for adding product.
Notice in line 15 we’re using relationship insertion to add product into order.
How to get the total price of an order?
There’s a method in Order class for you to get the total sum of all the product prices. It’s located at <redminportal>/src/App/Models/Order.php.
In our example, if you want to get the total sum of all the prices in a cart, do this:
NOTE: This total price does not include discounted value from coupon. It is purely the summation of the prices of all products/pricelists/bundles.
How to apply a coupon to an order?
Using the same relationship insertion, you can add any coupon into an order like this:
This insertion doesn’t check if the coupon is applicable to any product within the order. It merely adds the coupon to the order.
To add a coupon and making sure that it checks the “multiple_coupons” flag, you can use this method addCoupon():
All other checks are up to you, the developers.
How to get the total discounts in an order?
You can use the method getDiscounts() to get a list of all the products with applicable coupons. And then calculate the discount from there.
Note: the value of each discount is stored in the record key “value”.
I will add this function to the Cart class.
You can then calculate the total payable amount like this:
Update 14 Dec 2015:
Using the latest develop branch, you can now retrieve the total discount from an order like this:
Take note that getDiscounts() saves all the discount information in the $order->options[‘discounts’] array. This is to reduce recalculation whenever you call this method.
However, when you add or remove any coupon or product from the order, you need to call setDiscounts() method to recalculate the new value. Like this:
That’s it. Do leave a comment if there’s any mistake and I’ll update the post.