简体   繁体   English

如何将多种不同类型的产品添加到购物车

[英]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. 我正在创建一个购物车,尝试在其中添加不同类型的产品,例如Mobilesclothes等。我创建了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.

相关问题 如何观察产品的“添加到购物车”多个选项 - How to Observe 'add to cart' multiple option of a product 如何在购物车中添加产品说明 - How to add instruction with product in Cart 如何将产品尺寸添加到购物车? - How to add product size to cart? 自定义添加到购物车按钮,将多个产品添加到购物车中,数量:woocommerce - Custom add to cart button to add multiple product into cart with quantity :woocommerce Magento:选中复选框同时将多个产品添加到购物车 - Magento: Add multiple product to the cart simultaneously with checkbox 添加到购物车多个产品变异woocommerce - add to cart multiple product variation woocommerce WooCommerce:将产品作为不同的项目多次添加到购物车,但按其元数据分组 - WooCommerce: add product to cart multiple times as different items but grouped by its meta data 如何将产品再次添加到购物车或“添加到购物车”按钮不起作用? - How to add the product again to the cart or add to cart button not working? WooCommerce-如何使用具有相同购物车按钮的Ajax将多种不同的产品添加到购物车? - WooCommerce - How can I add multiple, different products to cart using ajax with same add-to-cart-Button? 如何将不同表格中的多个产品添加到一个购物车中 - How to add multiple products into one cart from different tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM