Search code examples
phpsessionshopping-cart

php session cart does not handle sizes


I have the following php code that add items to a session based shopping cart. It works fine when using items that don't come in a variation like size for example. The products consist of various clothes. Some come in different sizes and some not. One product can be a t-shirt that come in different sizes and another can be a cap that just is available in one size. I want to be able to add the t-shirt to the cart in different sizes. As it is now it only add to the session based cart if it not present. If present it will update the qty.

<?php
ini_set('display_errors', 'On');
/*include("session.php");   */
session_start();
if (isset($_POST["add_to_cart"]))
{

  if (isset($_SESSION["shopping_cart"]))
  {

      $item_array_id = array_column($_SESSION["shopping_cart"],"item_id");
      if (!in_array($_GET["id"], $item_array_id))
      {
         $count = count($_SESSION["shopping_cart"]);
         $item_array = array(
            'item_id'       => $_GET["id"],
            'item_name'     => $_POST["hidden_name"],
            'item_description'     => $_POST["hidden_description"],
            'item_price'    => $_POST["hidden_price"],
            'item_qty'      => $_POST["hidden_qty"],
            'itm_size'      => $_POST["select-size-1"],
            'itm_color'     => $_POST["product-1-color"]
         );
         $_SESSION["shopping_cart"][$count] =  $item_array;
      }
      else
      {
            // article exsits, update or remove
            foreach($_SESSION["shopping_cart"] as $keys => $values)
            {
                    if ($values["item_id"] == $_GET["id"] and ($values["itm_size"] != $_GET["select-size-1"]))
                    {
                        // check it item id is the same but color differs, if so add to session array
                        $count = count($_SESSION["shopping_cart"]);
                        $item_array = array(
                        'item_id'       => $_GET["id"],
                        'item_name'     => $_POST["hidden_name"],
                        'item_description'     => $_POST["hidden_description"],
                        'item_price'    => $_POST["hidden_price"],
                        'item_qty'      => $_POST["hidden_qty"],
                        'itm_size'      => $_POST["select-size-1"],
                        'itm_color'     => $_POST["product-1-color"]
                        );
                        $_SESSION["shopping_cart"][$count] =  $item_array;
                    }
                    else
                    {
                    // Orginal kod
                    if ($values["item_id"] == $_GET["id"])
                    {
                        // Om man anger 0 som antal ska artikel tas bort
                        if ($_POST["hidden_qty"] == "")
                        {
                            unset($_SESSION["shopping_cart"][$keys]);
                        }
                        else
                        {
                            // update qty
                            $_SESSION['shopping_cart'][$keys]['item_qty'] += $_POST["hidden_qty"];
                        }
                    }
                    }




            }


      }
  }
  else
  {
      //echo '<script>alert("session is set")</script>';
      $item_array = array(
        'item_id'       => $_GET["id"],
        'item_name'     => $_POST["hidden_name"],
        'item_description'     => $_POST["hidden_description"],
        'item_price'    => $_POST["hidden_price"],
        'item_qty'      => $_POST["hidden_qty"],
        'itm_size'      => $_POST["select-size-1"],
        'itm_color'     => $_POST["product-1-color"]
      );
      $_SESSION["shopping_cart"][0] =  $item_array;
  }
}
?>

So the qty should update based on ID on Size...

UPDATE: I have added what I thought would help considering handling sizes also. It do not work, if I add a t-shirt with Size XL it add it as it should, but when I add another t-shirt with size XL it does not change the qty but 2 more t-shirts with the same size?


Solution

  • As explain by 04FS, you doesn't check the size but only the product ID.

    [EDIT]

    Ok, let's simplify and clarify the all code... Carrefull to this line: $_SESSION["shopping_cart"][$index]['item_qty']++;, I'm not sure it's exactly what you want.

    if (isset($_POST["add_to_cart"]))
    {
    
        $index = false;
    
        if(!isset($_SESSION["shopping_cart"])) {
            $index = false;
        }
        else {
    
            foreach ($_SESSION["shopping_cart"] as $key => $value) {
    
                if($value["item_id"] == $_REQUEST["id"] && $value['itm_size'] == $_POST['select-size-1']) {
                    $index = $key;
                    break;
                }
    
            }
    
        }
    
        if($index !== false) {
            $_SESSION["shopping_cart"][$index]['item_qty']++;
        }
        else {
            // create new entry in the session shopping_cart
            $_SESSION['shopping_cart'][] = array(
                                                'item_id'       => $_REQUEST["id"],
                                                'item_name'     => $_POST["hidden_name"],
                                                'item_description'     => $_POST["hidden_description"],
                                                'item_price'    => $_POST["hidden_price"],
                                                'item_qty'      => $_POST["hidden_qty"],
                                                'itm_size'      => $_POST["select-size-1"],
                                                'itm_color'     => $_POST["product-1-color"]
                                             );
        }
    
    }
    

    [/EDIT]

    Feel free to ask if you need help with that.