Jump to content

Making multiple products show in a cart


jimleeder123

Recommended Posts

I can show a single product in a cart, but unsure how to show multiple products.

 

I have a cart on my front page. Categories are shown by PHP/mysql, when you choose a category its products are then shown. each product has an "add to cart" button, which leads to addtocart.php. The php code for this file is below:

 

--------------------------------------------------

 

session_start();
    unset ($_SESSION['.$product.']);
    unset ($_SESSION['.$total.']);
    
    $product = $_POST['hiddenid'];
    $child = $_POST['child'];
    $group = $_POST['attributes'];
    
    $total = array('id' => $product, 'child' => $child, 'attributes' => $group);
    
      $_SESSION['id'] = $total['id']; //Parent product ID, eg Margherita
      $_SESSION['child'] = $total['child']; //child product eg 8" Margherita
      $_SESSION['attributes'] = $total['attributes'];

    
    $message = "You have added a product to the cart. You will be redirected to the category page.";
    echo "<script type='text/javascript'>alert('$message');
    window.location.href = 'shop.php';</script>";

 

--------------------------------------------------------------

 

Then on the front page, I show the cart in an include file. The cart php code is as follows:

 

-------------------------------------

 

echo "<div id='cartwrap'>";
        echo "<div id='cart'>";
            
            echo "<div id='carttitlewrap'>";    
                echo "<span id='carttitle'>Cart</span>";
            echo "</div>";
            
            echo "<br /> <br />";        
                        
            if (!isset($_SESSION['id'])) {
                                                            
                echo "nothing here";    
                                                                
            }else{        
                    
                $id = $_SESSION['id'];
                $child = $_SESSION['child'];
                $group = $_SESSION['attributes'];

                echo "Main Product ID: ";                                
                echo '<span>'.$id.'</span>';
                echo "<br />";
                echo "<br />";
                
                echo "Child Product: ";
                  echo '<span>'.$child.'</span>';
                echo "<br />";
                echo "<br />";
                
                echo "Attribute Option: ";
                  echo '<span>'.$group.'</span>';
                                                                
            }//end of if to check if products in cart
                                                                
        echo "</div>";                                                                                                    
    echo "</div>";

 

----------------------------------------------------------

 

So I want to know how to add multiple products to the cart. The above code shows data for a single product in the cart, but not multiple.

Link to comment
https://forums.phpfreaks.com/topic/296504-making-multiple-products-show-in-a-cart/
Share on other sites

Update: I have got it working putting a single one in as an array, but when I append arrays (using stac overflow help) it comes up with the data in the first array, and then "array array array..." and so on. If i do a var_dump on it I can see the data. But obviously I need to echo it out.

you would use a multi-dimensional array.

 

firstly, you should separate the cart session data from other session data by giving it a primary array associative index name - $_SESSION['cart']

 

secondly, your id should uniquely identify an item, it should be the auto-increment id from your database table where you have defined the items, and the id should be used as the next array dimension's index - $_SESSION['cart'][id_value]

 

lastly, to allow multiple of any item to be stored in a cart, the value stored should be the quantity - $_SESSION['cart'][id_value] = quantity

 

to add a quantity of one (1) to the cart for any id, the basic code would look like - 

session_start();

if(!isset($_SESSION['cart'])){
    $_SESSION['cart'] = array(); // create an empty cart
}

// form processing code
if($_SERVER['REQUEST_METHOD'] == 'POST'){

    // add quantity one to cart
    if(isset($_POST['add'])){
        // validate/cast input data
        $id = (int)$_POST['hiddenid'];

        // the validation logic will produce a 'safe' $id to use
        if(!isset($_SESSION['cart'][$id])){
            $_SESSION['cart'][$id] = 0; // create an empty item (it will be incremented next)
        }
        $_SESSION['cart'][$id]++; // increment - add one to whatever the existing quantity is
    }
    
    // code for other form processing operations would go here....
    
}

I have since fixed this, got it to work with array_push and a few if statements. Code below for addtocart.php

 

session_start();
    
    unset ($_SESSION['.$product.']);
    unset ($_SESSION['.$total.']);
    //unset ($_SESSION['cart']);
    
    $id = $_POST['hiddenid'];
    $name = $_POST['hiddenname'];
    $child = $_POST['child'];
    
    if (!isset($_SESSION['cart'])) { //if cart is empty
    
        if ($child != "none"){ //if child product has been selected
        
            $_SESSION['cart'] = array($child);
            
        }else{ //if the child product has not been selected show the main product name (product won't have a child product)
            
            $_SESSION['cart'] = array($name);
        }
        
    }else{ //if cart already has 1 or more items in it
        
        if ($child != "none"){
        
            array_push($_SESSION['cart'], ''.$child.''); //if there is already an item in the cart, add a new additional one
        }else{
            
            array_push($_SESSION['cart'], ''.$name.'');
        }
    }

    $message = "You have added a product to the cart. You will be redirected to the category page.";
    echo "<script type='text/javascript'>alert('$message');
    window.location.href = 'shop.php';</script>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.