PHP LogoE-commerce Systems

E-commerce systems refer to the software applications and platforms that enable businesses and individuals to buy and sell goods and services over the internet. These systems facilitate online transactions, manage product catalogs, handle customer data, process payments, and often include functionalities for order fulfillment, inventory management, marketing, and customer support.

Key components and functionalities of E-commerce Systems typically include:

1. Product Catalog Management: Allows businesses to add, update, and categorize products, including details like descriptions, images, prices, and stock levels.
2. Shopping Cart: Enables customers to select products they wish to purchase and store them temporarily before checkout.
3. User Account Management: Provides functionalities for customer registration, login, profile management, order history viewing, and address book management.
4. Checkout Process: Guides customers through the final steps of placing an order, including selecting shipping methods, entering billing and shipping information, and confirming the purchase.
5. Payment Gateway Integration: Securely processes online payments through various methods (credit cards, PayPal, bank transfers, etc.) by integrating with third-party payment processors.
6. Order Management: Allows businesses to track orders from placement to delivery, update order statuses, and handle returns or cancellations.
7. Inventory Management: Keeps track of product stock levels, preventing overselling and ensuring timely restocking.
8. Search and Navigation: Provides tools for customers to easily find products through search bars, filters, and categorized navigation menus.
9. Marketing and Promotions: Includes features for discounts, coupons, promotions, email marketing, and SEO optimization.
10. Reporting and Analytics: Generates reports on sales, customer behavior, inventory, and other key metrics to help businesses make informed decisions.

E-commerce systems can range from simple self-hosted solutions to robust enterprise-level platforms or cloud-based SaaS offerings (e.g., Shopify, WooCommerce, Magento). They are crucial for modern businesses looking to expand their reach and offer convenient purchasing options to customers worldwide.

Example Code

<?php

class Product {
    public $id;
    public $name;
    public $price;
    public $stock;

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

class ShoppingCart {
    private $items = [];

    public function addItem(Product $product, $quantity = 1) {
        if ($quantity <= 0) {
            echo "Quantity must be positive.<br>";
            return false;
        }
        if ($product->stock < $quantity) {
            echo "Not enough stock for '{$product->name}'. Available: {$product->stock}<br>";
            return false;
        }

        if (isset($this->items[$product->id])) {
            $this->items[$product->id]['quantity'] += $quantity;
        } else {
            $this->items[$product->id] = [
                'product' => $product,
                'quantity' => $quantity
            ];
        }
        $product->stock -= $quantity; // Simulate stock reduction
        echo "Added {$quantity} x '{$product->name}' to cart.<br>";
        return true;
    }

    public function removeItem($productId) {
        if (isset($this->items[$productId])) {
            $product = $this->items[$productId]['product'];
            $quantity = $this->items[$productId]['quantity'];
            $product->stock += $quantity; // Return stock
            unset($this->items[$productId]);
            echo "Removed '{$product->name}' from cart.<br>";
        } else {
            echo "Product not found in cart.<br>";
        }
    }

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

    public function displayCart() {
        if (empty($this->items)) {
            echo "<br>Your cart is empty.<br>";
            return;
        }
        echo "<br>--- Shopping Cart ---<br>";
        foreach ($this->items as $item) {
            echo "- " . $item['product']->name . " (Price: $" . $item['product']->price . ") x " . $item['quantity'] . " = $" . ($item['product']->price * $item['quantity']) . "<br>";
        }
        echo "Total: $" . $this->getTotal() . "<br>";
        echo "--------------------<br>";
    }

    public function checkout() {
        if (empty($this->items)) {
            echo "<br>Cannot checkout. Your cart is empty.<br>";
            return false;
        }
        echo "<br>Processing checkout for total: $" . $this->getTotal() . "...<br>";
        // In a real system, this would involve:
        // 1. Payment gateway integration
        // 2. Order persistence to a database
        // 3. Emailing order confirmation
        // 4. Clearing the cart
        $this->items = []; // Clear cart after successful checkout
        echo "Order placed successfully! Cart cleared.<br>";
        return true;
    }
}

// --- Usage Example ---

// 1. Define some products
$product1 = new Product(101, "Laptop Pro X", 1200.00, 5);
$product2 = new Product(102, "Wireless Mouse", 25.50, 20);
$product3 = new Product(103, "USB-C Hub", 50.00, 10);

// 2. Create a shopping cart
$cart = new ShoppingCart();

// 3. Add items to the cart
$cart->addItem($product1, 1);
$cart->addItem($product2, 2);
$cart->addItem($product3, 1);
$cart->addItem($product1, 1); // Add another Laptop

// Try to add more than available stock
$cart->addItem($product1, 5); // Should fail

// 4. Display current cart contents
$cart->displayCart();

// 5. Remove an item
$cart->removeItem(102);

// 6. Display cart again
$cart->displayCart();

// 7. Proceed to checkout
$cart->checkout();

// 8. Try to display cart after checkout
$cart->displayCart();

// Output current stock levels (for demonstration of stock management)
echo "<br>Remaining Stock:<br>";
echo "{$product1->name}: {$product1->stock}<br>";
echo "{$product2->name}: {$product2->stock}<br>";
echo "{$product3->name}: {$product3->stock}<br>";

?>