Hardik Sisodia
Hardik Sisodia

Reputation: 635

Adding product quantity to cart in php only if product is available in cart with same color and size

I've created php ekart site just for testing purpose. My query is when I click on add to cart it should firstly validate whether any one color and any one size is selected, if both have been selected it adds product to the cart this works fine but if I add same product but with different color or size it overlaps the same product which was already added and that products quantity turns to 1 but if I select another product it works well but same thing goes when I select different size or color. Since I am a beginner at php, I'm not sure where am I going wrong here.

Php code of Product add to cart is given Below:

$sql = "SELECT * FROM allprods WHERE listing = '$listing' and  id = '$id'";
$data = mysql_query($sql);
if (mysql_num_rows($data) == 1)
{
    $row = mysql_fetch_array($data);
    if (isset($_SESSION['cart'][$id]))
    {
        if(isset($_SESSION['cart'][$id][$color]) && isset($_SESSION['cart'][$id][$size]))
        {
            $_SESSION['cart'][$id]['quantity']++;
            echo "<script>alert('".$row['product_name']." has been added to your cart.');</script>";
        }
        else
        {
            $_SESSION['cart'][$id] = array('quantity' = >1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);
            echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
        }
    }
    else
    {
        $_SESSION['cart'][$id] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);

        echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
    }
}

Cart program code

<?php
    if(isset($_POST['qty']))
    {
        foreach($_POST['qty'] as $product_id=>$item_qty)
        {
            $id=(int)$product_id;
            $qty=(int)$item_qty;
            if($qty==0)
            {
                unset($_SESSION['cart'][$id]);
            }
            elseif($qty>0)
            {
                $_SESSION['cart'][$id]['quantity']=$qty;
            }
        }
    }
    else
    {
        echo "";
    }
    if(!empty($_SESSION['cart']))
    {
        require "../link_db.php";
        $sql="SELECT * FROM allprods WHERE id IN(";
        foreach($_SESSION['cart'] as $id=>$value)
        {
            $sql.=$id.',';
        }
        $sql=substr($sql,0,-1).') ORDER BY product_id ASC'; 
        $data=mysql_query($sql);
        while($row=mysql_fetch_array($data))
        {
?>
Table row and data goes here!!
<?php
        }
        else
        {
?>
<tr>
    <th colspan='4' class='noprodavailablewrap'>
        There are no products in your cart.
    </th>
</tr>
<?php
    }
?>

DB structure given below: Image of database structure Image of database structure

Upvotes: 0

Views: 2012

Answers (1)

Zebi Rajpot
Zebi Rajpot

Reputation: 186

Use this code

<?php

$sql = "SELECT * FROM allprods WHERE listing = '$listing' and  id = '$id'";
$data = mysql_query($sql);
if (mysql_num_rows($data) == 1)
{
    $row = mysql_fetch_array($data);
    $index = md5($id.$color.$size);
    if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
        $_SESSION['cart'][$index]['quantity']++;
        echo "<script>alert('".$row['product_name']." has been added to your cart.');</script>";
    }else{
        $_SESSION['cart'][$index] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);
        echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
    }
}

?>

Upvotes: 1

Related Questions