Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. PAID_ITEM_COUNT = 9
  2. DISCOUNTED_ITEM_COUNT = 1
  3.  
  4. # Returns the integer amount of items that must be discounted next
  5. # given the amount of items seen
  6. #
  7. def discounted_items_to_find(total_items_seen, discounted_items_seen)
  8. Integer(total_items_seen / (PAID_ITEM_COUNT + DISCOUNTED_ITEM_COUNT) * DISCOUNTED_ITEM_COUNT) - discounted_items_seen
  9. end
  10.  
  11. # Partitions the items and returns the items that are to be discounted.
  12. #
  13. # Arguments
  14. # ---------
  15. #
  16. # * cart
  17. # The cart to which split items will be added (typically Input.cart).
  18. #
  19. # * line_items
  20. # The selected items that are applicable for the campaign.
  21. #
  22. def partition(cart, line_items)
  23. # Sort the items by price from high to low
  24. sorted_items = line_items.sort_by{|line_item| line_item.variant.price}
  25. # Create an array of items to return
  26. discounted_items = []
  27. # Keep counters of items seen and discounted, to avoid having to recalculate on each iteration
  28. total_items_seen = 0
  29. discounted_items_seen = 0
  30. total_items = 0
  31.  
  32. sorted_items.each do |line_item|
  33. if line_item.variant.product.product_vendor == 'STLTH_PODS' and !(line_item.variant.product.tags.include?("combo"))
  34. total_items += line_item.quantity
  35. end
  36. end
  37.  
  38. # Loop over all the items and find those to be discounted
  39. sorted_items.each do |line_item|
  40. if line_item.variant.product.product_vendor == 'STLTH_PODS' and !(line_item.variant.product.tags.include?("combo"))
  41. total_items_seen += line_item.quantity
  42. # After incrementing total_items_seen, see if any items must be discounted
  43. count = discounted_items_to_find(total_items, discounted_items_seen)
  44. # If there are none, skip to the next item
  45. next if count <= 0
  46.  
  47. if count >= line_item.quantity
  48. # If the full item quantity must be discounted, add it to the items to return
  49. # and increment the count of discounted items
  50. discounted_items.push(line_item)
  51. discounted_items_seen += line_item.quantity
  52. else
  53. # If only part of the item must be discounted, split the item
  54. discounted_item = line_item.split(take: count)
  55. # Insert the newly-created item in the cart, right after the original item
  56. position = cart.line_items.find_index(line_item)
  57. cart.line_items.insert(position + 1, discounted_item)
  58. # Add it to the list of items to return
  59. discounted_items.push(discounted_item)
  60. discounted_items_seen += discounted_item.quantity
  61. end
  62. end
  63. end
  64.  
  65. # Return the items to be discounted
  66. discounted_items
  67. end
  68.  
  69. eligible_items = Input.cart.line_items.select do |line_item|
  70. product = line_item.variant.product
  71. !product.gift_card?
  72. end
  73.  
  74. discounted_line_items = partition(Input.cart, eligible_items)
  75. discounted_line_items.each do |line_item|
  76. line_item.change_line_price(Money.zero, message: "(PROMO)")
  77. end
  78.  
  79. Output.cart = Input.cart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement