skip to Main Content

I have singlehandedly tried to find a solution to this problem which I encountered about a week ago and to be honest I have tried different ways to tackle this problem but all efforts proved futile as they led to another problem entirely.

I am trying to implement a feature that increases product quantity if the product size already exists in the cart and the user clicks on the add to cart button, but for some reason when I add an item to the cart and the product size exists it adds a new line rather than update the quantity of the product in the cart.

I have different sizes for each products so let’s say I added size 32 of the product with 1qty to the cart and I add size 32 of that same product to the cart again with 3qtys I want it to increment the quantity of the product by 3(which is 4) and not add it to a new line.

This is the page that is handling the cart processing.

<?php session_name("grind"); session_start();

ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL);



if (isset($_SESSION['cart'])) { $checker=array_column($_SESSION['cart'],'size','item_id');


//I know the if(in_array) is not working which is why it executes the else statement that adds the item on a new line but I don't understand why, any explanation with the use of code will be appreciated.

if (in_array($_GET['cart_id'], $checker) && in_array($_POST['size'],$checker)) {


    foreach($_SESSION['cart'] as $key => $value){
        if($value['item_id']==$_GET['cart_id'] && $value['size']==$_POST['size'] && ($_SESSION['cart'][$key]['quantity'] + $_POST['quantity'] > 5))

//checking if the item_id in the cart is the same as the one in the url using the $_GET variable, check if the product size in the cart is equal to the one that just got added to the cart and if the sum of the cart quantity and the quantity that just got added is greater than 5(if the quantity is > than 5 set the cart quantity of that product size to 5).
        {
            
            $_SESSION['cart'][$_POST['size']]['quantity'] = 5;
            echo "<script>
            alert('only 5 items available for this product');
            window.location.href='grind.php';
            </script>";
        }

//else if the following is true, increment the quantity by the number of quantity added to the cart e.g if 2 is item quantity in the session and the user adds 4 the updated quantity will be 6.
        else if($value['item_id']==$_GET['cart_id'] && $value['size']==$_POST['size'] && ($_SESSION['cart'][$key]['quantity'] + $_POST['quantity'] <= 5)){
            
            $_SESSION['cart'][$key]['quantity'] +=$_POST['quantity'];

            echo 
            "<script>
            alert('quantity updated');
            window.location.href='grind.php';
            </script>";
        }
    
        
         
    }
    
    
     

}

else{

$count=count($_SESSION['cart']);
$_SESSION['cart'][$count]=array('item_id' => $_GET['cart_id'], 'item_name'=>$_GET['cart_name'],'item_picture'=>$_GET['cart_picture'], 'item_price'=>$_GET['cart_price'],'size'=>$_POST['size'], 'quantity'=>$_POST['quantity']);
echo "<script>alert('product added');
window.location.href='grind.php';
</script>";
     }

}

 else{ $_SESSION['cart'][0]=array('item_id'=>$_GET['cart_id'], 'item_name'=>$_GET['cart_name'], 'item_picture'=>$_GET['cart_picture'],'item_price'=>$_GET['cart_price'],'size'=>$_POST['size'], 'quantity'=>$_POST['quantity']); echo "<script>alert('product added'); window.location.href='grind.php'; </script>"; 
}

?>

2

Answers


  1. Chosen as BEST ANSWER

    Alright, I am just putting my solution out here in case someone else comes across a similar problem, I was able to resolve this problem after employing the use of the ULTIMATE "var_dump" with the help of a friend. The issue I had was in

    $checker=array_column($_SESSION['cart'],'size','item_id');

    The reason I kept getting the same product with same size on a new line rather than incrementing the quantity is because the 'item_id' is the same whenever that block of code runs so it just picks one of them and adds it to a new line.

    I had to replace item_id with custom_item_id which I just created and added it to the cart array(refer to the else statement below to see how I added it to the cart). $checker=array_column($_SESSION['cart'],'size','custom_item_id');

    After my foreach loop I created two variable names, see them below

    $custom_item_id=$value['item_id'].$value['size'];//expected output is item_id.size e.g 243(2 is the item_id and 43 is the size)
    $custom_cart_id=$_GET['cart_id'].$_POST['size'];
    

    The reason for the code above is to create a unique id for the same item with different sizes that is being added to the cart even as they may have the same item id. So, in the if statement block the $value['item_id']==$_GET['cart_id'] will be replaced with $custom_item_id==$custom_cart_id

    replace $_SESSION['cart'][$_POST['size']]['quantity'] = 5; with $_SESSION['cart'][$key]['quantity'] = 5;

    In the else statement of the cart I have to add the new key('custom_item_id') and value which I just created as a unique id for each item added to the cart.

    else{
    
    $count=count($_SESSION['cart']);
    $_SESSION['cart'][$count]=array('item_id' => $_GET['cart_id'], 'item_name'=>$_GET['cart_name'],'item_picture'=>$_GET['cart_picture'], 'item_price'=>$_GET['cart_price'],'size'=>$_POST['size'], 'quantity'=>$_POST['quantity'],custom_item_id=>$_GET['cart_id'].$_POST['size']);
    echo "<script>alert('product added');
    window.location.href='grind.php';
    </script>";
         }
    

    Do the same for the other else statement.


  2. I can’t really test it but here’s what I’ve come up with according to the logic I read.

    In short we use a for loop to check if an item with the same id and size is in the cart and if it is we save the array key in a variable. Then if it found something we update the quantity and then clamp it down to 5 if its over that.

    Hope this helps or gives you some idea of how to do it.

    <?php
    
    session_name("grind");
    session_start();
    
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    
    
    if (isset($_SESSION['cart'])) {
        //Check if the item with same size has already been added to the cart
        $itemWithSameSizeIsInCart = false;
        foreach ($_SESSION['cart'] as $key => $value) {
            if ($value['item_id'] == $_GET['cart_id'] && $value['size'] == $_POST['size']) {
                $itemWithSameSizeIsInCart = $key;
                break;
            }
        }
    
        //If item is in cart and size is the same do this and return
        if ($itemWithSameSizeIsInCart !== false) {
            $workingItem = $_SESSION['cart'][$itemWithSameSizeIsInCart];
    
            //Add posted quantity to item
            $workingItem['quantity'] = $workingItem['quantity'] += $_POST['quantity'];
            $alertMsg = "alert('product added');";
    
            //Check if quantity is greater than 5
            if ($workingItem['quantity'] > 5) {
                $workingItem['quantity'] = 5;
                $alertMsg = "alert('only 5 items available for this product');";
            }
    
            echo "<script>$alertMsg window.location.href='grind.php'; </script>";
            return;
        }
    
        //Default behaviour if item not in cart, add item to cart
        $count = count($_SESSION['cart']);
        $_SESSION['cart'][$count] = array(
            'item_id' => $_GET['cart_id'],
            'item_name' => $_GET['cart_name'],
            'item_picture' => $_GET['cart_picture'],
            'item_price' => $_GET['cart_price'],
            'size' => $_POST['size'],
            'quantity' => $_POST['quantity']
        );
        echo "<script>alert('product added'); window.location.href='grind.php'; </script>";
    } else {
        $_SESSION['cart'][0] = array(
            'item_id' => $_GET['cart_id'],
            'item_name' => $_GET['cart_name'],
            'item_picture' => $_GET['cart_picture'],
            'item_price' => $_GET['cart_price'],
            'size' => $_POST['size'],
            'quantity' => $_POST['quantity']
        );
        echo "<script>alert('product added'); window.location.href='grind.php'; </script>";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search