简体   繁体   English

如何在在线商店应用程序中建模“产品”

[英]How to model “products” in an online store application

I'm building an online store to sell products like "Green Extra-large, T-shirts". 我正在建立一个网上商店来销售“绿色超大号T恤”等产品。 Ie, the same shirt can have many sizes / colors, different combination can be sold out, different combination might have different prices, etc. 即,同样的衬衫可以有很多尺寸/颜色,不同的组合可以卖光,不同的组合可能有不同的价格等。

My question is how I should model these products in my Rails application (or really how to do it in any application). 我的问题是我应该如何在我的Rails应用程序中建模这些产品(或者实际上如何在任何应用程序中进行建模)。

My current thinking is: 我目前的想法是:

Class Product
  has_many :variants, :through => :characteristics
  has_many :characteristics 
end

Class Characteristic
  belongs_to :product
  belongs_to :variants
end

Class Variant
  has_many :products, :through => :characteristics
  belongs_to :characteristic
end

So each product will have one or more characteristics (eg, "Color", "Size", etc), and each characteristic will then have one or more variants (eg, "Red", "Blue", etc). 因此,每个产品将具有一个或多个特征(例如,“颜色”,“尺寸”等),并且每个特征将具有一个或多个变体(例如,“红色”,“蓝色”等)。

The problem with this method is where do I store price and inventory? 这种方法的问题是我在哪里存储价格和库存? Ie, a given product's price and inventory are determined by the variants its characteristics take. 即,给定产品的价格和库存由其特征所采用的变体决定。 (Green might be more expensive than red, large might be out of stock, etc). (绿色可能比红色更贵,大可能缺货等)。

One thought I had was to give products a "base_price", and let variants modify it, but this seems overly complex (and might not work). 我有一个想法是给产品“base_price”,并让变体修改它,但这看起来过于复杂(并且可能不起作用)。

I have seen two solutions to this kind of dilemma. 我已经看到了解决这种困境的两种方法。 The first is to try to use characteristics to define subordinate products to the "main" product. 第一种是尝试使用特征来定义“主要”产品的从属产品。 The challenge here is that in addition to your thoughts for far, in most cases the product will evolve with new manufacturers that bring new aspects to the table. 这里面临的挑战是,除了您的想法之外,在大多数情况下,产品将随着新的制造商的发展而发展,这些制造商将新的方面带到了桌面上。 For example, one manufacturer may make a cheaper product, but have a different application method for the logo or stitching that may be significant enough to track. 例如,一个制造商可以制造更便宜的产品,但是具有可能足以跟踪的标志或缝合的不同应用方法。

I think that carrying a non significant product number for each product and then attaching the characteristics as attributes works out the best. 我认为每个产品带有一个不重要的产品编号,然后将这些特征作为属性附加起来是最好的。 It is easily searched and extensible. 它易于搜索和扩展。 If a group of products are strongly related, a ProductGroup that the individual products attach to works well. 如果一组产品密切相关,则各个产品附加的ProductGroup效果很好。

In tables: 在表格中:

            ProductGroup
            --------------------
            ProductGroupID
            ProductGroupName
            ProductGroupDescription

            Product
            --------------------
            ProductID
            ProductGroupID
            QtyOnHand
            BasePrice
            ProductColorID
            ProductSizeID

            ProductColor
            ------------
            ProductColorID
            ProductColorName

            ProductSize
            --------------
            ProductSizeID
            ProductSizeName

            ...more attributes...

The advantages here are that you can easily query for specific attributes, attributes are "flexible" in that more can be added (and old ones adjusted: if you started with "Red" but then added another "Red" to the color pool, you can change them to "Maroon" and "Bright Red". 这里的优点是你可以轻松查询特定的属性,属性是“灵活的”,因为可以添加更多(和旧的调整:如果你开始使用“红色”,但后来又添加了另一个“红色”到颜色池,你可以将它们改为“栗色”和“鲜红色”。

You can control price and inventory are at the detail product level (although more tables may be required to account for sourcing costs). 您可以在详细的产品级别控制价格和库存(尽管可能需要更多的表来计算采购成本)。

This all assumes that your characteristics are universally shared. 这一切都假设您的特征是普遍共享的。 If they are not, your characteristic subtable approach can work by creating a join table between characteristics and the product detail tables and populate as needed. 如果不是,则可以通过在特征和产品详细信息表之间创建连接表并根据需要填充来实现特征子表方法。 This will require more business logic .to ensure each product category gets all characteristics necessary. 这将需要更多的业务逻辑。以确保每个产品类别具备必要的所有特征。 In this latter case I would use "prototype" products in the base product table (with Qty and Cost of 0) that I would clone the characteristics from and then adjust as each new product is entered. 在后一种情况下,我会在基本产品表中使用“原型”产品(数量和成本为0),我将从中克隆特征,然后在输入每个新产品时进行调整。 As you move forward, when a new variation appears, having a "clone this product" function that allows you to just adjust the differences from the base product would be valuable. 随着您的前进,当出现新的变化时,拥有“克隆此产品”功能,只允许您调整与基础产品的差异将是有价值的。

Finally, as far as managing the inventory and pricing, this is going to happen at the UI layer. 最后,就管理库存和定价而言,这将发生在UI层。 Being able to generate queries for related products (product groups) and manage all the pricing for related products will go a long way to making this livable. 能够为相关产品(产品组)生成查询并管理相关产品的所有定价将大大有助于实现这一目标。

Just a quick-note. 只是一个快速说明。 You can always try and take a look at the sourcecode of some other e-commerce products like Spree and Substruct they probably already answered that question for you. 您可以随时尝试查看其他一些电子商务产品(如SpreeSubstruct)的源代码,他们可能已经为您解答了这个问题。

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

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