ruby 一个折扣不适用Shopify脚本

jdzmm42g  于 2023-03-22  发布在  Ruby
关注(0)|答案(1)|浏览(68)

我已经为不同的客户创造了折扣,所有的工作,除了大的一个40%和20% .产品被正确标记.我可以看到,20%被应用,也10%被应用,但40%和20%不是.

DISCOUNTS_FOR_CUSTOMER_TAG = [
  {
    customer_tag_match_type: :include,
    customer_tags: ["Cidesco"],
    discount_type: :percent,
    discount_amount: 10,
    discount_message: "Cidesco atlaide -10% !",
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["Pastāvīgais klients"],
    discount_type: :percent,
    discount_amount: 10,
    discount_message: "Pastāvīgā klienta atlaide -10% !",
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["Studentu atlaide instrumentiem"],
    discount_type: :percent,
    discount_amount: 10,
    discount_message: "Studentu atlaide instrumentiem -10% !",
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["Studentu atlaide instrumentiem"],
    discount_type: :percent,
    discount_amount: 20,
    discount_message: "Studentu atlaide instrumentiem -20% !",
    product_tags: ["Studentu atlaide instrumentiem"],
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["CIDESCO"],
    discount_type: :percent,
    discount_amount: 40,
    discount_message: "CIDESCO -40% !",
    product_tags: ["Cidesco-40"],
  },
  {
    customer_tag_match_type: :include,
    customer_tags: ["CIDESCO"],
    discount_type: :percent,
    discount_amount: 20,
    discount_message: "CIDESCO -20% !",
    product_tags: ["Cidesco-20"],
  },
]
# ==========================================
# Script Code (do not edit)
# ==========================================

class CustomerTagSelector
  def initialize(match_type, tags)
    @comparator = match_type == :include ? 'any?' : 'none?'
    @tags = tags.map { |tag| tag.downcase.strip }
  end

  def match?(customer)
    customer_tags = customer.tags.map { |tag| tag.downcase.strip }
    (@tags & customer_tags).send(@comparator)
  end
end

class ProductTagSelector
  def initialize(match_type, tags)
    @comparator = match_type == :include ? 'any?' : 'none?'
    @tags = tags.map { |tag| tag.downcase.strip }
  end

  def match?(product)
    product_tags = product.tags.map { |tag| tag.downcase.strip }
    (@tags & product_tags).send(@comparator)
  end
end

class DiscountApplicator
  def initialize(discount_type, discount_amount, discount_message)
    @discount_type = discount_type
    @discount_message = discount_message
    @discount_amount = if discount_type == :percent
      1 - (discount_amount * 0.01)
    else
      Money.new(pennies: 100) * discount_amount
    end
  end

  def apply(line_item)
    # check if a discount has already been applied
    return if line_item.line_price_was != line_item.line_price

    new_line_price = if @discount_type == :percent
      line_item.line_price * @discount_amount
    else
      [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
    end

    line_item.change_line_price(new_line_price, message: @discount_message)
  end
end

class DiscountForCustomerTagCampaign
    def initialize(discounts)
      @discounts = discounts
    end
  
    def run(cart)
      return unless cart.customer&.tags
        customer_tag_selector = CustomerTagSelector.new(discount[:customer_tag_match_type], discount[:customer_tags])
        next unless customer_tag_selector.match?(cart.customer)
  
        cart.line_items.each do |line_item|
          case discount[:discount_type]
          when :percent
            discount_applicator = DiscountApplicator.new(discount[:discount_type], discount[:discount_amount], discount[:discount_message])
            if line_item.variant.product.tags.include?("Cidesco-40") && discount[:customer_tags].include?("CIDESCO")
              DiscountApplicator.new(
                :percent,
                40,
                "Cidesco atlaide -40%!"
              ).apply(line_item)
            elsif line_item.variant.product.tags.include?("Cidesco-20") && discount[:customer_tags].include?("CIDESCO")
              DiscountApplicator.new(
                :percent,
                20,
                "Cidesco atlaide -20%!"
              ).apply(line_item)
            elsif line_item.variant.product.tags.include?("Studentu atlaide instrumentiem") && discount[:customer_tags].include?("Studentu atlaide instrumentiem")
              DiscountApplicator.new(
                :percent,
                20,
                "Studentu atlaide -20%!"
              ).apply(line_item)
            else
              discount_applicator.apply(line_item)
            end
          end
        end
      end
    end
  end
  
  CAMPAIGNS = [
    DiscountForCustomerTagCampaign.new(DISCOUNTS_FOR_CUSTOMER_TAG),
  ]
  
  CAMPAIGNS.each do |campaign|
    campaign.run(Input.cart)
  end
  
  Output.cart = Input.cart

我已经为不同的客户创造了折扣,所有的工作,除了大的一个40%和20% .产品被正确标记.我可以看到,20%被应用,也10%被应用,但40%和20%不是.

vawmfj5a

vawmfj5a1#

发现错误- CIDESCO应该在打字没有大写字母。当改变为Cidesco的折扣开始适用。

相关问题