Skip to content

Instantly share code, notes, and snippets.

@ahmedsayedabdelsalam
Created October 3, 2020 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedsayedabdelsalam/0cb83567cf97a995e0116838afbd3b19 to your computer and use it in GitHub Desktop.
Save ahmedsayedabdelsalam/0cb83567cf97a995e0116838afbd3b19 to your computer and use it in GitHub Desktop.
task.php
<?php
/**
* we have a cart with coupon and we need to calculate the discount amount
*
* Note:
* - the coupon support multiple conditions
* - the included and excluded are optional
*/
// Order Data
$order = new stdClass();
$order->total_cart = 175;
$order->shipping_cost = 50;
$order->customer_id = 101;
$order->cart_content = [
['product_id' => 1, 'product_name' => 'Book', 'product_price' => 5, 'category_id' => 10],
['product_id' => 2, 'product_name' => 'Pen', 'product_price' => 1, 'category_id' => 20],
['product_id' => 3, 'product_name' => 'Bag', 'product_price' => 120, 'category_id' => 30],
['product_id' => 4, 'product_name' => 'Notebook', 'product_price' => 35, 'category_id' => 40],
['product_id' => 5, 'product_name' => 'Pencil Case', 'product_price' => 14, 'category_id' => 50]
];
// Coupon Data
$coupon = new stdClass();
$coupon->type = 'percentage'; // fixed, percentage
$coupon->amount = 20; // Based on type
$coupon->end_date = strtotime("+1 day");
$coupon->minimum_amount = 100;
$coupon->free_shipping = false; // true, false
$coupon->included_categories = [10, 20];
$coupon->excluded_categories = [50];
$coupon->included_products = [3];
$coupon->excluded_products = [4, 1];
function calculate_coupon_discount($order, $coupon)
{
$discounted_amount = 0;
if ($coupon->end_date < strtotime('now')) {
return $discounted_amount;
}
if ($coupon->free_shipping) {
$discounted_amount += $order->shipping_cost;
}
$included_products = get_included_products($order, $coupon);
$total_price_of_included_products = array_sum(array_column($included_products, 'product_price'));
if ($total_price_of_included_products >= $coupon->minimum_amount) {
$discounted_amount += calculate_discount_amount($total_price_of_included_products, $coupon);
}
return $discounted_amount;
}
function calculate_discount_amount($price, $coupon)
{
return $coupon->type === 'percentage' ?
($price * $coupon->amount / 100) :
$coupon->amount;
}
function get_included_products($order, $coupon)
{
$included_products = [];
foreach ($order->cart_content as $product) {
if (in_array($product['category_id'], array_get($coupon->included_categories))) {
$included_products[$product['product_id']] = $product;
}
if (in_array($product['category_id'], array_get($coupon->excluded_categories))) {
unset($included_products[$product['product_id']]);
}
if (in_array($product['product_id'], array_get($coupon->included_products))) {
$included_products[$product['product_id']] = $product;
}
if (in_array($product['product_id'], array_get($coupon->excluded_products))) {
unset($included_products[$product['product_id']]);
}
}
return $included_products;
}
function array_get($array, $default = [])
{
return $array ?? $default;
}
echo calculate_coupon_discount($order, $coupon);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment