Created
November 3, 2017 18:47
-
-
Save ismasan/a67d42959dfc3bee719ca5e22f57f17f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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