简体   繁体   English

如何在购物车中的PHP中具有相同名称的会话数组变量?

[英]How to have session array variables in PHP with same name in shopping cart?

I am creating a shopping cart in PHP using session variables to store the users product selections prior to checking out. 我正在使用会话变量在PHP中创建一个购物车,以便在签出之前存储用户的产品选择。 For the most part, it works. 在大多数情况下,它是有效的。 The problem is that if a user selects lets say a 'red' shirt in size 'small', it adds it to the session variable as 问题是,如果用户选择说“小”号的“红色”衬衫,则会将其添加到会话变量中,如下所示:

    'products' =>
    array(size=1)
        'code' =>
            array(size=5)
                'product_size' => string 'Small'
                'quantity' => string '1'
                'product_code' => string 'sku'
                'name' => string 'shirt red'
                'price' => string '25'      

BUT if the user selects the same item in a different size, it overwrites the previous session variable with same code. 但是,如果用户选择大小不同的同一项目,则会用相同的代码覆盖先前的会话变量。

How can I make it so the user can get the same item in different sizes without overwriting the previous session variable? 我如何做到这一点,以便用户可以以不同的大小获得相同的项目,而不会覆盖先前的会话变量?

I would build it like this 我会这样建造

 ['products' => [
    $sku => [  //the actual sku value, not literally sku
        'price' => '25', //if it's common ( total = price x total_qty ), if not put a total here
         //'total_price' => 70
        'total_qty' => 3,
        'name' => string 'shirt red' //if its common
        'items' => [
             0 => [
                 'size' => 'Small'
                 'quantity' => '1'              
                 //'price' => 20
             ], 1 => [ 
                 'size' => 'Medium'
                 'quantity' => '2'              
                 //'price' => '25' 
             ]   
        ] //end items
   ]//end sku
]

Basically nest an item with the stuff is different for each item. 基本上,嵌套一个项目时,每个项目的内容都不相同。 I put price in commented form if its not common to the sizes. 如果价格在尺寸上不常见,我会以注释形式列出价格。

You could also key the items on size if that is the main difference like this 如果这是主要区别,您也可以按大小键入项目

  .....
   'items' => [
        'Small' => [
             'quantity' => '1'              
             //'price' => 20
         ], 'Medium' => [ 
             'quantity' => '2'              
             //'price' => '25' 
         ]   
    ]

This is less "flexible" but easier to minimize duplicate items. 这不太“灵活”,但更容易减少重复项。 The items array keys are easier to check then a further nested 'size' key. items数组键比另一个嵌套的“ size”键更易于检查。

This should give you the idea, the actual structure is up to you and your use case, build it in the way that allows you the easiest access to the items, but is still flexible enough to accommodate all your products. 这应该给您一个想法,实际的结构取决于您和您的用例,以允许您最轻松地访问项目的方式构建它,但仍然足够灵活以容纳您的所有产品。

Last example, if the size and quantity are the only things that are different you could even do away with the nested array. 最后一个例子,如果大小和数量是唯一不同的东西,那么您甚至可以消除嵌套数组。

.....
'items' => [
    'Small' => '1','Medium' => '2'  
 ]

Also your array has this obvious error in your question. 同样,您的数组在您的问题中也有明显的错误。

   product_color' => string 'Small'

Keep product id as key and inside that have the attributes such as name, sku and types. 将产品ID保留为键,并在其中保留具有名称,sku和类型等属性。

[
'products' => [
    'pid123' => [
        [
            'name' => 'abc',
            'sku' => 'sku-abc',
            'types' => [
                [
                    'colour' => 'red',
                    'size' => '42',
                    'qty' => 1
                ],
                [
                    'colour' => 'red',
                    'size' => '40',
                    'qty' => 2
                ]
            ]
        ]
    ],
    'pid789' => [
        [
            'name' => 'xyz',
            'sku' => 'sku-xyz',
            'types' => [
                [
                    'colour' => 'red',
                    'size' => '42',
                    'qty' => 1
                ],
                [
                    'colour' => 'red',
                    'size' => '40',
                    'qty' => 2
                ]
            ]
        ]
    ] 
]];

Type list will contain various size, colour, and quantity information. 类型列表将包含各种尺寸,颜色和数量信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM