<?php class Cart { protected array $cart = []; protected array $fees = []; public function __construct() { // generate test Cart $testProducts = [ ['id' => 1 , 'nb' => 22, 'price' => 23], ['id' => 383 , 'nb' => 38, 'price' => 19], ['id' => 412, 'nb' => 11, 'price' => 34], ]; foreach ($testProducts as $testProduct) { while ($testProduct['nb']-- > 0) { $this->addProduct($testProduct['id'], $testProduct['price']); } } } public function cart_contents(): array { return $this->cart; } public function addProduct(int $id, int $price) { $this->cart[] = ['product_id' => $id, 'price' => $price]; } public function add_fee(string $txt, float $price) { $this->fees[] = ['txt' => $txt, 'price' => $price]; } function apply_discount_for_quantity() { // For sample, I let you choose of to define the rules and send it here $discount_rules = [ ['eligible_product_ids' => [383, 411], 'required_count' => 12, 'discount_percent' => 4], ['eligible_product_ids' => [412], 'required_count' => 10, 'discount_percent' => 5], ]; // Will use counter per lligible product $discounts_counter_cache = []; // cycle through the products in the cart to count thecantidad de productos elegibles foreach ($this->cart_contents() as $product) { foreach ($discount_rules as $rule) { // if product is eligible to one of our discount rule if (!in_array($product['product_id'], $rule['eligible_product_ids'])) { // product not eligible for a discount continue; } // let's count of many product whe got $discounts_counter_cache[ $product['product_id'] ] = ($discounts_counter_cache[ $product['product_id'] ] ?? 0) + 1; // Apply per elligible product only when it's reach the limit if ($discounts_counter_cache[ $product['product_id'] ] == $rule['required_count']) { echo sprintf(' -- Found discount of %d%% to apply on the batch of %d product (id %d) in the cart' . PHP_EOL, $rule['discount_percent'], $rule['required_count'], $product['product_id'] ); // We trigger the discount $this->add_fee( 'Descuento por cantidad: ' . $rule['discount_percent'] .'% por '. $rule['required_count'] .'x'. $product['product_id'], // You can also set the value in the rule -($product['price'] * $rule['required_count'] * ($rule['discount_percent'] / 100)) ); // Reset counter $discounts_counter_cache[ $product['product_id'] ] = 0; } } } } public function show_cart() { $products = []; foreach ($this->cart as $entry) { if (!isset($products[$entry['product_id']])) { $products[$entry['product_id']] = [ 'id' => $entry['product_id'], 'nb' => 0, 'price' => $entry['price'], 'total' => $entry['price'], ]; } $products[$entry['product_id']]['nb']++; $products[$entry['product_id']]['price'] += $entry['price']; } echo 'Cart:'. PHP_EOL; foreach ($products as $product) { echo sprintf( '> Product Id:"%d" - %d$ x %d = %d$'.PHP_EOL, $product['id'], $product['price'], $product['nb'], $product['total'] ); } echo PHP_EOL; if (!empty($this->fees)) { echo 'Fees:'.PHP_EOL; $totalFees = ['nb' => 0, 'total' => 0]; foreach ($this->fees as $fee) { $totalFees['nb']++; $totalFees['total']+= $fee['price']; echo sprintf( '> %d$ - %s' . PHP_EOL, $fee['price'], $fee['txt'] ); } echo sprintf( '>> Total of %d discounts for %d$' . PHP_EOL, $totalFees['nb'], $totalFees['total'] ); } } } $cart = new Cart(); echo 'Before discount:'.PHP_EOL; echo $cart->show_cart(); $cart->apply_discount_for_quantity(); echo PHP_EOL.'After discount:'.PHP_EOL; echo $cart->show_cart();
You have javascript disabled. You will not be able to edit any code.