【问题标题】:Consolidating values from a PHP shopping cart合并 PHP 购物车中的值
【发布时间】:2013-02-19 21:33:50
【问题描述】:

我有以下问题。

我将购物车存储在 PHP 会话中,然后加载它以显示购物车中的商品。

问题是,目前,我看到每个商品都没有合并,这意味着如果一个商品在购物车中添加了 2 次,它将显示两次。

如何按 itemId 合并结果?

    if(is_array($_SESSION['cart']))
            {
    $max=count($_SESSION['cart']);
    for($i=0;$i<$max;$i++)
{
    $pid=$_SESSION['cart'][$i]['itemId'];
    $q=$_SESSION['cart'][$i]['qty'];
    if($q==0) continue;
    $query2 = $con -> prepare("SELECT * FROM item_descr WHERE id_item = :idItem");
    $query2-> bindValue (':idItem',$pid);
    $query2->execute();
    $row2 = $query2->fetch(PDO::FETCH_ASSOC);
                    ?>
            <div class="checkoutItems">     
            <span class='itemNameChck'><?php echo $row2['name']; ?> </a></span>
            <span class="itemQtyChck"><?php echo $row2['price']; ?> </span>
            <span class="itemQtyChck"><?php echo $q; ?> </span>
            <span class="itemQtyChck"><?php $totalPerItem = $q * $row2['price']; echo $totalPerItem; ?> </span>
            </div>
            <?php 
            }
            }

这是我将商品添加到购物车的方法:

function addtocart($pid,$q)
    {

        if($pid<1 or $q<1) return;
        if(is_array($_SESSION['cart']))
            {

                $max=count($_SESSION['cart']);
                $_SESSION['cart'][$max]['itemId']=$pid;
                $_SESSION['cart'][$max]['qty']=$q;
                $max=count($_SESSION['cart']);
            }
            else
            {
                $_SESSION['cart']=array();
                $_SESSION['cart'][0]['itemId']=$pid;
                $_SESSION['cart'][0]['qty']=$q;
                $max=count($_SESSION['cart']);

            }
        //}
    }

谢谢

【问题讨论】:

  • 将商品添加到购物车时,您应该验证它是否已经存在,如果存在,请添加该商品的数量而不是添加商品。
  • 另一种选择是通过 itemId 而不是索引 ($i) 对它们进行索引。这样一来,就不可能出现重复。
  • @legrandviking - 刚刚添加了添加项目的功能。我确实检查了数组是否存在...

标签: php cart shopping


【解决方案1】:

在我看来 - 你必须摆脱包含购物车项目索引的那个 ID。

//             I mean this
//                 ||
//                 ||
//                \  /
//                 \/
$_SESSION['cart'][$max]['qty']=$q;

只需将itemId 放在那里,然后在添加项目之前 - 检查是否设置了此数组元素。

您编写了这段代码,每次都向数组添加新元素,现在您想要合并。这对我来说毫无意义。仅在必要时添加新元素。否则,只需更新数量。

在我看来,您的购物车应该是这样的:

$_SESSION['cart'][$itemid]['qty']=$q;

// example how to store other attributes
$_SESSION['cart'][$itemid]['name']="Rubber duck";
$_SESSION['cart'][$itemid]['color']="Yellow";
$_SESSION['cart'][$itemid]['size']="XXL";
$_SESSION['cart'][$itemid]['price']="1000";

然后,在添加商品时,您可以检查具有该 itemid 的购物车商品是否包含任何元素。

if isarray($_SESSION['cart'][$itemid]) // when exists - add to previous quantity
    $_SESSION['cart'][$itemid]['qty']= $_SESSION['cart'][$itemid]['qty'] + $q;
else                                   // else - set quantity
    $_SESSION['cart'][$itemid]['qty'] = $q;

这只是一个草稿/想法,但我希望它会对你有所帮助。

【讨论】:

    【解决方案2】:

    这样的事情应该可以解决问题

    function addtocart($pid,$q)
    {
    if($pid<1 or $q<1) return;
    
    for($i=0;$i> count($_SESSION['cart']);$i++)
    {
        if($_SESSION['cart'][$i]["item_id"] == $pid)
        {   
            $_SESSION['cart'][$i]['qty'] += $q
            return;
        }
    }
    
    
    if(is_array($_SESSION['cart']))
    {
    
        $max=count($_SESSION['cart']);
        $_SESSION['cart'][$max]['itemId']=$pid;
        $_SESSION['cart'][$max]['qty']=$q;
        $max=count($_SESSION['cart']);
    }
    else
    {
        $_SESSION['cart']=array();
        $_SESSION['cart'][0]['itemId']=$pid;
        $_SESSION['cart'][0]['qty']=$q;
        $max=count($_SESSION['cart']);
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2012-04-10
      • 1970-01-01
      • 2015-01-08
      • 2015-08-02
      相关资源
      最近更新 更多