Skip to content

Instantly share code, notes, and snippets.

@ismasan
Created November 3, 2017 18:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismasan/a67d42959dfc3bee719ca5e22f57f17f to your computer and use it in GitHub Desktop.
Save ismasan/a67d42959dfc3bee719ca5e22f57f17f to your computer and use it in GitHub Desktop.
order_code = 'ABCXXX'
variant_id = 123
variant_units = 3 # unidades a agregar
# encuentra pedido existente
order = shop.order(id: order_code) # si no existe, creamos una nueva
# construimos un array de line_items
# a partir de los items del pedido existente
# si es que la variante que queremos agregar
# ya existe, le sumamos las unidades nuevas, en vez de reemplazar
new_line_items = order.line_items.map do |item|
hash = {variant_id: item.variant_id}
hash[:units] = (item.variant_id == variant_id) ? (item.units + variant_units) : variant_units
hash
end
# actualiza pedido
updated_order = order.update_order(line_items: new_line_items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment