如果在我的餐馆中添加限制,如何添加限制添加到购物车

时间:2018-02-01 14:10:12

标签: php mysql

您好我有一个关于添加不同餐馆的购物车食品的任务,但在添加食物的添加限制方面存在一些问题。我的问题是,当我添加到购物车食品时,可以添加所有餐厅我希望每个餐厅有1个交易,当我添加到购物车时不同的餐馆食物会提示我在购物车中有不同的商品餐厅,如果我想继续推车将被取消,并将添加新的食物在我的购物车。请帮助谢谢

我不知道在哪里放置我的if else声明来限制添加食物

        //check if Add to Cart button has been submitted
        if(filter_input(INPUT_POST, 'add_to_cart')){
            if(isset($_SESSION['shopping_cart'])){
                //keep track of how many products are in the shopping cart
                $count = count($_SESSION['shopping_cart']);
                //create sequential array for matching array keys to products id's
                $product_ids = array_column($_SESSION['shopping_cart'], 'Product_ID');

                if(!in_array(filter_input(INPUT_GET, 'p'), $product_ids)){
                    $_SESSION['shopping_cart'][$count] = array (
                    'Restaurant_ID' => filter_input(INPUT_GET, 'r'),
                    'Product_ID' => filter_input(INPUT_GET, 'p'),
                    'name' => filter_input(INPUT_POST, 'name'),
                    'price' => filter_input(INPUT_POST, 'price'),
                    'quantity' => filter_input(INPUT_POST, 'quantity')
                    ); 
                }
                else { //product already exists, increase quantity
                    //match array key to id of the product being added to the cart
                    for ($i = 0; $i < count($product_ids); $i++){
                        if($product_ids[$i] == filter_input(INPUT_GET, 'p')){
                            //add item quantity to the existing product in the array
                            $_SESSION['shopping_cart'][$i]['quantity'] += filter_input(INPUT_POST, 'quantity');
                        }
                    }
                }
            }
            else {//if shopping cart doesn't exist, create first product with array key 0
                //create array using submitted form data, start from key 0 and fill it with values
                $_SESSION['shopping_cart'][0] = array (
                'Restaurant_ID' => filter_input(INPUT_GET, 'r'),
                'Product_ID' => filter_input(INPUT_GET, 'p'),
                'name' => filter_input(INPUT_POST, 'name'),
                'price' => filter_input(INPUT_POST, 'price'),
                'quantity' => filter_input(INPUT_POST, 'quantity')
                );        
            }
        }
      }
        ?>

0 个答案:

没有答案