简体   繁体   English

Codeigniter - 如果选项不同,请添加具有相同ID的产品

[英]Codeigniter - Add a product with the same ID if options are different

I'm currently building a simple ecommerce site and have ran into an interesting problem. 我目前正在构建一个简单的电子商务网站,并遇到了一个有趣的问题。

Basically, the products are in the products table however there are also a number of other options such as price, colour etc... some of these have an effect on the total price. 基本上,产品在产品表中,但也有一些其他选项,如价格,颜色等......其中一些对总价格有影响。

However, if the same product but with a different set of options exist then the product id is obviously the same and the item is not added into the codeigniter cart. 但是,如果存在相同但具有不同选项集的产品,那么产品ID显然是相同的,并且该项目不会添加到codeigniter购物车中。

The easiest way that I have thought of is to allow products with the same id to be included in the cart, is this easily possible? 我想到的最简单的方法是允许具有相同ID的产品包含在购物车中,这很容易吗? Is there a better way of tackling this problem? 有没有更好的方法来解决这个问题?

Any ideas will be very helpful! 任何想法都会非常有帮助!

OK so I found an answer to my problem which people may find helpful. 好的,所以我找到了一个人们可能会觉得有用的问题的答案。

Basically I was using multidimensional arrays for the options which is not supported by Codeigniter's cart class. 基本上我使用多维数组作为Codeigniter的cart类不支持的选项。

This was then generating the same row id due to the fact that the arrays were showing up as array but may also have been breaking the implode function used to generate the hash in the Codeigniter Cart class. 然后,由于数组显示为数组,但也可能已经破坏了用于在Codeigniter Cart类中生成哈希的implode函数,因此生成相同的行id。

To fix this you can simple replace this line within the CI_Cart class: 要解决此问题,您可以在CI_Cart类中简单地替换此行:

$rowid = md5($items['id'].implode('', $items['options']));

with this line: 用这一行:

$rowid = md5($items['id'].serialize($items['options']));

to fix the problem. 解决问题。

This then creates a storable representation of the multidimensional array as text that is then hashed and will always be unique if there is a multidimensional array used as the options value. 然后,这将创建多维数组的可存储表示作为文本,然后进行散列,并且如果存在用作选项值的多维数组,则始终是唯一的。

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

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