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 
4

1 に答える 1

0

カート内のすべてのアイテムを 1 つのアイテムに対してチェックしているようです。現在のアイテムが最初の繰り返しで一致する場合は、ループから抜けて、アイテムが存在するというフラグを設定してはいけません。ループの後、そのアイテムの数量を増やします。それ以外の場合は、ループの後に別のアイテムを追加します。実例。

ただし、別の色の製品の他のインスタンスを追加する方法は、次回製品の別のインスタンスを追加し、if 部分を介してチェックするときに機能しません。

ここでは、$id キーに対してアイテムを挿入しています。

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

アイテムIDが同じであるが異なる属性のelse部分では、カートの別の子配列に製品を追加しています:

$_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 を使用してください。

不明な点がある場合は、質問してください。また、あなたの問題を理解していないかどうかも教えてください。

于 2013-01-03T15:24:55.563 に答える