PHP shopping cart using OOP

Creating a shopping cart system using Object-Oriented Programming (OOP) in PHP involves organizing your code into classes and objects to achieve modularity, reusability, and maintainability. Below, I'll provide you with a basic example of how you can implement a simple shopping cart system using OOP principles in PHP.

Step 1: Create the Product Class

php code
class Product {
    private $id;
    private $name;
    private $price;

    public function __construct($id, $name, $price) {
        $this->id = $id;
        $this->name = $name;
        $this->price = $price;
    }

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getPrice() {
        return $this->price;
    }
}

Step 2: Create the Cart Class

php code
class Cart {
    private $items = [];

    public function addItem(Product $product, $quantity = 1) {
        $productId = $product->getId();

        if (isset($this->items[$productId])) {
            $this->items[$productId]['quantity'] += $quantity;
        } else {
            $this->items[$productId] = [
                'product' => $product,
                'quantity' => $quantity
            ];
        }
    }

    public function removeItem($productId) {
        if (isset($this->items[$productId])) {
            unset($this->items[$productId]);
        }
    }

    public function getTotal() {
        $total = 0;
        foreach ($this->items as $item) {
            $total += $item['product']->getPrice() * $item['quantity'];
        }
        return $total;
    }

    public function getItems() {
        return $this->items;
    }
}

Step 3: Usage Example

php code
// Creating products
$product1 = new Product(1, 'Product 1', 10.99);
$product2 = new Product(2, 'Product 2', 19.99);

// Creating a cart
$cart = new Cart();

// Adding products to the cart
$cart->addItem($product1, 2);
$cart->addItem($product2, 1);

// Removing a product from the cart
$cart->removeItem(1);

// Getting cart items and total
$cartItems = $cart->getItems();
$total = $cart->getTotal();

// Displaying cart contents and total
foreach ($cartItems as $item) {
    $product = $item['product'];
    $quantity = $item['quantity'];
    echo "Product: {$product->getName()}, Quantity: $quantity <br>";
}

echo "Total: $total";

In this example, the Product class represents a product with its ID, name, and price. The Cart class manages the shopping cart, allowing you to add and remove items, calculate the total price, and retrieve the cart items.

Remember that this is a basic example. In a real-world scenario, you might want to add more features like updating quantities, handling user sessions, persisting cart data, and integrating with a database to store product information.

Comments

Popular posts from this blog

WORDPRESS: Content optimization and keyword research

Rating system in PHP with MYSQL

Dependency Management: Using tools like Composer to manage dependencies in PHP projects.

Different types of SEO techniques

Caching mechanisms in MYSQL