Current Location: Home> Latest Articles> PHP E-commerce Tutorial: Implementing Product Wishlist and Sharing Features

PHP E-commerce Tutorial: Implementing Product Wishlist and Sharing Features

M66 2025-06-21

PHP E-commerce Tutorial: Implementing Product Wishlist and Sharing Features

In modern e-commerce, product wishlist and sharing features have become essential tools for increasing user engagement and sales. In this tutorial, we will guide you through implementing these features in a PHP-based e-commerce website. Whether it’s to improve user retention or increase product exposure, these features will enhance the overall user experience.

1. Implementing the Product Wishlist Feature

1.1 Create the Database Table

First, you need to create a database table to store user product wishlist information. Below is the SQL statement to create the “collections” table:

CREATE TABLE collections (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT,
  product_id INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

1.2 Add the Wishlist Button

In the product details page, you need to provide a button for users to add a product to their wishlist. Add the following HTML code:

<button id="collectBtn" data-productid="<?php echo $product_id; ?>">Add to Wishlist</button>

1.3 Add JavaScript Code

Using JavaScript and jQuery, we connect the wishlist button to the backend logic. When the button is clicked, the user’s action is sent to the backend for processing.

$(document).ready(function() {
  $("#collectBtn").click(function() {
    var productId = $(this).data("productid");
    $.ajax({
      type: "POST",
      url: "collect.php",
      data: { product_id: productId },
      success: function(response) {
        if(response.status == "success") {
          alert("Product has been successfully added to your wishlist!");
        } else {
          alert("Failed to add product to wishlist, please try again later.");
        }
      }
    });
  });
});

1.4 Create the Backend Wishlist Handler

On the server-side, create a file called “collect.php” to handle the wishlist button click. Here’s an example of the backend code:

<?php
$pdo = new PDO("mysql:host=localhost;dbname=db_name", "username", "password");
$userId = $_SESSION["user_id"];
$productId = $_POST["product_id"];
$sql = "INSERT INTO collections (user_id, product_id) VALUES (?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$userId, $productId]);
echo json_encode(["status" => "success"]);
?>

2. Implementing the Product Sharing Feature

2.1 Create the Database Table

Similar to the wishlist feature, the sharing feature also requires a database table to store shared product information. Below is the SQL statement to create the “shares” table:

CREATE TABLE shares (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_id INT,
  product_id INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

2.2 Add the Share Button

In the product details page, you need to provide a button for users to share a product. Add the following HTML code:

<button id="shareBtn" data-productid="<?php echo $product_id; ?>">Share</button>

2.3 Add JavaScript Code

Similar to the wishlist feature, we use JavaScript and jQuery to connect the share button to the backend logic.

$(document).ready(function() {
  $("#shareBtn").click(function() {
    var productId = $(this).data("productid");
    $.ajax({
      type: "POST",
      url: "share.php",
      data: { product_id: productId },
      success: function(response) {
        if(response.status == "success") {
          alert("Product has been successfully shared!");
        } else {
          alert("Failed to share product, please try again later.");
        }
      }
    });
  });
});

2.4 Create the Backend Share Handler

On the server-side, create a file called “share.php” to handle the product sharing action. Below is an example of the backend code:

<?php
$pdo = new PDO("mysql:host=localhost;dbname=db_name", "username", "password");
$userId = $_SESSION["user_id"];
$productId = $_POST["product_id"];
$sql = "INSERT INTO shares (user_id, product_id) VALUES (?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$userId, $productId]);
echo json_encode(["status" => "success"]);
?>

3. Conclusion

Through this tutorial, you have learned how to implement product wishlist and sharing features in a PHP-based e-commerce website. These features will enhance user experience, increase user engagement, and boost product exposure. By combining PHP and JavaScript, you can further customize and expand these functionalities based on your business needs.