简体   繁体   English

如果会话中已有产品,如何增加产品数量

[英]How to increase product quantity if a product is already exist in session

I am using the below code and want to increase the quantity in session array but its not working 我正在使用以下代码,并希望增加会话数组中的数量,但无法正常工作

if(!empty($_SESSION['quote']))
{

$prdata=$_SESSION['quote'];
}

$product_id = $data['id'];
$product_image = $data['image'];
$product_name = $data['pname'];
$product_quantity = $data['quantity'];
$product_price = $data['price'];

foreach ($_SESSION["quote"] as $cart_itm){         
         if($cart_itm['product_id'] == $product_id){ //the item exist in array

    $newdat = array('product_id' => $product_id,'quantity'=>$product_quantity,'price'=>$product_price,'name'=>$product_name,'image'=>$product_image); 
    $found = true;

}else{

    $newdat = array('product_id' => $product_id,'quantity'=>$product_quantity,'price'=>$product_price,'name'=>$product_name,'image'=>$product_image); 

}

You don't need to put there product_id etc. again, change just amount. 您无需再次放置product_id等。只需更改金额即可。 Your code will be shorter, clearer and you avoid to other possible typos, etc. 您的代码将更短,更清晰,并且避免出现其他可能的错别字等。

if($cart_itm['product_id'] == $product_id) {
    $cart_itm['quantity'] += $product_quantity; // expect in $product_quantity is the amount you want to add to the current one
    // it's a shorter variant of $cart_itm['quantity'] = $cart_itm['quantity'] + $product_quantity; 
}

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

相关问题 laravel session购物车-如果产品已经存在,如何增加数量 - laravel session shopping cart - how to increase the quantity if the product already exists 如何增加同一产品的数量,以及如何减少数量? - How to increase the quantity within the same product, and how to decrease the quantity? 添加相同产品时增加$ _SESSION中的数量值 - Increase quantity values in $_SESSION when adding same product 如果项目已经存在,则更新php会话数组中的产品数量 - Update product quantity in php session array if item is already there php增加和减少产品数量? - php increase and decrease quantity of product? 如何使用PHP MySQL或javascript增加/删除产品数量。 或如何插入产品的数量值和复选框 - how to increase/delete quantity of a product in PHP MySQL or javascript. Or how to insert a quantity value of a product along with a checkbox 更新 $_SESSION 产品数量未更新 - Update $_SESSION product quantity not updating 将相同的商品添加到购物车时,如何在会话中更新产品数量? - How to update product quantity in session when same items are added to cart? 如果产品已经存在,则禁用按钮 - disabling a button if the product already exist 覆盖购物车中现有产品的产品数量 -Woocommerce - override the product quantity of existing already product in cart -Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM