Home  >  Article  >  Backend Development  >  PHP Mall Function Tutorial: Implementing Shopping Cart and Order Synchronization Function

PHP Mall Function Tutorial: Implementing Shopping Cart and Order Synchronization Function

WBOY
WBOYOriginal
2023-07-30 10:30:30949browse

PHP Mall Function Tutorial: Implementing Shopping Cart and Order Synchronization Function

In a complete e-commerce website, the shopping cart is one of the essential functions. The shopping cart provides users with a convenient space to purchase goods and temporarily store goods. The order is an important part of the shopping process and is the final step for users to confirm the purchase of goods. This article will introduce how to use PHP to implement shopping cart and order synchronization functions.

  1. Create a shopping cart database table

First, we need to create a shopping cart database table. This table should contain the following fields:

  • id: the unique identifier of the shopping cart record
  • user_id: the ID of the user who owns the shopping cart
  • product_id: the ID of the product
  • quantity: the quantity of the product
  • created_at: the time when the shopping cart record was created
  • updated_at: the time when the shopping cart record was last updated

Yes Use the following SQL statement to create a shopping cart table:

CREATE TABLE `cart` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `quantity` int(11) NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. Add items to the shopping cart

When the user clicks the "Add to Shopping Cart" button, we need to add the corresponding item's Information is added to the shopping cart table. We can use the following code example to achieve this:

<?php
// 获取用户ID和商品ID
$user_id = $_SESSION['user_id'];
$product_id = $_POST['product_id'];

// 检查购物车中是否已存在该商品记录
$query = "SELECT * FROM cart WHERE user_id = $user_id AND product_id = $product_id";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) == 0) {
    // 若购物车中不存在该商品记录,则添加一条新记录
    $query = "INSERT INTO cart (user_id, product_id, quantity, created_at, updated_at) VALUES ($user_id, $product_id, 1, NOW(), NOW())";
} else {
    // 若购物车中已存在该商品记录,则更新数量
    $query = "UPDATE cart SET quantity = quantity + 1, updated_at = NOW() WHERE user_id = $user_id AND product_id = $product_id";
}

mysqli_query($connection, $query);
?>
  1. Synchronize shopping cart and order

When the user enters the order confirmation page, we need to synchronize the product information in the shopping cart to the order form. You can use the following code example:

<?php
// 获取用户ID
$user_id = $_SESSION['user_id'];

// 获取购物车中的商品信息
$query = "SELECT * FROM cart WHERE user_id = $user_id";
$result = mysqli_query($connection, $query);

while($row = mysqli_fetch_array($result)) {
    $product_id = $row['product_id'];
    $quantity = $row['quantity'];
    
    // 将购物车中的商品信息添加至订单表中
    $query = "INSERT INTO orders (user_id, product_id, quantity, created_at) VALUES ($user_id, $product_id, $quantity, NOW())";
    
    mysqli_query($connection, $query);
}

// 清空购物车
$query = "DELETE FROM cart WHERE user_id = $user_id";
mysqli_query($connection, $query);
?>

Through the above code example, we can enable the user to add products to the shopping cart, and then synchronize the product information in the shopping cart to the order table.

Summary:

The shopping cart and order synchronization function is one of the necessary functions in e-commerce websites. Through the code examples provided in this article, we can use PHP to implement the synchronization function of the shopping cart and orders, thereby providing users with a better shopping experience. Of course, the above example is just a simplified implementation, and the actual situation may be more complex. Specific requirements and business logic may require additional code and database table design to complete.

The above is the detailed content of PHP Mall Function Tutorial: Implementing Shopping Cart and Order Synchronization Function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn