繁体   English   中英

如何在 laravel 中使用 $_SESSION 将商品添加到购物车

[英]How to use $_SESSION in laravel to add items to cart

我很想在 Laravel 中使用上面的代码,就像在普通php中使用$_SESSION一样。 所以我需要有关如何将其转换为 Laravel 的帮助。 我只是尝试将东西添加到购物车。

if(isset($_POST["action"]))
{
    if($_POST["action"] == "add")
    {
        if(isset($_SESSION["shopping_cart"]))
        {
            $is_available = 0;

            foreach($_SESSION["shopping_cart"] as $keys => $values)
            {
                if($_SESSION["shopping_cart"][$keys]['product_id'] == $_POST["product_id"])
                {
                    $is_available++;

                    $_SESSION["shopping_cart"][$keys]['product_quantity'] = $_SESSION["shopping_cart"][$keys]['product_quantity'] + $_POST["product_quantity"];
                }
            }

            if($is_available == 0)
            {
                $item_array = array(
                    'product_id'       => $_POST["product_id"],
                    'product_name'     => $_POST["product_name"],
                    'product_price'    => $_POST["product_price"],
                    'product_quantity' => $_POST["product_quantity"]
                );

                $_SESSION["shopping_cart"][] = $item_array;
            }
        }
        else
        {
            $item_array = array(
                'product_id'       => $_POST["product_id"],
                'product_name'     => $_POST["product_name"],
                'product_price'    => $_POST["product_price"],
                'product_quantity' => $_POST["product_quantity"]
            );

            $_SESSION["shopping_cart"][] = $item_array;
        }
    }
}

请帮我转换。

查看Laravel session 文档

我认为您将使用的是session()->has()session()->push()

暂无
暂无

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

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