购物车会话数组

时间:2011-05-04 18:01:58

标签: php session session-variables shopping-cart

当用户点击添加按钮时,产品ID将存储到会话数组中。

请参阅以下代码:

Array
(

    [storeID] => 123
    [10] => Array
        (
            [quantity] => 1
            [product_id] => 2
            [extras_id] => Array
                (
                )

        )

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

        )
)

如您所见,10和20是product_id = 2

中的option_id

用户可以从特定产品中选择多个选项。

用户可以从选项

中选择(或不选择)额外内容

这个阵列是好的设计还是如何改进?

示例:

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

用户为汉堡选择了ID 10和20。

3 个答案:

答案 0 :(得分:1)

我没有看到它的问题,除了你可以通过这种方式让它更“有组织”:

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

但这只是我的意见和思考方式。

答案 1 :(得分:1)

如果产品3也有选项10怎么办?

我会使用产品作为关键,并添加数量,选项和附加组件。

此设置确实假设您不能再添加相同的产品,即使额外的产品不匹配也是如此。另一张海报建议添加额外/选项/ productid的组合,这很好。如果是这样的话,请向他投票:)。

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)
            )
        )       
    )
)

答案 2 :(得分:0)

我更喜欢以这种格式存储产品:

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

这为每个购物车项目提供了自己的“id”,如果需要,可以轻松修改每个购物车项目的数据,例如数量。

<强>编辑:
这是我的购物车项目阵列的简化示例:
7483f8f0007eb9ef3ddb8d2bff606bd6 859d1bb225ba5d16de4d4c23076cfae0 都是由md5($itemId.serialize($submittedOptions))创建的md5哈希值。

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
                        (
                            ...
                        )

                )

        )

)