确定新的购物车条目是否包含与购物车/会话中已有条目相同的标识符

发布于 2024-12-03 00:58:02 字数 2098 浏览 1 评论 0原文

我有一个功能可以在购物车中添加定制的浮雕。购物车存储在会话变量中。如果客户决定构建完全相同的客串,我不想在数组中添加另一个条目,我只想能够在所述产品的数量上再添加 1 个条目。问题是,当脚本到达检查数组中是否存在值时,它会返回 false 并向数组添加一个新条目。我对 PHP 相当陌生,所以我不确定我是否以正确的方式处理它。

function AddToCart($subpid,$subtype,$subprice,$subtotal,$subcarving,$subline1,$subline2){
    global $form;
    
    $exist = false;
    $i=0;
    
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array(0 => array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => 1));
    }
    else{
        foreach($_SESSION['cart'] as $item){
            $i++;
            while(list($key,$value) = each($item)){
                /* If product exist add 1 to quantity */
                if($key == "Product ID" && $value == $subpid && $key == "Type" && $value == $subtype && $key == "Price" && $value == "$".$subprice && $key == "Subtotal" && $value == "$".$subtotal  && $key == "Carving" && $value == $subcarving && $key == "Line 1" && $value == $subline1 && $key == "Line 2" && $value == $subline2){
                    array_splice($_SESSION['cart'], $i-1, 1, array(array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => $item['Quantity'] + 1)));
                    $exist = true;
                }
            }
        }
        if($exist == false){
            array_push($_SESSION['cart'], array("Product ID" => $subpid, "Type" => $subtype, "Price" => "$".$subprice, "Subtotal" => "$".$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => 1));
        }
    }
    return 0;
}

如果我只是使用: $key == "Product ID" && $value == $subid 它将返回 true 并更新数量,但问题是如果客户购买两个具有相同 id 但雕刻不同的浮雕或雕刻,我的购物车将会关闭。

I have a function that adds custom built cameos in a cart. The cart is stored in a session variable. If the customer decides to build the very same cameo I don't want to add another entry to the array I just want to be able to add 1 more to the quantity of said product. The problem is when the scripts reaches the point where it's checking to see if the values exist in the array it returns false and adds a new entry to the array. I am fairly new to PHP so I'm not sure if I am going about it the right way.

function AddToCart($subpid,$subtype,$subprice,$subtotal,$subcarving,$subline1,$subline2){
    global $form;
    
    $exist = false;
    $i=0;
    
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array(0 => array("Product ID" => $subpid, "Type" => $subtype, "Price" => "
quot;.$subprice, "Subtotal" => "
quot;.$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => 1));
    }
    else{
        foreach($_SESSION['cart'] as $item){
            $i++;
            while(list($key,$value) = each($item)){
                /* If product exist add 1 to quantity */
                if($key == "Product ID" && $value == $subpid && $key == "Type" && $value == $subtype && $key == "Price" && $value == "
quot;.$subprice && $key == "Subtotal" && $value == "
quot;.$subtotal  && $key == "Carving" && $value == $subcarving && $key == "Line 1" && $value == $subline1 && $key == "Line 2" && $value == $subline2){
                    array_splice($_SESSION['cart'], $i-1, 1, array(array("Product ID" => $subpid, "Type" => $subtype, "Price" => "
quot;.$subprice, "Subtotal" => "
quot;.$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => $item['Quantity'] + 1)));
                    $exist = true;
                }
            }
        }
        if($exist == false){
            array_push($_SESSION['cart'], array("Product ID" => $subpid, "Type" => $subtype, "Price" => "
quot;.$subprice, "Subtotal" => "
quot;.$subtotal, "Carving" => $subcarving, "Line 1" => $subline1, "Line 2" => $subline2, "Quantity" => 1));
        }
    }
    return 0;
}

If I was to just use: $key == "Product ID" && $value == $subid it will return true and update the quantity but the problem with that is if the customer buys two cameo with the same id but different carving on it or engravings my cart will be off.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2024-12-10 00:58:02

我认为你让这种方式变得比它必须的更加令人困惑......

我会像这样设置你的购物车数组:

$cart[0]["name"] = "whatever";
$cart[0]["ProductID"] = "1234";
$cart[0]["price"] = 0.00;
$cart[0]["quantity"] = 1;
$cart[0]["options"] = array(
     "subcarving" => "asdf",
     "subline1"   => "asdfsafd",
     "subline2"   => "asfdsadfdf");

然后你可以像这样通过简单的循环来处理它:

$didadd = 0;
for($x = 0; $x < sizeof($cart); $x++) {
    if($subid == $cart[$x]["ProductID"]) {
        // check options
        $sameOpts = 1;
        foreach($cart[$x]["options"] as $key => $val) {

            if($val != ${$key}) { // checks if the current items option[val] = function(val)
                $sameOpts = 0;
            }

        }

        if($sameOpts) {
            $didadd = 1; // sets the flag that we added the element.
            // increase quantity since the product id and options matched.

        } else {
            $didadd = 1;
            // add new element
            // sets the flag that we added the element
        }

    }
} 

if(!$didadd) { 
    // still need to add the item
    // do code to create new $cart item here.

}

I think you're making this way more confusing than it has to be...

I would set up your cart array like this:

$cart[0]["name"] = "whatever";
$cart[0]["ProductID"] = "1234";
$cart[0]["price"] = 0.00;
$cart[0]["quantity"] = 1;
$cart[0]["options"] = array(
     "subcarving" => "asdf",
     "subline1"   => "asdfsafd",
     "subline2"   => "asfdsadfdf");

Then you could just handle it with easy looping like so:

$didadd = 0;
for($x = 0; $x < sizeof($cart); $x++) {
    if($subid == $cart[$x]["ProductID"]) {
        // check options
        $sameOpts = 1;
        foreach($cart[$x]["options"] as $key => $val) {

            if($val != ${$key}) { // checks if the current items option[val] = function(val)
                $sameOpts = 0;
            }

        }

        if($sameOpts) {
            $didadd = 1; // sets the flag that we added the element.
            // increase quantity since the product id and options matched.

        } else {
            $didadd = 1;
            // add new element
            // sets the flag that we added the element
        }

    }
} 

if(!$didadd) { 
    // still need to add the item
    // do code to create new $cart item here.

}
莫多说 2024-12-10 00:58:02

它不起作用,因为您同时与 && 语句比较每个键,但一次循环遍历每个键。取出 while 循环并像这样比较它:

if( $item['Product ID'] == $subpid ... //etc ) {

}

此外,您不需要 array_splice 只需更新项目即可。

It doesn't work because you are comparing each key at the same time with the && statement but you're looping through each one of your keys one at a time. Take out the while loop and just compare it like this:

if( $item['Product ID'] == $subpid ... //etc ) {

}

Also you don't need array_splice just update the items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文