Question
Customize the shopping cart program to add at least fifteen different products (think about any real life application shopping cart electronics items, books, consumables etc..).
Customize the shopping cart program to add at least fifteen different products (think about any real life application shopping cart electronics items, books, consumables etc..). Make sure you apply taxes to your items. You could also try to add a column of quantity next to the item. Write the changes that you have made under your program purpose.
class Product { private $productId; private $productName; private $price;
public function __construct( $productId, $productName, $price ) { $this->productId = $productId; $this->productName = $productName; $this->price = $price; }
public function getId() { return $this->productId; }
public function getName() { return $this->productName; }
public function getPrice() { return $this->price; }
}
$products = array( 1 => new Product( 1, "SuperWidget", 19.99 ), 2 => new Product( 2, "MegaWidget", 29.99 ), 3 => new Product( 3, "WonderWidget", 39.99 ) );
if ( !isset( $_SESSION["cart"] ) ) $_SESSION["cart"] = array();
if ( isset( $_GET["action"] ) and $_GET["action"] == "addItem" ) { addItem(); } elseif ( isset( $_GET["action"] ) and $_GET["action"] == "removeItem" ) { removeItem(); } else { displayCart(); }
function addItem() { global $products; if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3 ) { $productId = (int) $_GET["productId"];
if ( !isset( $_SESSION["cart"][$productId] ) ) { $_SESSION["cart"][$productId] = $products[$productId]; } }
session_write_close(); header( "Location: shopping_cart.php" ); }
function removeItem() { global $products; if ( isset( $_GET["productId"] ) and $_GET["productId"] >= 1 and $_GET["productId"] <= 3 ) { $productId = (int) $_GET["productId"];
if ( isset( $_SESSION["cart"][$productId] ) ) { unset( $_SESSION["cart"][$productId] ); } }
session_write_close(); header( "Location: shopping_cart.php" ); }
function displayCart() { global $products; ?>
Your shopping cart
- getName() ?>
- $getPrice(), 2 ) ?> getId() ?>">Remove
- Cart Total:
- $
getPrice(); ?>
Product list
- getName() ?>
- $getPrice(), 2 ) ?> getId() ?>">Add Item
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started