mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-25 01:23:23 +00:00
Working version of the cart integration
This commit is contained in:
@@ -8,14 +8,16 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants)->
|
||||
line_item.variant.line_item = line_item
|
||||
Variants.register line_item.variant
|
||||
|
||||
register_variant: (variant)=>
|
||||
@create_line_item(variant) unless @line_items.some (li)->
|
||||
li.variant == variant
|
||||
line_items_present: =>
|
||||
@line_items.filter (li)->
|
||||
li.quantity > 0
|
||||
|
||||
register_variant: (variant)=>
|
||||
exists = @line_items.some (li)-> li.variant == variant
|
||||
@create_line_item(variant) unless exists
|
||||
|
||||
create_line_item: (variant)->
|
||||
li =
|
||||
variant.line_item =
|
||||
variant: variant
|
||||
quantity: 0
|
||||
variant.line_item = li
|
||||
@line_items.push li
|
||||
|
||||
@line_items.push variant.line_item
|
||||
|
||||
@@ -11,9 +11,9 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Car
|
||||
update: =>
|
||||
@loading = true
|
||||
@products = $resource("/shop/products").query (products)=>
|
||||
@extend()
|
||||
@dereference()
|
||||
@registerVariants()
|
||||
@extend() && @dereference()
|
||||
@registerVariants()
|
||||
@registerVariantsWithCart()
|
||||
@loading = false
|
||||
@
|
||||
|
||||
@@ -21,20 +21,26 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Car
|
||||
for product in @products
|
||||
product.supplier = Enterprises.enterprises_by_id[product.supplier.id]
|
||||
Dereferencer.dereference product.taxons, Taxons.taxons_by_id
|
||||
|
||||
|
||||
# May return different objects! If the variant has already been registered
|
||||
# by another service, we fetch those
|
||||
registerVariants: ->
|
||||
for product in @products
|
||||
if product.variants
|
||||
variants = []
|
||||
product.variants = (Variants.register variant for variant in product.variants)
|
||||
product.master = Variants.register master if product.master
|
||||
product.master = Variants.register product.master if product.master
|
||||
|
||||
registerVariantsWithCart: ->
|
||||
for product in @products
|
||||
if product.variants
|
||||
for variant in product.variants
|
||||
Cart.register_variant variant
|
||||
Cart.register_variant product.master if product.master
|
||||
|
||||
extend: ->
|
||||
for product in @products
|
||||
if product.variants?.length > 0
|
||||
prices = (v.price for v in product.variants)
|
||||
product.price = Math.min.apply(null, prices)
|
||||
|
||||
product.hasVariants = product.variants?.length > 0
|
||||
|
||||
# Iterate through variants
|
||||
# Cart.register_variant(v)
|
||||
|
||||
Reference in New Issue
Block a user