2017 © Pedro Peláez

library discounts

A package to apply discounts on orders.



A package to apply discounts on orders.

  • Monday, April 3, 2017
  • by rgcouto
  • Repository
  • 1 Watchers
  • 0 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 33 % Grown


Discounts - Cloudoki

By Rafael G. Couto rafaelgcouto@gmail.com, (*1)

Developed work

In order to develop this practical test, I've implemented a Laravel package with everything necessary to handle the discounts tasks proposed by the Cloudoki team., (*2)


  • A fresh (or not) installation of Laravel 5.4


  • Since this is just a practical test and not for production, it is required to lower the minimum stability of Laravel so that you can be able to include this package. In order to do so update you composer.json file of your Laravel project by adding the two lines below:, (*3)

    "minimum-stability" : "dev", 
    "prefer-stable" : true
  • Then add the package to your project by running the following command:, (*4)

    composer require rgcouto/discounts "*", (*5)

  • Copy the cloudoki folder with example data to the project root by running the following command:, (*6)

    cp -R vendor/rgcouto/discounts/cloudoki ./, (*7)

  • On routes/web.php add the following line, (*8)

    Route::get('order/{order}', 'OrderController@processOrder');, (*9)

  • Run, (*10)

    php artisan make:controller OrderController, (*11)

  • Open OrderController.php and add the following functions, (*12)

     * Processes order by loading orders from file
     * @param $order
    public function processOrder($order)

        //Load Order
        $order = json_decode(file_get_contents(base_path('cloudoki/example-orders')."/order".$order.".json"));

        //Apply discounts
        $orderForCheckout = self::applyDiscounts($order);

        //Show end result
        echo json_encode($orderForCheckout);


    * Using a json representation of an order, determines the discount of the order
    * @param String $order
    * @return String mixed
    private function applyDiscounts($order)

         //Load customers data
         $customers = json_decode(file_get_contents(base_path('cloudoki/data')."/customers.json"));

         //Load products data
         $products = json_decode(file_get_contents(base_path('cloudoki/data')."/products.json"));

         //Associate costumer to order
         $order->customer = array_values(array_filter($customers,
             function ($customer) use ($order)
                 return $customer->id == $order->{'customer-id'};

         //Associate product for each item in order
         $newItems = array();
         foreach ($order->items as $item) {
             $item->product = array_values(array_filter($products,
                 function ($product) use ($item) {
                     return $product->id == $item->{'product-id'};
             array_push($newItems, $item);
         $order->items = $newItems;

         //Uses custom package to determine discount and returns an updated checkout order
         $returnOrder = new DiscountsCalculator($order);
         return $returnOrder->getOrderWithDiscount();


Open your browser and navigate to the following URL:, (*13)


(this link will calculate the discounts for the order located in cloudoki/example-data/order1.json, you can change the number in the end of the URL to check the discounts on the other files/orders.), (*14)

Discounts configuration

In the config folder of the package it is possible to find a discount criterion configuration file., (*15)

In this file there are 3 pre-configured types of discount: - [discountPercentageByRevenue] A customer who has already bought for over € 1000, gets a discount of 10% on the whole order. - [freeByAmountOfItemsOfSameCategory] For every products of category "Switches" (id 2), when you buy five, you get a sixth for free. - [percentageOnCheapestByAmountOfItemsOfSameCategory] If you buy two or more products of category "Tools" (id 1), you get a 20% discount on the cheapest product., (*16)

It is possible to add more discounts to the 3 types, just follow the structure instructed in the criterion.json file (_comment_[...] field)., (*17)


By querying an order, the discounts will be calculated, applied and a new order JSON object will be returned already with the discounts applied. However, this JSON object has a few changes in regard to the original one, namely in:, (*18)

  • Each item of each order can now have two new fields:, (*19)

    • discount: if present, with the discount percentage that was applied to the item;
    • free: if present, free items that are to be added to the order.
  • Each order can also have:, (*20)

    • discount-reasons: if present, stating clearly which and why the discounts were made;
    • discount: if present, the discount percentage made on the whole order.

The Versions

03/04 2017



A package to apply discounts on orders.

  Sources   Download


by Rafael Couto