再次添加项目时增加购物车中的商品数量

时间:2017-02-08 13:33:39

标签: php html

当我将特定商品添加到购物车中时,如果商品已经在购物车中,那么我需要更新其数量。现在添加重复项而不是更新数量。任何人都可以帮忙!代码如下。

case "add":
    if(!empty($_POST["quantity"])) {
        $productByCode = $db_handle->runQuery("SELECT * FROM medicine  WHERE med_id='" . $_GET["med_id"] . "'");
        $itemArray = array(
            $productByCode[0]["med_id"]=>array(
                'name'=>$productByCode[0]["med_name"],
                'med_id'=>$productByCode[0]["med_id"],
                'image'=>$productByCode[0]["Image"],
                'quantity'=>$_POST["quantity"],
                'price'=>$productByCode[0]["unit_cost"]
            )
        );

        if(!empty($_SESSION["cart_item"])) {
            if(in_array($productByCode[0]["med_id"],$_SESSION["cart_item"])){
                foreach($_SESSION["cart_item"] as $k => $v) {
                    if($productByCode[0]["med_id"] == $k)
                        $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                }
            }
             else {
                $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
            }
        }
        else {  
            $_SESSION["cart_item"] = $itemArray;
        }
    }
    break;

case "remove":
    if(!empty($_SESSION["cart_item"])) {
        foreach($_SESSION["cart_item"] as $k => $v){
            if($_GET["med_id"] == $_SESSION["cart_item"][$k]['med_id']) 
                unset($_SESSION["cart_item"][$k]);          
            if(empty($_SESSION["cart_item"]))  
                unset($_SESSION["cart_item"]);
         }
    }
    break;

1 个答案:

答案 0 :(得分:0)

为了解决添加问题,我可能会看到使用函数重新编写逻辑是否会有所帮助。我也会把它清理一下。将功能存储在单独的页面上并在使用前包含它们:

function getMedsById($id,$db)
    {
        if(!is_numeric($id))
            return false;

        $productByCode = $db->runQuery("SELECT * FROM medicine  WHERE med_id='" . $id . "'");

        $product = (!empty($productByCode[0]["med_id"]))? $productByCode[0] : false;
        if(empty($product))
            return false;

        return array(
            'name'=>$product["med_name"],
            'med_id'=>$product["med_id"],
            'image'=>$product["Image"],
            'price'=>$product["unit_cost"]
        );
    }

function addToCart($array,$id,$qty)
    {
        $array['quantity'] = $qty;
        # Save item to cart
        $_SESSION["cart_item"][$id] = $array;

    }

添加开关的返工:

case "add":
    if(!empty($_POST["quantity"])) {
        # Just do a basic ternary to avoid warnings
        $id   = (!empty($_GET["med_id"]))? $_GET["med_id"] : false;
        # Check input not manipulated
        $qty  =  (!empty($_POST['quantity']) && is_numeric($_POST['quantity']))? $_POST['quantity'] : 1;
        # Use the function to get the item from db
        $prod = getMedsById($id,$db_handle);
        # Stop if return is empty (user may be trying to manipulate cart)
        if(empty($prod))
            break;
        # Same as yours
        if(!empty($_SESSION["cart_item"])) {
            # Check if this product is already set
            if(isset($_SESSION["cart_item"][$id])){
                # If already set, just add the qty to existing
                $_SESSION['cart_item'][$id]['quantity'] += $qty;
                # Stop the add process here
                break;
            }
        }
        # Just add to cart here, it won't get here if already in cart
        addToCart($prod,$id,$qty);
    }
    break;