Using acts_as_shopping_cart how do I implement basic quantity editing?

纵饮孤独 提交于 2019-11-29 18:09:21
Valery Kvon

Quantity:

Actually, I don't know acts_as_shopping cart and what exactly it do. But anyway look at the 'standard' realization in Agile Web Development with Rails (4ed), page 117. You can just steal those cart to your application, or whole the application. I think this book is about you. :)

Product properties:

It is VERY important to clarify the problem from the beginning and its potential complications due to the demands of the owner. So you do not have trouble link there.

Any 'attributes' have to be known and classified carefully. Let say, color is a simple anywhere (red is red in america, africa or europe), but the size - is not. Size of what? Cloth size depends on region dimension, culture, cloth type etc and can be just one value ("M", "S" or "46") and depends on product origin... Size of 3D is a 3 numerics (can be inches, cm, m) and depends on region of sale.

So, due to OOP thinking, you have to write every product 'attribute' in a stand-alone ActiveRecord model (which has_many product_items) but you have to know exactly HOW you will be able to search them as fast as possible.

At the finish i think you accomplish something like that.

size = Size.new(:origin => :us, :value => "m")
size.origin
#=> "us"
size.value
#=> "m"
size.with_analogs # model knows how to translate sizes between each other
#=> [<self>, <#Size @origin: "europe", @value: 46>, <#Size. @origin: "europe", @value: 45.5>, <#Size @origin: "australia", @value: ...>]

So despite the fact that the product item has (actually belongs_to) only one particular size, you can find a similar size of similar goods like that:

product = Product.find(1)
Product.where(:name => "%#{product.name}", :size => {:id => [product.size.with_analogs.map(&:id)]})

Or you can store in the database common measurement learning STI how to translate them, but it little bit complicated.

class Size
class EuropeSize < Size     # STI
class UsSize < Size         # STI
class AustraliaSize < Size  # STI

...

Here Is how I add/edit the quantity

  1. On the shop page, I create a new shopping cart and store its id in the session
  2. Adding items to the shopping cart is handled using Ajax calls that will return the new cart items html to update a div in the header
  3. On the checkout page, there is quantity edit textbox for each item.
  4. On change of the quantity, I make an ajax call with both the new quantity and product_id (you could do it using cart_item_id)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!