Write a php code for add to cart

Find out how to write PHP code to implement an "add to cart" feature, allowing users to select and save items for purchase in an online store.

Write a php code for add to cart
Manish Kumar Oct-29-2023 02:20:27
Viewed 381 times

1 Answer

1

Certainly, here's a basic example of a PHP code for adding items to a shopping cart. Please note that this is a simplified example, and in a real-world scenario, you'd likely use a database to store cart items and handle user sessions for a more robust e-commerce solution.


```php

<?php

session_start();


// Check if the "Add to Cart" button is clicked

if (isset($_POST['add_to_cart'])) {

    // Get the product ID and quantity from the form

    $product_id = $_POST['product_id'];

    $quantity = $_POST['quantity'];


    // Initialize the cart if it doesn't exist in the session

    if (!isset($_SESSION['cart'])) {

        $_SESSION['cart'] = array();

    }


    // Check if the product is already in the cart

    if (array_key_exists($product_id, $_SESSION['cart'])) {

        // If it is, update the quantity

        $_SESSION['cart'][$product_id] += $quantity;

    } else {

        // If it's not, add it to the cart

        $_SESSION['cart'][$product_id] = $quantity;

    }


    // Redirect to the shopping cart page or wherever you need to go

    header('Location: shopping_cart.php');

    exit;

}

?>


<!DOCTYPE html>

<html>

<head>

    <title>Add to Cart Example</title>

</head>

<body>

    <form method="post">

        <input type="hidden" name="product_id" value="1">

        <label for="quantity">Quantity:</label>

        <input type="number" name="quantity" value="1" min="1">

        <input type="submit" name="add_to_cart" value="Add to Cart">

    </form>

</body>

</html>

```


In this example:


1. When the user clicks the "Add to Cart" button, the `add_to_cart` form is submitted.


2. The PHP code at the top checks if the button was clicked and retrieves the product ID and quantity from the form.


3. It initializes or retrieves the shopping cart from the PHP session.


4. It checks if the product is already in the cart. If it is, it updates the quantity; otherwise, it adds the product to the cart.


5. Finally, it redirects the user to the shopping cart page (replace `"shopping_cart.php"` with the appropriate URL) after updating the cart.


This is a very basic example and does not include a proper product catalog or cart display, which you would typically implement in a real e-commerce application.

avatar
Cynthia
13 Ques 1 Ans
answered 29 Oct 2023

Your Answer

undraw-questions

Login or Create Account to answer this question.

Do you have any opinion about Write a php code for add to cart?

Login / Signup

Answers Adda Q&A communities are different.
Here's how

bubble
Knowledge sharing.

Question and answer communities are a great way to share knowledge. People can ask questions about any topic they're curious about, and other members of the community can provide answers based on their knowledge and expertise.

vote
Engagement and connection

These communities offer a way to engage with like-minded individuals who share similar interests. Members can connect with each other through shared experiences, knowledge, and advice, building relationships that extend beyond just answering questions..

check
Community building.

Answers Adda Question & Answer communities provide a platform for individuals to connect with like-minded people who share similar interests. This can help to build a sense of community and foster relationships among members.