pastebin

Paste Search Dynamic
Recent pastes
discounted_amount
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6.  
  7. // Order Data
  8. $order                        = new stdClass();
  9. $order->total_cart            = 175;
  10. $order->shipping_cost         = 50;
  11. $order->customer_id           = 101;
  12. $order->cart_content          = [
  13.  
  14.         ['product_id' => 1, 'product_name' => 'Book', 'product_price' => 5, 'category_id' => 10],
  15.         ['product_id' => 2, 'product_name' => 'Pen', 'product_price' => 1, 'category_id' => 20],
  16.         ['product_id' => 3, 'product_name' => 'Bag', 'product_price' => 120, 'category_id' => 30],
  17.         ['product_id' => 4, 'product_name' => 'Notebook', 'product_price' => 35, 'category_id' => 40],
  18.     ['product_id' => 5, 'product_name' => 'Pencil Case', 'product_price' => 14, 'category_id' => 50]
  19. ];
  20.  
  21.  
  22. // Coupon Data
  23. $coupon                        = new stdClass();
  24. $coupon->type                  = 'percentage';         // fixed, percentage
  25. $coupon->amount                = 20;                   // Based on type
  26. $coupon->end_date              = strtotime("+1 day");
  27. $coupon->minimum_amount        = 100;
  28. $coupon->free_shipping         = false;                // true, false
  29. $coupon->included_categories   = [10, 20];
  30. $coupon->included_products     = [3];
  31. $coupon->excluded_categories   = [50];
  32. $coupon->excluded_products     = [4, 1];
  33.  
  34. ///////////////
  35.  
  36. function filter_included_data($order,$coupon){
  37.  
  38.  
  39.         $filtered_collection= array_filter($order->cart_content, function ($item) use ($coupon) {
  40.                 if(in_array($item['category_id'], $coupon->included_categories) || in_array($item['product_id'], $coupon->included_products)){
  41.                   return true;
  42.  
  43.                 }
  44.         });
  45.  
  46.         return $filtered_collection;
  47. }
  48.  
  49. /////////
  50.  
  51. function filter_excluded_data($order,$coupon){
  52.  
  53.  
  54.         $filtered_excluded= array_filter($order->cart_content, function ($item) use ($coupon) {
  55.                 if(in_array($item['category_id'] , $coupon->excluded_categories) || in_array($item['product_id'] , $coupon->excluded_products)){
  56.                   return true;
  57.  
  58.                 }
  59.         });
  60.  
  61.         return $filtered_excluded;
  62. }
  63.  
  64.  
  65. //////////
  66. function calculate($order, $coupon) {
  67.  
  68.         if($coupon->type == 'percentage'){
  69.  
  70.                 $discounted_amount =  ($coupon->amount * $order->total_cart) / 100  ;
  71.                 return $discounted_amount;
  72.         }
  73. }
  74.  
  75. echo  count(filter_included_data($order,$coupon));
  76. echo count("\n",filter_excluded_data($order,$coupon));
  77.  
  78. echo 'your discounted is  ' .calculate($order, $coupon);
  79.  
  80.  
  81. ?>
Parsed in 0.093 seconds