2012-07-20 60 views
1

好的,我遇到了这个问题。我试图将产品添加到购物车的产品作为选项颜色等,但我似乎无法做到正确。如果我点击“添加到购物车”,它会每次都会计数,但是如果我改变颜色,那么它就会出错。帮助,男生和女生!一直在这方面工作了一段时间。购物车阵列和会话

if(isset($_POST['submit'])){ 
         $id = $_POST['id']; 
         $sleeve = $_POST['sleeve']; 
         $colour = $_POST['colour']; 
         $size = $_POST['size']; 
         $action = $_POST['action']; 
         $quantity = 1; 
         } 
         if(!isset($_SESSION['cart'][$id])){ 
          $_SESSION['cart'][$id] = array($id, $colour, $size, $quantity);  
         } 
          else{ 
           if(isset($_SESSION['cart'][$id])){ // if the session as already been set then.. 
             while(list($key, $value) = each($_SESSION['cart'])){ // while loop to chech to content of the session.. 
               if($value[0] == $id && $value[1] == $colour && $value[2] == $size){ // checking to see if the session content is the same.. 
                $quantity = $value[3] +=1; 
                $_SESSION['cart'][$id] = array($id, $colour, $size, $quantity); // if it is the same then add this.. 
               }// if is == 
                else{ 
                 $quantity +=1; 
                 $_SESSION['cart'][$id][] = array($id, $colour, $size, $quantity); // is session is not the same do this.. 
                 }//else if it is not == 

              }// while 
             }// if isset 
           }// else isset session 

回答

0

好像你正在检查车对一个项目的每个项目,如果目前的项目将在第一次迭代匹配,那么它不应该走出循环,并设置标志,产品目前循环之后的话,那增量项目数量在循环后添加另一个实例。

但是,如果您将添加另一个产品实例,并且将通过您的if部分进行检查,那么您下次再添加具有不同颜色的产品的其他实例的方式仍然无效。

这里要插入项目针对的$ id键:

if(!isset($_SESSION['cart'][$id])){ 
         $_SESSION['cart'][$id] = array($id, $colour, $size, $quantity);  
        } 

而在其他部分时,项目ID是要添加产品的购物车另一个孩子阵列相同,但不同的属性:

$_SESSION['cart'][$id][] = array($id, $colour, $size, $quantity); // is session is not the same do this.. 

所以你应该添加每个产品/项目为:

$_SESSION['cart'][$id][] = array($id, $colour, $size, $quantity); 

在这两种情况下,她的产品或其他变体的第一个变体。

但是,如果您的价格因变异而异,请对每个商品的每种变化使用不同的商品实例ID。

如果我在任何时候都不清楚,那么请问,也请告诉我如果我没有理解你的问题。