Set Static Price By Tag, Script not working, not sure what I'm missing

Highlighted
Tourist
27 0 1

Hey all, 

So this is what I have so far, but it's not seeming to work, I'm trying to set items with a specific tag to $50

 

class StaticPrice
  def initialize(selector)
    @selector = selector
  end
class TagSelector
  def initialize(tag)
    @tag = tag
  end
  def match?(line_item)
    line_item.variant.product.tags.include?(@tag)
  end
end
CAMPAIGNS = [
  StaticPrice.new(
    TagSelector.new("boots"),
    line_item.line_price == (5000), message: "SALE!")
]

Output.cart = Input.cart

 

 

 

0 Likes
Highlighted
Tourist
27 0 1

**** UPDATE... Well I got it to work, however it's extremely bloated and I'm quite sure unprofessional (rookie here), but.. it works.. This allows me to set static prices on products based off tags for a particular sale while at the same time not allowing someone to use a coupon to get any additional price off of the sale item.. I appreciate any suggestions for improvement ****

case Input.cart.discount_code
when CartDiscount::Percentage
          if  Line_items.quantity > 1
            Input.cart.discount_code.reject(message: "Coupons can not be combined with BOGO promotion")
          end
        end

class ItemCampaign
  def initialize(selector, discount, partitioner)
    @selector = selector
    @discount = discount
    @partitioner = partitioner
  end
  def run(cart)
    applicable_items = cart.line_items.select do |line_item|
      @selector.match?(line_item)
    end
    discounted_items = @partitioner.partition(cart, applicable_items)

    discounted_items.each do |line_item|
      @discount.apply(line_item)
    end
  end
end
class TagSelector
  def initialize(tag)
    @tag = tag
  end
  def match?(line_item)
    line_item.variant.product.tags.include?(@tag)
  end
end
class PercentageDiscount50
  def initialize(percent, message)
    @percent = Money.new(cents: 100) * 50
    @message = message
  end
  def apply(line_item)
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 50
    new_line_price = Money.new(cents: 100) * 50
    line_item.change_line_price(new_line_price, message: @message)
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}."
  end
end
class PercentageDiscount40
  def initialize(percent, message)
    @percent = Money.new(cents: 100) * 40
    @message = message
  end
  def apply(line_item)
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 40
    new_line_price = Money.new(cents: 100) * 40
    line_item.change_line_price(new_line_price, message: @message)
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}."
  end
end
class PercentageDiscount30
  def initialize(percent, message)
    @percent = Money.new(cents: 100) * 30
    @message = message
  end
  def apply(line_item)
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 30
    new_line_price = Money.new(cents: 100) * 30
    line_item.change_line_price(new_line_price, message: @message)
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}."
  end
end
class PercentageDiscount20
  def initialize(percent, message)
    @percent = Money.new(cents: 100) * 20
    @message = message
  end
  def apply(line_item)
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 20
    new_line_price = Money.new(cents: 100) * 20
    line_item.change_line_price(new_line_price, message: @message)
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}."
  end
end
class PercentageDiscount10
  def initialize(percent, message)
    @percent = Money.new(cents: 100) * 10
    @message = message
  end
  def apply(line_item)
    line_discount = line_item.line_price - line_item.line_price + Money.new(cents: 100) * 10
    new_line_price = Money.new(cents: 100) * 10
    line_item.change_line_price(new_line_price, message: @message)
    puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}."
  end
end
class LowToHighPartitioner
  def initialize(paid_item_count, discounted_item_count)
    @paid_item_count = paid_item_count
    @discounted_item_count = discounted_item_count
  end
  def partition(cart, applicable_line_items)
    sorted_items = applicable_line_items.sort_by{|line_item| line_item.variant.price}
    total_applicable_quantity = sorted_items.map(&:quantity).reduce(0, :+)
    discounted_items_remaining = Integer(total_applicable_quantity / (@paid_item_count + @discounted_item_count) * @discounted_item_count)
    discounted_items = []
    sorted_items.each do |line_item|
      break if discounted_items_remaining == 0
      discounted_item = line_item
      if line_item.quantity > discounted_items_remaining
        discounted_item = line_item.split(take: discounted_items_remaining)
        position = cart.line_items.find_index(line_item)
        cart.line_items.insert(position + 0, discounted_item)
      end
      discounted_items_remaining -= discounted_item.quantity
      discounted_items.push(discounted_item)
    end
    discounted_items
  end
end
CAMPAIGNS = [
  ItemCampaign.new(
    TagSelector.new("SCRIPT50"),
    PercentageDiscount50.new(10, "$50 FINAL SALE!"),
    LowToHighPartitioner.new(0,1),
  ),
  ItemCampaign.new(
    TagSelector.new("SCRIPT40"),
    PercentageDiscount40.new(10, "$40 FINAL SALE!"),
    LowToHighPartitioner.new(0,1),
  ),
    ItemCampaign.new(
    TagSelector.new("SCRIPT30"),
    PercentageDiscount30.new(10, "$30 FINAL SALE!"),
    LowToHighPartitioner.new(0,1),
  ),
    ItemCampaign.new(
    TagSelector.new("SCRIPT20"),
    PercentageDiscount20.new(10, "$20 FINAL SALE!"),
    LowToHighPartitioner.new(0,1),
  ),
    ItemCampaign.new(
    TagSelector.new("SCRIPT10"),
    PercentageDiscount10.new(10, "$10 FINAL SALE!"),
    LowToHighPartitioner.new(0,1),
  )
]
CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end
Output.cart = Input.cart

 

0 Likes
Highlighted
Tourist
27 0 1

Hey guys, I ran into a problem with this, it is not taking into consideration quantities,  so if I have an item being overwritten for $20,  one is $20,  and 500 are a total of $20.. Looking for a way to have it multiply the line item price by quantity to give a total line price to send to the cart.  Any ideas? 

0 Likes
Highlighted
Tourist
27 0 1

***UPDATE*** 

Got the Qty to work,  just changed this:

    new_line_price = Money.new(cents: 100) * 50

to this:

 

    new_line_price = Money.new(cents: 100) * 50 * line_item.quantity * line_item.quantity

 

For each of my 5 price points.

0 Likes
Highlighted
Shopify Expert
4175 29 400

Are you sure you want to multiply by the quantity twice? Could make things expensive no?

    line_item.quantity * line_item.quantity

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes