[英]how to add multiple different types of product to cart
I am creating a shopping cart where I am trying to add different types of product eg Mobiles
, clothes
etc. I have created a add_to_cart
method and added products to session, then at last I save all products to database table. 我正在创建一个购物车,尝试在其中添加不同类型的产品,例如
Mobiles
, clothes
等。我创建了add_to_cart
方法并将产品添加到会话中,然后最后将所有产品保存到数据库表中。 But now I am confused how to add different product because parameter for different products are different eg 但是现在我很困惑如何添加不同的产品,因为不同产品的参数不同,例如
1. product_id, quantity, price for mobile.
2. product_id, quantity, price, size for cloths.
....
should I create different add_to_cart()
method for different types of products. 我应该为不同类型的产品创建不同的
add_to_cart()
方法。
In your scenario it would be better to have the product options sent as a JSON string and then decode it on the server side so that you don't have to worry about the different options for each product. 在您的方案中,最好将产品选项作为JSON字符串发送,然后在服务器端将其解码,这样您就不必担心每种产品的不同选项。
So product one might have options like: 因此,产品一可能具有以下选项:
{color: "black", size: "large"}
And product two might have: 产品二可能具有:
{memory: "2 GB", harddisk: "750 GB"}
etc. 等等
As long as products have at least some common fields, writing different methods is not entirely justified. 只要产品至少具有一些共同的领域,编写不同的方法是不完全合理的。
The other attributes can simply be passed as key-value pairs. 其他属性可以简单地作为键值对传递。
When writing the data into a database, you could apply EAV modeling . 将数据写入数据库时,可以应用EAV建模 。
you can keep using the add_to_cart() 您可以继续使用add_to_cart()
instead, you'll add an array parameter, say, "attributes" or "extra" and add there specific/unique product valued keys. 相反,您将添加一个数组参数,例如“ attributes”或“ extra”,并在此添加特定/唯一的乘积值键。
then in database you create an "attributes" column of variable size eg. 然后在数据库中创建一个可变大小的“属性”列,例如。 VARCHAR(128) and add those attributes encoded as JSONs
VARCHAR(128)并添加编码为JSON的那些属性
you can still search for any product then using "LIKE '%"attribute" :%'" (json) 您仍然可以搜索任何产品,然后使用“ LIKE'%” attribute“:%'”(json)
this doesnt forcing you to create additional columns for every new product attribute. 这不会强迫您为每个新产品属性创建其他列。
You can keep basic common things in primary array and specification in specification array as follows Products[mobile] =>array(name => 'test1', price =>'20' specification=array('color'=>red,model=>'N70' etc....)) 您可以在基本数组和规范数组中的规范中保留基本的通用属性,如下所示:Products [mobile] => array(name =>'test1',price =>'20'Specification = array('color'=> red,model = >'N70'等...))
Products[cloths] =>array(name => 'test1', price =>'30' specification=array('size'=>'XL',color=>'yello',etc......)) 产品[布料] =>阵列(名称=>'test1',价格=>'30'规格=阵列('尺寸'=>'XL',颜色=>'yello',等等...))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.