Multiple Inline Scripts

liveyoung
New Member
3 0 0

Hi,

We have multiple "in-line" scripts and right now Script Editor allows for ONLY ONE to be published. We have spoken to your support and they have advised us to contact you here.

They mentioned there could be a possibility to have these 2 combined together as one script. 

How could we do this?

0 Likes
_JB
Shopify Staff
Shopify Staff
809 95 176

Hey @liveyoung,

If you provide examples of the scripts there may be someone in the community who can suggest a method to combine them.

Another option is to request our Shopify Plus front end development team to have a look, this is the way to go if you're looking for specific code help from Shopify. You can request this from the support advisor you were speaking with.

JB | Developer Support @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes
liveyoung
New Member
3 0 0
Please see below

We have 2 inline type script

#1 is when user has $100 or more they get a Free T Shirt offer in theirs
cart

#2 is to check and see if user has a tag called "custom coaching" and if
they do they get automatic 20% off

both script are below separately.

it would be amazing if shopify had a script mergin tool

================ $100 Free Tshirt Offer code ====================

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 ConditionalDiscount < Campaign
def initialize(condition, customer_qualifier, cart_qualifier,
line_item_selector, discount, max_discounts)
super(condition, customer_qualifier, cart_qualifier)
@line_item_selector = line_item_selector
@discount = discount
@items_to_discount = max_discounts == 0 ? nil : max_discounts
end

def run(cart)
raise "Campaign requires a discount" unless @discount
return unless qualifies?(cart)
applicable_items = cart.line_items.select { |item|
@line_item_selector.nil? || @line_item_selector.match?(item) }
applicable_items = applicable_items.sort_by { |item| item.variant.price
}
applicable_items.each do |item|
break if @items_to_discount == 0
if (!@items_to_discount.nil? && item.quantity > @items_to_discount)
discounted_items = item.split(take: @items_to_discount)
@discount.apply(discounted_items)
cart.line_items << discounted_items
@items_to_discount = 0
else
@discount.apply(item)
@items_to_discount -= item.quantity if !@items_to_discount.nil?
end
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 CustomerTotalSpentQualifier < Qualifier
def initialize(comparison_type, amount)
@comparison_type = comparison_type
@amount = Money.new(cents: amount * 100)
end

def match?(cart, selector = nil)
return false if cart.customer.nil?
total = cart.customer.total_spent
compare_amounts(total, @comparison_type, @amount)
end
end

class CartAmountQualifier < Qualifier
def initialize(behaviour, comparison_type, amount)
@behaviour = behaviour
@comparison_type = comparison_type
@amount = Money.new(cents: amount * 100)
end

def match?(cart, selector = nil)
total = cart.subtotal_price
if @behaviour == :item || @behaviour == :diff_item
total = cart.line_items.reduce(Money.zero) do |total, item|
total + (selector&.match?(item) ? item.line_price : Money.zero)
end
end
case @behaviour
when :cart, :item
compare_amounts(total, @comparison_type, @amount)
when :diff_cart
compare_amounts(cart.subtotal_price_was - @amount,
@comparison_type, total)
when :diff_item
original_line_total = cart.line_items.reduce(Money.zero) do |total,
item|
total + (selector&.match?(item) ? item.original_line_price :
Money.zero)
end
compare_amounts(original_line_total - @amount, @comparison_type,
total)
end
end
end

class Selector
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
end

class ProductIdSelector < Selector
def initialize(match_type, product_ids)
@invert = match_type == :not_one
@Product_ids = product_ids.map { |id| id.to_i }
end

def match?(line_item)
@invert ^ @Product_ids.include?(line_item.variant.product.id)
end
end

class PercentageDiscount
def initialize(percent, message)
@discount = (100 - percent) / 100.0
@message = message
end

def apply(line_item)
line_item.change_line_price(line_item.line_price * @discount, message:
@message)
end
end

CAMPAIGNS = [
ConditionalDiscount.new(
:any,
CustomerTotalSpentQualifier.new(
:greater_than,
100
),
CartAmountQualifier.new(
:cart,
:greater_than,
100
),
ProductIdSelector.new(
:is_one,
["6275249111218"]
),
PercentageDiscount.new(
100,
""
),
1
)
].freeze

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

Output.cart = Input.cart

================ END ====================


================ Customer Tag Check for 20% Off ====================

# ================================ Customizable Settings
================================
# ================================================================
# Product Discounts by Customer Tag
#
# If we have a matching customer (by tag), the entered discount
# will be applied to any matching items.
#
# - '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
# - 'product_selector_match_type' determines whether we look for
# products that do or don't match the entered selectors. Can
# be:
# - ':include' to check if the product does match
# - ':exclude' to make sure the product doesn't match
# - 'product_selector_type' determines how eligible products
# will be identified. Can be either:
# - ':tag' to find products by tag
# - ':type' to find products by type
# - ':vendor' to find products by vendor
# - ':product_id' to find products by ID
# - ':variant_id' to find products by variant ID
# - ':subscription' to find subscription products
# - ':all' for all products
# - 'product_selectors' is a list of identifiers (from above)
# for qualifying products. Product/Variant ID lists should
# only contain numbers (ie. no quotes). If ':all' is used,
# this can also be 'nil'.
# - '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: ["Custom Coaching"],
product_selector_match_type: :include,
product_selector_type: :all,
product_selectors: nil,
discount_type: :percent,
discount_amount: 20,
discount_message: "Discount for Custom Coaching 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

# ================================================================
# 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 subscription(line_item)
!line_item.variant.selling_plan_id.nil?
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 to any matching items.
# ================================================================
class DiscountForCustomerTagCampaign
def initialize(campaigns)
@campaigns = campaigns
end

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

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

next unless customer_tag_selector.match?(cart.customer)

product_selector = ProductSelector.new(
campaign[:product_selector_match_type],
campaign[:product_selector_type],
campaign[:product_selectors]
)

discount_applicator = DiscountApplicator.new(
campaign[:discount_type],
campaign[:discount_amount],
campaign[:discount_message]
)

cart.line_items.each do |line_item|
next unless product_selector.match?(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

================ END ====================
0 Likes
liveyoung
New Member
3 0 0

Did you see my scripts example?

0 Likes