Project final - Shopping cart
Combine all parts and finalize a working shopping cart.
Project final - Shopping cart
The cart app is almost done: you added products, listed them and computed the total. The last step is to apply a discount, like real-world carts do.
Most shops give a discount above a certain amount. You'll apply a 10% discount with if ($total > 100): $discount = $total * 0.1. Then you show the amount due as $total - $discount.
Computing the amount due with a discount
The discount logic is a simple if/else. When the condition holds, $discount is set to 10% of the total; otherwise it is set to 0. This way, even without a discount, $total - $discount computes correctly.
The final output line shows the customer the net amount: echo "Due: " . ($total - $discount) . " USD";. The parentheses matter: subtraction must run before the dot (concatenation) operator.
Exercise Apply the discount and print the amount due
For the final step, let's apply a discount. If $total is greater than 100, assign $discount to 10% of the total (that is, $total * 0.1); otherwise assign 0. Then print Due: <amount-due> USD, where the amount due is $total - $discount (e.g. Due: 108 USD).
Show example solution
" . ($total - $discount) . " USD";.
Example solution:
<?php
// Initialize the cart array and add the first product
$cart = [];
$cart[] = ["name" => "Pen", "price" => 25, "quantity" => 2];
// Add two more products with array_push
array_push($cart, ["name" => "Notebook", "price" => 40, "quantity" => 1], ["name" => "Eraser", "price" => 10, "quantity" => 3]);
echo "Cart count: " . count($cart) . "\n";
// List the cart with foreach
foreach ($cart as $product) {
echo $product["name"] . " - " . $product["price"] . " USD x" . $product["quantity"] . "\n";
}
// Compute the total
$total = 0;
foreach ($cart as $product) {
$total += $product["price"] * $product["quantity"];
}
echo "Total: " . $total . " USD\n";
// Apply the discount
if ($total > 100) {
$discount = $total * 0.1;
} else {
$discount = 0;
}
echo "Due: " . ($total - $discount) . " USD";
Project complete
The shopping cart app is complete. Over five lessons you have:
- created an empty array and added products
- added several products with
array_push - listed the cart with
foreach - computed the total with an accumulator
- applied a discount and shown the amount due
These techniques work the same in real projects: product catalogs, order summaries, stock tracking. In the next groups you'll dig deeper into arrays.
Test yourself
-
What should be the last step of the cart app?
- Decorate the cart with colors
- Apply a discount for large carts
- Print product names in uppercase
- Open the cart in a browser
Show answer
Apply a discount for large carts — Correct. A discount is the last step of a real cart: when the total crosses a threshold the customer gets a discount.
-
Match the discount concepts
- if ($total > 100)
- discount threshold
- $discount = $total * 0.1
- 10% discount
- $total - $discount
- amount due
- else
- no discount case
-
Complete the discount condition
if (____) { $discount = $total * 0.1; } else { $discount = 0; }Show answer
if ($total > 100) { $discount = $total * 0.1; } else { $discount = 0; }