Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # enumerate and count by tag
- # get items matching tag "x" from cart as array[]
- product_x_items = Input.cart.line_items.select do |line_item|
- product = line_item.variant.product # get product
- !product.gift_card? && product.tags.include?("x") # return boolean to selector
- end
- # get total number of items
- match_count = product_x_items.inject(0) { |sum, p| sum + p.quantity } # https://apidock.com/rails/Enumerable/sum
- puts "Items matching x:"
- puts match_count # print value to the console
- # ---
- # enumerate and count by tags
- # tags to match
- DISCOUNT_TAGS = [ "discount_1", "discount_2" ]
- tagged_items = Input.cart.line_items.select do |line_item|
- product = line_item.variant.product # get product
- # compare product tags and tags we're looking for, either method below works
- # https://apidock.com/ruby/Array/include%3F
- !product.gift_card? && product.tags & DISCOUNT_TAGS != [] # return boolean
- #!product.gift_card? && product.tags.any? { |i| DISCOUNT_TAGS.include?(i) } # return boolean
- end
- # get total number of items
- tagged_items_count = tagged_items.inject(0) { |sum, p| sum + p.quantity } # https://apidock.com/rails/Enumerable/sum
- puts "Tagged Items:"
- puts tagged_items_count # print value to the console
- Output.cart = Input.cart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement