Shipping Rate Script to Discount for Specific Gift Cards

Solved
RetailForThePpl
Tourist
5 0 1

I am trying to use the Script Editor on our store to apply a free shipping discount to a group of customers with code specific gift cards that were given at an event. The context is to have it so that the shipping is free and the gift card balance is not going towards it. Or is should I go about it with a different route? I have been testing the Modify Shipping Rate Price template and using the cart qualifier set to 'cart has discount code," but am at a loss for what I am doing wrong. Cannot seem to get it right. Any help is appreciated. Thanks!

The script I have been testing is below:

 

class Campaign
def initialize(condition, *qualifiers)
@condition = (condition.to_s + '?').to_sym
@qualifiers = PostCartAmountQualifier ? [] : [] rescue qualifiers.compact
@line_item_selector = qualifiers.last unless @line_item_selector
qualifiers.compact.each do |qualifier|
is_multi_select = qualifier.instance_variable_get(:@conditions).is_a?(Array)
if is_multi_select
qualifier.instance_variable_get(:@conditions).each do |nested_q|
@post_amount_qualifier = nested_q if nested_q.is_a?(PostCartAmountQualifier)
@qualifiers << qualifier
end
else
@post_amount_qualifier = qualifier if qualifier.is_a?(PostCartAmountQualifier)
@qualifiers << qualifier
end
end if @qualifiers.empty?
end

def qualifies?(cart)
return true if @qualifiers.empty?
@unmodified_line_items = cart.line_items.map do |item|
new_item = item.dup
new_item.instance_variables.each do |var|
val = item.instance_variable_get(var)
new_item.instance_variable_set(var, val.dup) if val.respond_to?(:dup)
end
new_item
end if @post_amount_qualifier
@qualifiers.send(@condition) do |qualifier|
is_selector = false
if qualifier.is_a?(Selector) || qualifier.instance_variable_get(:@conditions).any? { |q| q.is_a?(Selector) }
is_selector = true
end rescue nil
if is_selector
raise "Missing line item match type" if @Li_match_type.nil?
cart.line_items.send(@li_match_type) { |item| qualifier.match?(item) }
else
qualifier.match?(cart, @line_item_selector)
end
end
end

def run_with_hooks(cart)
before_run(cart) if respond_to?(:before_run)
run(cart)
after_run(cart)
end

def after_run(cart)
@discount.apply_final_discount if @discount && @discount.respond_to?(:apply_final_discount)
revert_changes(cart) unless @post_amount_qualifier.nil? || @post_amount_qualifier.match?(cart)
end

def revert_changes(cart)
cart.instance_variable_set(:@line_items, @unmodified_line_items)
end
end

class ShippingDiscount < Campaign
def initialize(condition, customer_qualifier, cart_qualifier, li_match_type, line_item_qualifier, rate_selector, discount)
super(condition, customer_qualifier, cart_qualifier, line_item_qualifier)
@Li_match_type = (li_match_type.to_s + '?').to_sym
@rate_selector = rate_selector
@discount = discount
end

def run(rates, cart)
raise "Campaign requires a discount" unless @discount
return unless qualifies?(cart)
rates.each do |rate|
next unless @rate_selector.nil? || @rate_selector.match?(rate)
@discount.apply(rate)
end
end
end

class Qualifier
def partial_match(match_type, item_info, possible_matches)
match_type = (match_type.to_s + '?').to_sym
if item_info.kind_of?(Array)
possible_matches.any? do |possibility|
item_info.any? do |search|
search.send(match_type, possibility)
end
end
else
possible_matches.any? do |possibility|
item_info.send(match_type, possibility)
end
end
end

def compare_amounts(compare, comparison_type, compare_to)
case comparison_type
when :greater_than
return compare > compare_to
when :greater_than_or_equal
return compare >= compare_to
when :less_than
return compare < compare_to
when :less_than_or_equal
return compare <= compare_to
when :equal_to
return compare == compare_to
else
raise "Invalid comparison type"
end
end
end

class CodeQualifier < Qualifier
def initialize(match_type, match_condition, codes)
@match_condition = match_condition
@invert = match_type == :does_not
@codes = codes.map(&:downcase)
end

def match?(cart, selector = nil)
return false if cart.discount_code.nil?
code = cart.discount_code.code.downcase
case @match_condition
when :match
return @invert ^ @codes.include?(code)
when :start_or_end_with
return @invert ^ @codes.any? { |check| code.start_with?(check) || code.end_with?(check) }
else
return @invert ^ partial_match(@match_condition, code, @codes)
end
end
end

class PercentageDiscount
def initialize(percent, message)
@percent = Decimal.new(percent) / 100
@message = message
end

def apply(rate)
rate.apply_discount(rate.price * @percent, { message: @message })
end
end

CAMPAIGNS = [
ShippingDiscount.new(
:any,
nil,
CodeQualifier.new(
:does,
:include,
["Testcode123", "Testcode456", "Testcode567"]
),
:any,
nil,
nil,
PercentageDiscount.new(
100,
"FREE SHIPPING APPLIED"
)
)
].freeze

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

Output.shipping_rates = Input.shipping_rates

Accepted Solution (1)

Accepted Solutions
playwright-matt
Explorer
59 15 21

This is an accepted solution.

Hey @RetailForThePpl, unfortunately it doesn't look like you can tell whether a customer is using a gift card as a form of payment. Someone else asked a similar question here.

 

However, one of the following options may help:

 

Let me know if that was helpful!
Matthew

Playwright | Create Shopify Scripts without writing code | https://playwrightapp.com
- Was my reply helpful? Please Like and Accept Solution.
- Need help with a custom Shopify script? Email me at matthew@playwrightapp.com

View solution in original post

Replies 2 (2)
playwright-matt
Explorer
59 15 21

This is an accepted solution.

Hey @RetailForThePpl, unfortunately it doesn't look like you can tell whether a customer is using a gift card as a form of payment. Someone else asked a similar question here.

 

However, one of the following options may help:

 

Let me know if that was helpful!
Matthew

Playwright | Create Shopify Scripts without writing code | https://playwrightapp.com
- Was my reply helpful? Please Like and Accept Solution.
- Need help with a custom Shopify script? Email me at matthew@playwrightapp.com

View solution in original post

RetailForThePpl
Tourist
5 0 1

Thank you for providing these they are extremely helpful! Not sure how I missed them! Thanks