开发者

Best way to track multiple instances of a product (with different options) in a shopping cart?

开发者 https://www.devze.com 2023-04-11 23:47 出处:网络
I\'m developing a lightweight shopping cart but have become stumped with products that have options, e.g. a t-shirt in multiple sizes.

I'm developing a lightweight shopping cart but have become stumped with products that have options, e.g. a t-shirt in multiple sizes.

The database design I have no problem with. I have shop_categories and shop_products tables, and tables for product options that works with a HABTM relationship.

My problem is: how do I store multiple instances of a product (with different options) in my cart?

At the moment, my cart is stored as an array in the $_SESSION, containing product details and keyed on the product's ID. For example:

$cart = array(
    32 => array(
        'id' => 32,
        'category' => 7,
        'title' => 'Generic T-shirt',
        'friendly_name' => 'generic-t-shirt',
        'price' => '10.00',
        'quantity' => 2
    )
);

This worked fine but now I'm stuck with the introduction for product items. For example, if someone adds a small t-shirt to the cart, and then a large t-shirt to the cart, they'll need to be tracked individually I'd surmise?

How can I overcome this? I guess my first stumbling block would be storing cart contents on the product ID, but this is how I currently increase/decrease quantity when someone adds or removes from their cart. For example:

if (isset($_POST['add_to_cart'])) {
    // $_POST['product_id'] and $_POST['quantity'] are validated here
    // to check they're both integers and the product actually exists
    if (array_key_exists($cart[$product_id])) {
        // increment quantity by $quantity
    }
    else {
        // add product to cart and set to quantity to $quantity
    }
}

Sorry if I'm rambled on but I think that should give a sufficient overview of my application and the problem I face. 开发者_运维问答I look forward to seeing your answers.


Without me rewriting your whole code, I suggest reformatting your cart to something like this...

$cart = array(
    32 => array(
        'id' => 32,
        'category' => 7,
        'title' => 'Generic T-shirt',
        'friendly_name' => 'generic-t-shirt',
        'price' => '10.00',
        'itemCount' => 2,
        'items' => array(
            array(    
                'quantity' => 1,
                'size' => 'L'
            ),
            array(    
                'quantity' => 1,
                'size' => 'M'
            )
        ),
    38 => array(
        'id' => 38,
        'category' => 17,
        'title' => 'Stack Overflow T-shirt',
        'friendly_name' => 'stack-overflow-t-shirt',
        'price' => '15.00',
        'itemCount' => 3,
        'items' => array(
            array(    
                'quantity' => 2,
                'size' => 'L'
            ),
            array(    
                'quantity' => 1,
                'size' => 'M'
            )
        )

    )
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号