php简单的购物车

abstract class shopCart{
    /**
     * 向购物车中添加1个商品
     * @param $goods_id int 商品id
     * @param $goods_name String 商品名
     * @param $shop_price float 价格
     * @return boolean
     */
    abstract public function add($goods_id,$goods_name,$shop_price,$market_price,$thumb_img);
    /**
     * 减少购物中1个商品的数量,如果减至0,则从购物车删除此商品
     * @param $goods_id int 商品id
     */
    abstract public function decr($goods_id);
    /**
     * 从购物车删除某商品
     * @param $goods_id 商品id
     */
    abstract public function del($goods_id);
    /**
     * 列出购物车所有的商品
     * @return Array
     */
    abstract public function items();
    /**
     * 返回购物车中有几种商品
     * @return int
     */
    abstract public function calcType();
    /**
     * 返回购物中商品的个数
     * @return int
     */
    abstract public function calcCnt();
    /**
     * 返回购物车中商品的总价格
     * @return float
     */
    abstract public function calcMoney();
    /**
     * 清空购物车
     * @return void
     */
    abstract public function clear();


}



class CartTool extends shopCart
{
    protected $items=array();
    protected static $ins=null;

    protected function __construct(){
        $cart=session('cart');
        if(!empty($cart)){
            $this->items=$cart;
        }
    }
    static function getIns(){
        if(self::$ins==null && !(self::$ins instanceof self)){
            self::$ins=new self();
        }
        return self::$ins;
    }
    /**
     * 向购物车中添加1个商品
     * @param $goods_id int 商品id
     * @param $goods_name String 商品名
     * @param $shop_price float 价格
     * @return boolean
     */
    public function add($goods_id,$goods_name,$shop_price,$market_price,$thumb_img){
        $row=array();
        if(isset($this->items[$goods_id])){
            $this->items[$goods_id]['num']+=1;
        }else{
            $row['goods_name']=$goods_name;
            $row['shop_price']=$shop_price;
            $row['market_price']=$market_price;
            $row['thumb_img']=$thumb_img;
            $row['num']=1;
            $row['price']=$row['num']* $row['shop_price'];
            $this->items[$goods_id]=$row;
        }
        return true;

    }
    /**
     * 减少购物中1个商品的数量,如果减至0,则从购物车删除此商品
     * @param $goods_id int 商品id
     */
     public function decr($goods_id){
         if(isset($this->items[$goods_id])){
             $this->items[$goods_id]['num']-=1;
         }
         if($this->items[$goods_id]['num']==0){
            $this->del($goods_id);
         }

     }
    /**
     * 从购物车删除某商品
     * @param $goods_id 商品id
     */
     public function del($goods_id){
         unset($this->items[$goods_id]);
     }
    /**
     * 列出购物车所有的商品
     * @return Array
     */
    public function items(){
        return $this->items;
    }
    /**
     * 返回购物车中有几种商品
     * @return int
     */
     public function calcType(){
         return count($this->items);
     }
    /**
     * 返回购物中商品的个数
     * @return int
     */
     public function calcCnt(){
         $cnt=0;
        foreach($this->items as $v){
            $cnt+=$v['num'];
        }
        return $cnt;
     }
    /**
     * 返回购物车中商品的总价格
     * @return float
     */
    public function calcMoney(){
        $price=0.00;
        foreach($this->items as $v){
            $price+=$v['num']*$v['shop_price'];
        }
        return  $price;
    }

    /**
     * 清空购物车
     * @return void
     */
     public function clear(){//清空购物车
         $this->items=array();
     }
    public function __destruct(){//在离开本页面时把购物车信息添加到session里
        session('cart',$this->items);
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单PHP 购物车的示例代码: 1. 首先,创建一个名为 "cart.php" 的文件,并在文件中添加以下代码: ```php <?php session_start(); if(isset($_POST['add-to-cart'])) { $product_id = $_POST['product-id']; $product_name = $_POST['product-name']; $product_price = $_POST['product-price']; $product_quantity = $_POST['product-quantity']; $_SESSION['cart'][] = array( 'id' => $product_id, 'name' => $product_name, 'price' => $product_price, 'quantity' => $product_quantity ); } ?> ``` 2. 在需要显示购物车的页面上,添加以下代码: ```php <?php session_start(); if(!empty($_SESSION['cart'])) { echo '<table>'; echo '<tr><th>ID</th><th>Name</th><th>Price</th><th>Quantity</th></tr>'; foreach($_SESSION['cart'] as $item) { echo '<tr>'; echo '<td>' . $item['id'] . '</td>'; echo '<td>' . $item['name'] . '</td>'; echo '<td>' . $item['price'] . '</td>'; echo '<td>' . $item['quantity'] . '</td>'; echo '</tr>'; } echo '</table>'; } else { echo 'Your cart is empty'; } ?> ``` 3. 在需要添加商品到购物车的页面上,添加以下代码: ```html <form method="post" action="cart.php"> <input type="hidden" name="product-id" value="1"> <input type="hidden" name="product-name" value="Product 1"> <input type="hidden" name="product-price" value="10"> <label>Quantity: <input type="number" name="product-quantity" value="1"></label> <button type="submit" name="add-to-cart">Add to cart</button> </form> ``` 这里的示例代码假设您的商品信息是硬编码在代码中的,实际情况下您需要从数据库或其他数据源动态获取商品信息。此外,这只是一个简单的示例代码,实际的购物车可能需要更多的功能和验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值