Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4622

Shopify Ruby Script Error - discount code requirements not met (empty cart)

$
0
0

I am working on a script that would block promo code usage for products that are tagged with 'sale' - I am seeing an error message on line 1 of my script and I'm not sure how to resolve it. The script otherwise functions fine and works as expected.

Any suggestions on how to resolve this error?

# ================================ Customizable Settings ================================# ================================================================# Define the tag for products where discounts are not allowed.# ================================================================SALE_TAGS = ['sale', 'Sale'] # Tags used to identify sale productsREJECTION_MESSAGE = "Discount codes can't be used with Sale products"# ================================ Script Code (do not edit) ================================# ================================================================# ProductSelector# Finds matching products by the entered criteria.# ================================================================class ProductSelector  def initialize(tags)    @tags = tags.map(&:downcase)  end  def match?(line_item)    product_tags = line_item.variant.product.tags.map(&:downcase)    (@tags & product_tags).any?  endend# ================================================================# DisableDiscountCodesForProductsCampaign# If any matching items are in the cart, the discount code is rejected.# ================================================================class DisableDiscountCodesForProductsCampaign  def initialize(tags, message)    @product_selector = ProductSelector.new(tags)    @rejection_message = message  end  def run(cart)    return if cart.discount_code.nil?    if cart.line_items.any? { |line_item| @product_selector.match?(line_item) }      cart.discount_code.reject(message: @rejection_message)    end  endend# ================================================================# Campaign Execution# ================================================================campaign = DisableDiscountCodesForProductsCampaign.new(SALE_TAGS, REJECTION_MESSAGE)campaign.run(Input.cart)Output.cart = Input.cart

enter image description here


Viewing all articles
Browse latest Browse all 4622

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>