返回新闻列表

php简单Session购物车功能function函数

发布日期: 2024-03-14 | 来源: 智软设计工作室

<?php
// 启动会话
session_start();

// 初始化购物车
if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}


// 判断产品是否已经在购物车中
function isProductInCart($product_id) {
    if (isset($_SESSION['cart'][$product_id])) {
        return true;
    }
    return false;
}


// 添加商品到购物车 产品 ID (product_id)、产品名称 (product_name)、产品图片 (product_image) 和数量 (quantity)

function addToCart($product_id, $product_name, $product_image, $quantity) {
    if (isProductInCart($product_id)) {
        // 产品已经在购物车中,更新数量
        $_SESSION['cart'][$product_id]['quantity'] += $quantity;
        echo "产品已经添加到购物车中,并且数量已更新。";
    } else {
        // 产品不在购物车中,添加新产品
        $_SESSION['cart'][$product_id] = array(
            'product_id' => $product_id,
            'product_name' => $product_name,
            'product_image' => $product_image,
            'quantity' => $quantity
        );
        echo "产品已成功添加到购物车中。";
    }
}

// 更新购物车中商品的数量
function updateQuantity($product_id, $quantity) {
    if (isset($_SESSION['cart'][$product_id])) {
        $_SESSION['cart'][$product_id]['quantity'] = $quantity;
    }
}

// 清空购物车
function clearCart() {
    $_SESSION['cart'] = array();
}

// 显示购物车中的商品列表
function displayCart() {
    if (!empty($_SESSION['cart'])) {
        foreach ($_SESSION['cart'] as $item) {
            echo "Product ID: " . $item['product_id'] . "<br>";
            echo "Product Name: " . $item['product_name'] . "<br>";
            echo "Product Image: " . $item['product_image'] . "<br>";
            echo "Quantity: " . $item['quantity'] . "<br>";
            echo "<br>";
        }
    } else {
        echo "购物车为空";
    }
}



// 测试添加商品到购物车
addToCart(1, "产品1", "image1.jpg", 2);
addToCart(2, "产品2", "image2.jpg", 1);

// 显示购物车中的商品列表
displayCart();

// 更新购物车中商品的数量
updateQuantity(1, 4);

// 再次显示购物车中的商品列表
displayCart();

// 清空购物车
clearCart();

// 显示清空购物车后的商品列表
displayCart();




//单独判单 是否存在
if (isset($_SESSION['cart'][1])) {
    $product = $_SESSION['cart'][1];
    // 现在 $product 变量中包含了产品 ID 为 1 的产品信息
    $product_id = $product['product_id'];
    $product_name = $product['product_name'];
    $product_image = $product['product_image'];
    $quantity = $product['quantity'];
    // 然后您可以使用这些变量进行其他处理或显示
} else {
    // 产品 ID 为 1 的产品不存在于购物车中
}



?>


推荐技术资料

公安网站安全监测平台漏洞修复方法,

2023-09-22

今天一个客户网站。收到了公安网站安全监测平台漏洞报告。要求整改。于是我看了下。大概80%以上都是服务器响应头的问题,于是给客户服务器。单独去设置一下响应式和https,其实我觉得这些漏洞。也不行叫漏洞。客户网站里。就3-4个静态页面,还有一个漏洞是a链接不能用target="_blank,我还是第一次听说,

阅读更多

swiper里面如果又视频和图片,css将视频100%铺满,不撑高解决办法

2024-08-20

swiper里面如果又视频和图片,由于视频客户比例和图片不对。会导致高度不一样。会导致swiper高度撑高,其他比例少的就会又空白,由于考虑图片自适应。没有采用背景,背景的话。有些是显示不完全,只能像办法css裁剪掉视频,里面视频样式用这个即可。 .swiper-containervd .swiper-slide video {

阅读更多

video标签,自定义设置某个div播放按钮,默认去除video播放等按钮,点击在显示,

2023-08-24

<video src="img/xc.mp4" id="my-video" poster="enimg/pcv.jpg" controls></video> <div class="play" id="play-button"></div&

阅读更多

Ueditor怎么增加模板,添加模板解决方法-方便后面添加产品

2023-02-28

Ueditor怎么增加模板,直接进入,模板js目录:dialogs/template/config.js添加即可。参数说明:pre:模板选择时候的预览图,可自定义。title:模板预览时候显示的title 'preHtml':预览的代码'html': 插入的代码

阅读更多

pbootcms栏目和栏目内容列表一起循环标签

2021-10-18

{pboot:nav parent=6}<a href="[nav:link]">[nav:name]</a>{pboot:list scode=*}<a href= "[list:link]">[list:title]</a>{/pboot:list}{/pboot:nav}

阅读更多

解决pbootcms栏目URL名称,不支持_ 下划线问题,

2023-06-12

栏目URL名称自定义功能还是挺常用的。但是发现客户要去有下划线,但是pbootcms不支持。下面是修改方法,解决办法:找到ContentSortController.php修改二处。一个是新增和修改,//官方正则不支持下横杠//if

阅读更多

TP5 where数组查询(有多个查询条件)

2021-09-29

有查询条件就查询,多个查询条件,只要有查询,就增加一个查询条件//类型if($sotype){$where['type']=$sotype;}//合作单位if($companyid){&

阅读更多

pbootcms怎么判断分页是否在尾页

2024-05-09

{pboot:if('{page:current}'=='{page:count}')} 尾页 {else}不在尾页

阅读更多

pbootcms后台列表加入自定义字段,模板里面可判断模型

2024-05-24

后台列表加入自定义字段,先打开 apps/admin/model/content/ContentModel.php//获取文章列表publicfunctiongetList($mcode,$where=array()){$field 

阅读更多