简体   繁体   English

将数量添加到购物篮中将添加一个新商品

[英]Adding quantity to basket item is adding a new item

I am creating a basket and what I want is if an item that is already in the basket is added again, the quantity of the original item will be appended by 1. 我正在创建一个购物篮,我想要的是,如果再次添加已经在购物篮中的商品,则原始商品的数量将附加1。

Instead when I add an item that is already in the basket, a new item is added, and the quantity of this new item can be appended by clicking +1 on the original item. 相反,当我添加已经在购物篮中的项目时,会添加一个新项目,并且可以通过在原始项目上单击+1追加此新项目的数量。

basket.php basket.php

<?php
  foreach ($_SESSION["basket"] as $basketItemArray) {                   
?>

<form role="form" action="includes/functions/create_shopping_cart.php" method="post">
    <button type="submit" name="basket-button" class="btn btn-default" value="<?php echo $basketItemArray["item_id"]; ?>"><i class="fa fa-plus" aria-hidden="true"></i></button>
    <? echo $basketItemArray["quantity"]; ?>
    <button class="btn btn-default"><i class="fa fa-minus" aria-hidden="true"></i></button>
</form>

The above form sends the item_id through the value of the + button. 上面的表格通过+按钮的值发送item_id

create_shopping_cart.php create_shopping_cart.php

$product_id = $_POST['basket-button'];
$sql = "SELECT * FROM menu WHERE product_id='".$product_id."'";

$result = $connection->query($sql);
$row = $result->fetch_assoc();

if (empty($_SESSION["basket"])) {
    $_SESSION["basket"]  = array( 

    array( "item_id"=>$row['product_id'], "item_name"=>$row['name'],"quantity"=>1 , "price"=>$row['price']) );

} else {
    // There is already a basket to append to
    $current_basket = $_SESSION["basket"];

    $found = false;
    foreach($_SESSION['basket'] as $product)
    {
        if($product_id == $product['item_id']) {
            $found = true;
            break;
        }
    }

    if($found)
    {
        $_SESSION['basket'][$product_id]['quantity'] ++;               
    } else {
        $new_basket  = array( 

        array( "item_id"=>$row['product_id'], "item_name"=>$row['name'],"quantity"=>1 , "price"=>$row['price']) );

        $_SESSION['basket'] = array_merge($current_basket, $new_basket);      
    } 
}

Result 结果 这是在原始项目上单击+按钮两次的结果

You are not targeting/using the index of the products in basket. 您没有针对/使用篮子中产品的索引。 Try this: 尝试这个:

$product_id = $_POST['basket-button'];
$sql = "SELECT * FROM menu WHERE product_id='".$product_id."'";

$result = $connection->query($sql);
$row = $result->fetch_assoc();

if (empty($_SESSION["basket"])) {
    $_SESSION["basket"]  = array( 

    array( "item_id"=>$row['product_id'], "item_name"=>$row['name'],"quantity"=>1 , "price"=>$row['price']) );

} else {
    // There is already a basket to append to
    $current_basket = $_SESSION["basket"];

    $found = false;
    $id = '';
    foreach($_SESSION['basket'] as $key=>$product)
    {
        if($product_id == $product['item_id']) {
            $found = true;
            $id    = $key;
            break;
        }
    }

    if($found)
    {
        $_SESSION['basket'][$id]['quantity']++;               
    } else {
        $new_basket  = array( 

        array( "item_id"=>$row['product_id'], "item_name"=>$row['name'],"quantity"=>1 , "price"=>$row['price']) );

        $_SESSION['basket'] = array_merge($current_basket, $new_basket);      
    } 
}

Your code can be optimized some more but for now it is more important to get it to work correctly. 您的代码可以进行更多优化,但现在更重要的是使其正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM