Current Location: Home> Latest Articles> How to Build a Shopping List Feature for WeChat Mini Programs with PHP

How to Build a Shopping List Feature for WeChat Mini Programs with PHP

M66 2025-10-27

How to Build a Shopping List Feature for a WeChat Mini Program with PHP

With the rapid growth of mobile internet, WeChat Mini Programs have become an essential part of many users' daily lives. Among their many practical features, the shopping list function stands out as both useful and widely applicable. This article provides a detailed guide on how to create a shopping list feature for a WeChat Mini Program using PHP, complete with code examples.

Preparation

Before starting development, you need to set up the following environment:

  • WeChat Developer Tools: Used for Mini Program development and debugging.
  • PHP environment: Used to handle backend data and build APIs.

Database Design

The shopping list feature requires storing user shopping data, so we need to design a suitable database structure. Key tables include:

  • User information: stores user ID, username, etc.
  • Product information: stores product ID, name, price, etc.
  • Shopping cart information: stores cart ID, user ID, product ID, and quantity.

MySQL or another relational database can be used to store this data.

Backend API Implementation

Get Product List API

On the server side, we need to create an API that retrieves the product list and returns it in JSON format to the Mini Program.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query the product list
$sql = "SELECT * FROM products";
$result = $conn->query($sql);
$products = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $products[] = $row;
    }
}

// Return the results
echo json_encode($products);
$conn->close();
?>

Add Product to Shopping Cart API

We also need an API to allow the Mini Program to add items to the shopping cart.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Get parameters
$user_id = $_POST['user_id'];
$product_id = $_POST['product_id'];
$quantity = $_POST['quantity'];

// Add product to cart
$sql = "INSERT INTO shopping_cart (user_id, product_id, quantity) VALUES ('$user_id', '$product_id', '$quantity')";
if ($conn->query($sql) === TRUE) {
    echo "Added successfully";
} else {
    echo "Add failed";
}

$conn->close();
?>

Calling the APIs in the Mini Program

Retrieve and Display Product List

In the Mini Program frontend, you can call the backend API to get and display all products:

wx.request({
  url: 'http://localhost/products.php',
  success: function(res) {
    let productList = res.data;
    // Display product list...
  }
})

Add Product to Cart

Similarly, call the backend API to add selected products to the shopping cart:

wx.request({
  url: 'http://localhost/addToCart.php',
  method: 'POST',
  data: {
    user_id: '123',
    product_id: '456',
    quantity: '1'
  },
  success: function(res) {
    let result = res.data;
    // Handle response...
  }
})

By following the steps above, you can build a fully functional shopping list feature for a WeChat Mini Program using PHP. Depending on your specific needs and server environment, you can further optimize and extend the code to improve performance and maintainability.