将另一个维度添加到数组购物车

时间:2014-04-20 10:10:57

标签: php

我有一个购物车,工作得很好,但我想存储商品类型(例如颜色,尺寸等)。

这是一个从购物车中获取商品的功能

public static function getCart() {
        if((isset($_SESSION['cart'])) && count($_SESSION['cart'])>0) {
            $ids = "";
            foreach($_SESSION['cart'] as $id => $quantity) {
                $ids = $ids . $id . ",";
            } 
            $ids = rtrim($ids, ',');

            $dotaz = dibi::fetchAll("SELECT * FROM eshop_products WHERE idProduct IN ({$ids})");
            return $dotaz;
        } else {
            //do nothing
        }
    }

将商品添加到购物车的功能

public static function addToCart($data) {

        $id = $data['id']; 
        $quantity = $data['qty']; 
        $varianta = $data['varianty']; //THIS IS WHAT I NEED TO ADD TO SESSION ARRAY


        if(!isset($_SESSION['cart'])) {
            $_SESSION['cart'] = array();
        }


        if(isset($_SESSION['cart'][$id])) {
            $_SESSION['cart'][$id] += $quantity;
        } else {
            $_SESSION['cart'][$id] = $quantity;
        }   
    }

有一些简单的方法吗?我搜索了一些教程,但仍然没有成功。

谢谢。

1 个答案:

答案 0 :(得分:0)

也许这是对你的暗示:

$id = $data['id']; 
$quantity = $data['qty']; 
$varianta = $data['varianty']; //THIS IS WHAT I NEED TO ADD TO SESSION ARRAY

$cart = array();

array_push($cart, array(
  'id' => $id,
  'quantity' => $quantity,
  'varianta' => $varianta
));