开发者

shopping cart session array

When user click on the add button, the product id are stored into the session array.

See Code below:

Array
(

    [storeID] => 123
    [10] => Array
        (
            [quantity] =&g开发者_开发技巧t; 1
            [product_id] => 2
            [extras_id] => Array
                (
                )

        )

    [20] => Array
        (
            [quantity] => 12
            [product_id] => 2
            [extras_id] => Array
                (
                   8
                )

        )
)

As you can see 10 and 20 is option_id from the product_id = 2

User can select number of options from a specific product.

User can select extras (or without) from option

Is this array good design or how can it be improved?

Example:

Product (2): Burger
- Option (10): Large (User not selected any extra)
- Option (20): Small (User selected coke(8) as extra)

User selected ID 10 and 20 for burger.


I don't see a problem with it, except that you could get it more "organized", this way:

Array
(
    [123] => array(
        [2] => array(
            [10] => array(
                [quantity] => '',
                [extras] => ''
            ),
            [20] => array(
                [quantity] => '',
                [extras] => ''
            )
        )       
    )
)

But that's just my opinion and my way to think.


What if product 3 also has option 10?

I'd go for using product's as the key, and adding quantity, options and extras an subs of that array.

This setup does assume you can't add the same product more then once, even if the extra's do not match. Another poster suggest adding an combination of extras/options/productid, which is good. If that's the case, please upvote him :).

Array
(
    'cart' => array(
        'storeid' => 123,
        'products' => array(
            2 => array(
              'quantity' => 2,
              'options' => array(10, 20),
              'extras' => array(2)
            ),
            3 => array(
              'quantity' => 12,
              'options' => array(150, 20),
              'extras' => array(1, 7)
            )
        )       
    )
)


What I prefer is to store products in this format:

Array
(
    [md5 hash of (product id + serialized array of selected options)] => Array
        (
            'qty' => 10
            'title' => 'Product XYZ'
            'price' => 49.99
            'options' => Array
                (
                   ...
                )
        )
)

That gives each cart item it's own "id", easily allowing you to modify data for each cart item, such as qty, if you need to.

Edited:
This is a trimmed down example of what my cart's items array looks like:
7483f8f0007eb9ef3ddb8d2bff606bd6 and 859d1bb225ba5d16de4d4c23076cfae0 are both md5 hashes created by md5($itemId.serialize($submittedOptions)).

Array
(
    [7483f8f0007eb9ef3ddb8d2bff606bd6] => Array
        (
            [id] => 3
            [qty] => 2
            [price] => 20.00
            [title] => Product XYZ
            [data] => Array
                (
                    [photo] => /uploads/media/products/product_xyz.jpg
                    [link] => /product/3-product-xyz/
                    [sku] => PRODUCT-XYZ
                    [weight] => 10
                    [attributes] => Array
                        (
                            ...
                        )

                )

        )

    [859d1bb225ba5d16de4d4c23076cfae0] => Array
        (
            [id] => 3
            [qty] => 2
            [price] => 30.00
            [title] => Product XYZ
            [data] => Array
                (
                    [photo] => /uploads/media/products/product_xyz.jpg
                    [link] => /product/3-product-xyz/
                    [sku] => PRODUCT-XYZ
                    [weight] => 15
                    [attributes] => Array
                        (
                            ...
                        )

                )

        )

)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜