Shopify脚本-

Shopify脚本-,shopify,Shopify,我想结合Shopify提供的两个示例脚本: () () 因此,折扣仅适用于具有特定标签(例如折扣产品)的产品,适用于具有特定标签(客户折扣)的客户 我已经尝试了好几次,但似乎都没能成功 下面是一个尝试组合这两个脚本的示例: # ================================ Customizable Settings ================================ # ==============================================

我想结合Shopify提供的两个示例脚本:

() ()

因此,折扣仅适用于具有特定标签(例如折扣产品)的产品,适用于具有特定标签(客户折扣)的客户

我已经尝试了好几次,但似乎都没能成功

下面是一个尝试组合这两个脚本的示例:

# ================================ Customizable Settings ================================
# ================================================================
# Discounts by Customer Tag
#
# If a customer is tagged (or not, depending on the setting below)
# with the entered tag(s), the entered discount will be applied to
# each item in the cart.
#
#   - 'customer_tag_match_type' determines whether we look for the customer
#     to be tagged with any of the entered tags or not. Can be:
#       - ':include' to check if the customer is tagged
#       - ':exclude' to make sure the customer isn't tagged
#   - 'customer_tags' is a list of tags to identify qualified
#     customers
#   - 'discount_type' is the type of discount to provide. Can be
#     either:
#       - ':percent'
#       - ':dollar'
#   - 'discount_amount' is the percentage/dollar discount to
#     apply (per item)
#   - 'discount_message' is the message to show when a discount
#     is applied
# ================================================================
DISCOUNTS_FOR_CUSTOMER_TAG = [
  {
    customer_tag_match_type: :include,
    customer_tags: ["VIP"],
    product_selector_match_type: :include,
    product_selector_type: :tag,
    product_selectors: ["your_tag"],
    discount_type: :percent,
    discount_amount: 20,
    discount_message: "Discount for VIP customers!",
  },
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# CustomerTagSelector
#
# Finds whether the supplied customer has any of the entered tags.
# ================================================================
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

# ================================ Script Code (do not edit) ================================
# ================================================================
# ProductSelector
#
# Finds matching products by the entered criteria.
# ================================================================
class ProductSelector
  def initialize(match_type, selector_type, selectors)
    @match_type = match_type
    @comparator = match_type == :include ? 'any?' : 'none?'
    @selector_type = selector_type
    @selectors = selectors
  end

  def match?(line_item)
    if self.respond_to?(@selector_type)
      self.send(@selector_type, line_item)
    else
      raise RuntimeError.new('Invalid product selector type')
    end
  end

  def tag(line_item)
    product_tags = line_item.variant.product.tags.map { |tag| tag.downcase.strip }
    @selectors = @selectors.map { |selector| selector.downcase.strip }
    (@selectors & product_tags).send(@comparator)
  end

  def type(line_item)
    @selectors = @selectors.map { |selector| selector.downcase.strip }
    (@match_type == :include) == @selectors.include?(line_item.variant.product.product_type.downcase.strip)
  end

  def vendor(line_item)
    @selectors = @selectors.map { |selector| selector.downcase.strip }
    (@match_type == :include) == @selectors.include?(line_item.variant.product.vendor.downcase.strip)
  end

  def product_id(line_item)
    (@match_type == :include) == @selectors.include?(line_item.variant.product.id)
  end

  def variant_id(line_item)
    (@match_type == :include) == @selectors.include?(line_item.variant.id)
  end

  def all(line_item)
    true
  end
end


# ================================================================
# DiscountApplicator
#
# Applies the entered discount to the supplied line item.
# ================================================================
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(cents: 100) * discount_amount
    end
  end

  def apply(line_item)
    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

# ================================================================
# DiscountForCustomerTagCampaign
#
# If we have a "matching" customer (by tag), the entered discount
# is applied.
# ================================================================
class DiscountForCustomerTagCampaign
  def initialize(discounts)
    @discounts = discounts
  end

  def run(cart)
    return unless cart.customer&.tags

    @discounts.each do |discount|
      customer_tag_selector = CustomerTagSelector.new(discount[:customer_tag_match_type], discount[:customer_tags])

      next unless customer_tag_selector.match?(cart.customer)

      discount_applicator = DiscountApplicator.new(
        discount[:discount_type],
        discount[:discount_amount],
        discount[:discount_message]
      )
    @campaigns.each do |campaign|
      product_selector = ProductSelector.new(
        campaign[:product_selector_match_type],
        campaign[:product_selector_type],
        campaign[:product_selectors],
      )


      cart.line_items.each do |line_item|
        discount_applicator.apply(line_item)
      end
    end
  end
end

CAMPAIGNS = [
  DiscountForCustomerTagCampaign.new(DISCOUNTS_FOR_CUSTOMER_TAG),
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart

你具体尝试了什么?你在哪里遇到了麻烦?请更具体一点,这样我们可以帮助你。如果你的问题太广泛,我们就无法解决。@user1438038我用一个试图组合脚本的示例更新了原始帖子。