简体   繁体   English

更新购物车中的多种产品

[英]Update multiple products in shopping cart

I am updating the cart using the following code but this code is updating the cart for a single product. 我正在使用以下代码更新购物车,但是此代码正在更新单个产品的购物车。 How can I update the cart for multiple products? 如何更新多个产品的购物车?

update.php (this page is to take input in text box ie the quantity ) update.php (此页面用于输入文本框,即数量)

<?php
session_start();
?>
<html>
<body>
    <form action="update1.php" method="post">
        Quantity:
        <input type="text" name="qty" value="">
        <br><br>
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
</html>

update1.php (this code is to update the quantity of the cart) update1.php (此代码用于更新购物车的数量)

// foreach ( $value as $key=> $final_val ){
foreach($value as $product)
{
    if($_REQUEST['pro_id'] == $product['pro_id'])
    {     
        $found = true;
        break;
    }
}

if($found)
{
    $_SESSION['cart'][$_REQUEST['pro_id']]['qty'] ;
    $_SESSION[$x]=$product['qty'];
    $_SESSION["$qty"] = $_SESSION[$x]+$qty1;
    echo "qty:".$_SESSION["$qty"]; 
}
else
{
    // go get new product and add to $_SESSION['cart']
    echo"not done";
}
//}

echo "<h4 align='center'>  click here to <a href='shoppingcart.php'>see list</a> </h4>";
?>

I'm still not 100% sure what you mean, so I will give you a simple demonstration of multiple in one form. 我仍然不确定100%的意思,因此,我将以一种形式简单地演示倍数。 You may or may not have a class or functions for your cart so I have made a very simple one for demonstration purposes. 您的购物车可能没有类别或功能,因此出于演示目的,我做了一个非常简单的说明。 You would need to make your inputs arrayed on your form. 您需要将输入排列在表单上。

/classes/ShoppingCart.php /classes/ShoppingCart.php

<?php
class   ShoppingCart
    {
        public  function initialize()
            {
                if(!isset($_SESSION['cart']))
                    $_SESSION['cart']   =   array();
            }

        public  function addToCart($id,$qty = 1)
            {
                if(empty($_SESSION['cart'][$id]))
                    $_SESSION['cart'][$id]  =   $qty;
                else
                    $_SESSION['cart'][$id]  +=  $qty;
            }

        public  function resetItem($id,$qty = 1)
            {
                $_SESSION['cart'][$id]  =   $qty;
            }

        public  function removeItem($id,$qty = false)
            {
                if(!$qty || !is_numeric($qty)) {
                    if(isset($_SESSION['cart'][$id]))
                        unset($_SESSION['cart'][$id]);
                }
                else {
                    if(empty($_SESSION['cart'][$id]))
                        return false;

                    $amt    =   ($_SESSION['cart'][$id] - $qty);

                    if($amt <= 0)
                        unset($_SESSION['cart'][$id]);
                    else
                        $_SESSION['cart'][$id]  =   $amt;
                }
            }

        public  function getItem($id)
            {
                return (!empty($_SESSION['cart'][$id]))? $_SESSION['cart'][$id] : 0;
            }

        public  function clearCart()
            {
                if(isset($_SESSION['cart']))
                    unset($_SESSION['cart']);
            }
    }

/update.php /update.php

<?php
session_start();
// Using spl_autoload_register() would be better than manual include
require_once(__DIR__.'/classes/ShoppingCart.php');
// Start the cart
$cartEngine =   new ShoppingCart();
?>
<html>
<body>
    <form action="" method="post">
        <input type="hidden" name="action" value="update" />
        <ul>
            <li>
                <h4>Product 1</h4>
                QTY<input type="text" name="item[1][qty]" value="<?php echo $cartEngine->getItem('ITM1'); ?>" />
                <input type="hidden" name="item[1][prod_id]" value="ITM1" />
            </li>
            <li>
                <h4>Product 2</h4>
                QTY<input type="text" name="item[2][qty]" value="<?php echo $cartEngine->getItem('ITM2'); ?>" />
                <input type="hidden" name="item[2][prod_id]" value="ITM2" />
            </li>
            <li>
                <h4>Product 3</h4>
                QTY<input type="text" name="item[3][qty]" value="<?php echo $cartEngine->getItem('ITM3'); ?>" />
                <input type="hidden" name="item[3][prod_id]" value="ITM3" />
            </li>
        </ul>
        <input type="submit" name="submit" value="Submit">
    </form>
    <form action="" method="post">
        <input type="hidden" name="action" value="clear" />
        <input type="submit" value="CLEAR CART" />
    </form>
</body>
</html>

This gives you an array on submission: 这为您提供了一个提交数组:

Array
(
    [action] => update_cart
    [item] => Array
        (
            [1] => Array
                (
                    [qty] => 1
                    [prod_id] => ITM1
                )

            [2] => Array
                (
                    [qty] => 2
                    [prod_id] => ITM2
                )

            [3] => Array
                (
                    [qty] => 1
                    [prod_id] => ITM3
                )

        )

    [submit] => Submit
)

I personally would just not have a whole new page for the processing unless you are doing ajax. 除非您正在执行ajax,否则我个人将不会有一个全新的页面进行处理。 I would stick this part at the top of the page and reload the same page, but that is just me. 我会将这一部分放在页面顶部,然后重新加载同一页面,但这就是我。 To process the array would be something like: 要处理数组,将类似于:

// Using spl_autoload_register() would be better than manual include
require_once(__DIR__.'/classes/ShoppingCart.php');
// Start the cart
$cartEngine =   new ShoppingCart();
// See if action happens
if(!empty($_POST['action'])) {
    // Start the cart
    $cartEngine->initialize();
    // Do the cart stuff
    switch($_POST['action']) {
        case('update'):
            foreach($_POST['item'] as $item) {
                $cartEngine->resetItem($item['prod_id'],$item['qty']);
            }
            break;
        case('clear'):
            $cartEngine->clearCart();
            break;
    }
}

The cart would just be simple (you could make it more complicated if you want to by storing more data than just quantity and item code) but this is for demonstration, this would output a simple array like: 购物车会很简单(如果您想存储更多的数据而不是数量和物品代码,则可能会变得更加复杂),但这只是为了演示,它会输出一个简单的数组,例如:

[cart] => Array
    (
        [ITM1] => 1
        [ITM2] => 2
        [ITM3] => 4
    )

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

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