ตัวอย่างสคริปต์อัตราค่าจัดส่ง

สคริปต์การจัดส่งเกี่ยวโยงกับอัตราค่าจัดส่งและสามารถเปลี่ยนชื่อ การแสดงผล ลำดับที่ปรากฏ และราคาของอัตราค่าจัดส่งได้ สคริปต์เหล่านี้จะทำงานทุกครั้งที่ลูกค้าเข้าถึงหน้าตัวเลือกการจัดส่งในขั้นตอนการชำระเงิน

หากต้องการใช้เทมเพลตในหน้านี้ ให้สร้างสคริปต์ใหม่ด้วยเทมเพลตเปล่า

ขั้นตอน:

  1. จากส่วน Shopify admin ให้ไปที่แอป > Script Editor
  2. คลิกสร้างสคริปต์
  3. คลิกอัตราค่าจัดส่ง
  4. เลือกเทมเพลตเปล่า แล้วคลิกสร้างสคริปต์
  5. ในส่วนซอร์สโค้ดของ Ruby ให้ลบบรรทัดโค้ดเริ่มต้น: Output.cart = Input.cart
  6. คัดลอกสคริปต์จากหน้านี้แล้ววางในส่วนซอร์สโค้ดของ Ruby
  7. แก้ไขส่วนการตั้งค่าแบบปรับแต่งเองของสคริปต์เพื่อให้ทำงานในร้านค้าของคุณ
  8. ทดสอบสคริปต์ของคุณ หากต้องการข้อมูลเพิ่มเติม ให้ไปที่การทดสอบและแก้จุดบกพร่อง Shopify Scripts
  9. หลังจากการทดสอบ: - คลิกบันทึกแบบร่างเพื่อบันทึกแบบร่างที่ยังไม่ได้เผยแพร่ของสคริปต์ หรือ
    • คลิกบันทึกและเผยแพร่เพื่อสร้างและเผยแพร่สคริปต์

เพิ่มข้อความไปยังชื่อของอัตราค่าจัดส่งสำหรับบางจังหวัดหรือประเทศ

ใช้สคริปต์นี้เพื่อเพิ่มข้อความไปยังอัตราค่าจัดส่งที่ปรากฏให้ลูกค้าเห็นในขั้นตอนการชำระเงิน

ตัวอย่างเช่น คุณสามารถใช้สคริปต์นี้เพื่อเพิ่มข้อความลงในอัตราค่าจัดส่งของออนแทรีโอโดยระบุว่า Due to COVID-19 disruptions, shipping may take longer than normal.

# ================================ Customizable Settings ================================
# ================================================================
# Add Message to Rate Names for Province/Country
#
# If the cart's shipping address province/country matches
# the entered settings, all shipping rates will have the
# entered message appended to their name
#
# - 'country_code' is a 2-character abbreviation for the
# applicable country
# - 'province_code' is a list of 2-character abbreviations for
# the applicable provinces
# - 'message' is the message to append to rate names
# ================================================================
RATE_MESSAGE_FOR_PROVINCE_COUNTRY = [
  {
    country_code: "US",
    province_codes: ["NY", "WA"],
    message: "Due to COVID-19 disruptions, shipping may take longer than normal."
  },
  {
    country_code: "CA",
    province_codes: ["BC", "ON"],
    message: "Due to COVID-19 disruptions, shipping may take longer than normal."
  }
]

# ================================ Script Code (do not edit) ===============================
# ================================================================
# ProvinceSelector
#
# Finds whether the supplied province code matches the entered
# strings
# ================================================================
class ProvinceSelector
  def initialize(provinces)
    @provinces = provinces.map { |province| province.upcase.strip }
  end

  def match?(province_code)
    @provinces.any? { |province| province_code.upcase.strip == province }
  end
end

# ================================================================
# AddMessageToRateForProvinceCountryCampaign
#
# If the cart's shipping address country/province matches the
# entered settings, all shipping rates will have the entered
# message appended to their name
# ================================================================
class AddMessageToRateForProvinceCountryCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart, shipping_rates)
    return if cart.shipping_address.nil?

    address = cart.shipping_address

    @campaigns.each do |campaign|
      next unless address.country_code.upcase.strip == campaign[:country_code].upcase.strip

      province_selector = ProvinceSelector.new(campaign[:province_codes])

      next unless province_selector.match?(address.province_code)

      shipping_rates.each do |shipping_rate|
        rate_name = shipping_rate.name + ' - ' + campaign[:message]
        shipping_rate.change_name(rate_name)
      end
    end
  end
end

CAMPAIGNS = [
  AddMessageToRateForProvinceCountryCampaign.new(RATE_MESSAGE_FOR_PROVINCE_COUNTRY),
]

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

Output.shipping_rates = Input.shipping_rates

แสดงอัตราค่าจัดส่งสำหรับรหัสไปรษณีย์ จังหวัด หรือประเทศแบบเฉพาะเจาะจง

ใช้สคริปต์นี้เพื่อแสดงให้ลูกค้าจากตำแหน่งที่ตั้งบางแห่งเห็นเฉพาะอัตราค่าจัดส่งที่คุณป้อน โดยซ่อนอัตราค่าจัดส่งอื่นๆ ไว้

ตัวอย่างเช่น คุณสามารถใช้สคริปต์นี้เพื่อแสดงอัตราค่าจัดส่งที่เฉพาะเจาะจงสำหรับลูกค้าในรัฐแคลิฟอร์เนีย โดยซ่อนอัตราค่าจัดส่งอื่นๆ ทั้งหมดไว้

# ================================ Customizable Settings ================================
# ================================================================
# Show Rate(s) for Zip/Province/Country
#
# If the cart's shipping address country/province/zip match the
# entered settings, the entered rate(s) are shown, and all other
# rates are hidden. Otherwise, the entered rate(s) are hidden.
#
# - 'country_code' is a 2-character abbreviation for the
# applicable country
# - 'province_code' is a list of 2-character abbreviations for
# the applicable provinces
# - 'zip_code_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'zip_codes' is a list of strings to identify zip codes
# - 'rate_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - ':all' for all rates
# - 'rate_names' is a list of strings to identify rates
# - if using ':all' above, this can be set to 'nil'
# ================================================================
SHOW_RATES_FOR_ZIP_PROVINCE_COUNTRY = [
  {
    country_code: "CA",
    province_code: "BC",
    zip_code_match_type: :partial,
    zip_codes: ["V8T"],
    rate_match_type: :exact,
    rate_names: ["Apparel Rate"],
  },
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# ZipCodeSelector
#
# Finds whether the supplied zip code matches any of the entered
# strings.
# ================================================================
class ZipCodeSelector
  def initialize(match_type, zip_codes)
    @comparator = match_type == :exact ? '==' : 'include?'
    @zip_codes = zip_codes.map { |zip_code| zip_code.upcase.strip }
  end

  def match?(zip_code)
    @zip_codes.any? { |zip| zip_code.to_s.upcase.strip.send(@comparator, zip) }
  end
end

# ================================================================
# RateNameSelector
#
# Finds whether the supplied rate name matches any of the entered
# names.
# ================================================================
class RateNameSelector
  def initialize(match_type, rate_names)
    @match_type = match_type
    @comparator = match_type == :exact ? '==' : 'include?'
    @rate_names = rate_names&.map { |rate_name| rate_name.downcase.strip }
  end

  def match?(shipping_rate)
    if @match_type == :all
      true
    else
      @rate_names.any? { |name| shipping_rate.name.downcase.send(@comparator, name) }
    end
  end
end

# ================================================================
# ShowRatesForZipProvinceCountryCampaign
#
# If the cart's shipping address zip/province/country match the
# entered settings, the entered rate(s) are shown, and all other
# rates are hidden. Otherwise, the entered rate(s) are hidden.
# ================================================================
class ShowRatesForZipProvinceCountryCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart, shipping_rates)
    address = cart.shipping_address

    @campaigns.each do |campaign|
      zip_code_selector = ZipCodeSelector.new(campaign[:zip_code_match_type], campaign[:zip_codes])
      rate_name_selector = RateNameSelector.new(campaign[:rate_match_type], campaign[:rate_names])

      if address.nil?
        full_match = false
      else
        country_match = address.country_code.upcase.strip == campaign[:country_code].upcase.strip
        province_match = address.province_code.upcase.strip == campaign[:province_code].upcase.strip
        zip_match = zip_code_selector.match?(address.zip)
        full_match = country_match && province_match && zip_match
      end

      shipping_rates.delete_if do |shipping_rate|
        rate_name_selector.match?(shipping_rate) != full_match
      end
    end
  end
end

CAMPAIGNS = [
  ShowRatesForZipProvinceCountryCampaign.new(SHOW_RATES_FOR_ZIP_PROVINCE_COUNTRY),
]

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

Output.shipping_rates = Input.shipping_rates

ซ่อนอัตราค่าจัดส่งสำหรับรหัสไปรษณีย์ จังหวัด หรือประเทศแบบเฉพาะเจาะจง

ใช้สคริปต์นี้เพื่อซ่อนอัตราค่าจัดส่งเฉพาะรายการไม่ให้ลูกค้าจากตำแหน่งที่ตั้งบางแห่งเห็น

ตัวอย่างเช่น คุณสามารถใช้สคริปต์นี้เพื่อซ่อนอัตราค่าจัดส่งที่เฉพาะเจาะจงไม่ให้ลูกค้าในรัฐแคลิฟอร์เนียเห็น โดยยังแสดงอัตราค่าจัดส่งอื่นๆ ทั้งหมดที่พวกเขามีสิทธิ์เลือก

# ================================ Customizable Settings ================================
# ================================================================
# Hide Rate(s) for Zip/Province/Country
#
# If the cart's shipping address country/province/zip match the
# entered settings, the entered rate(s) are hidden.
#
# - 'country_code' is a 2-character abbreviation for the
# applicable country
# - 'province_code' is a list of 2-character abbreviations for
# the applicable provinces
# - 'zip_code_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'zip_codes' is a list of strings to identify zip codes
# - 'rate_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - ':all' for all rates
# - 'rate_names' is a list of strings to identify rates
# - if using ':all' above, this can be set to 'nil'
# ================================================================
HIDE_RATES_FOR_ZIP_PROVINCE_COUNTRY = [
  {
    country_code: "CA",
    province_code: "BC",
    zip_code_match_type: :partial,
    zip_codes: ["V8T"],
    rate_match_type: :exact,
    rate_names: ["Apparel Rate"],
  },
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# ZipCodeSelector
#
# Finds whether the supplied zip code matches any of the entered
# strings.
# ================================================================
class ZipCodeSelector
  def initialize(match_type, zip_codes)
    @comparator = match_type == :exact ? '==' : 'include?'
    @zip_codes = zip_codes.map { |zip_code| zip_code.upcase.strip }
  end

  def match?(zip_code)
    @zip_codes.any? { |zip| zip_code.to_s.upcase.strip.send(@comparator, zip) }
  end
end

# ================================================================
# RateNameSelector
#
# Finds whether the supplied rate name matches any of the entered
# names.
# ================================================================
class RateNameSelector
  def initialize(match_type, rate_names)
    @match_type = match_type
    @comparator = match_type == :exact ? '==' : 'include?'
    @rate_names = rate_names&.map { |rate_name| rate_name.downcase.strip }
  end

  def match?(shipping_rate)
    if @match_type == :all
      true
    else
      @rate_names.any? { |name| shipping_rate.name.downcase.send(@comparator, name) }
    end
  end
end

# ================================================================
# HideRatesForZipProvinceCountryCampaign
#
# If the cart's shipping address zip/province/country match the
# entered settings, the entered rate(s) are hidden.
# ================================================================
class HideRatesForZipProvinceCountryCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart, shipping_rates)
    address = cart.shipping_address

    return if address.nil?

    @campaigns.each do |campaign|
      zip_code_selector = ZipCodeSelector.new(campaign[:zip_code_match_type], campaign[:zip_codes])

      country_match = address.country_code.upcase.strip == campaign[:country_code].upcase.strip
      province_match = address.province_code.upcase.strip == campaign[:province_code].upcase.strip
      zip_match = zip_code_selector.match?(address.zip)

      next unless country_match && province_match && zip_match

      rate_name_selector = RateNameSelector.new(campaign[:rate_match_type], campaign[:rate_names])

      shipping_rates.delete_if do |shipping_rate|
        rate_name_selector.match?(shipping_rate)
      end
    end
  end
end

CAMPAIGNS = [
  HideRatesForZipProvinceCountryCampaign.new(HIDE_RATES_FOR_ZIP_PROVINCE_COUNTRY),
]

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

Output.shipping_rates = Input.shipping_rates

ซ่อนอัตราค่าจัดส่งสำหรับสินค้าเฉพาะรายการในบางประเทศ

ใช้สคริปต์นี้เพื่อซ่อนอัตราค่าจัดส่งไม่ให้ลูกค้าจากตำแหน่งที่ตั้งบางแห่งเห็นเมื่อซื้อสินค้าเฉพาะรายการ

ตัวอย่างเช่น คุณสามารถใช้สคริปต์นี้เพื่อซ่อนอัตราค่าจัดส่งเฉพาะรายการไม่ให้ลูกค้าในรัฐแคลิฟอร์เนียเห็นหากพวกเขาซื้อเสื้อยืด

# ================================ Customizable Settings ================================
# ================================================================
# Hide Rate(s) for Product/Country
#
# If the cart contains any "matching" items, and the shipping
# address country matches the entered country, the entered rate(s)
# are hidden.
#
# - '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
# - ':all' for all products
# - 'product_selectors' is a list of tags or IDs to identify
# associated products
# - 'country_codes' is a list of country code abbreviations
# - ie. United States would be `US`
# - 'rate_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - ':all' for all rates
# - 'rate_names' is a list of strings to identify rates
# - if using ':all' above, this can be set to 'nil'
# ================================================================
HIDE_RATES_FOR_PRODUCT_AND_COUNTRY = [
  {
    product_selector_match_type: :include,
    product_selector_type: :product_id,
    product_selectors: [1234567890987, 1234567890986],
    country_codes: ["CA"],
    rate_match_type: :all,
    rate_names: nil,
  },
]

# ================================ 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

# ================================================================
# CountrySelector
#
# Finds whether the supplied country code matches the entered
# string
# ================================================================
class CountrySelector
  def initialize(countries)
    @countries = countries.map { |country| country.upcase.strip }
  end

  def match?(country_code)
    @countries.any? { |country| country_code.upcase.strip == country }
  end
end

# ================================================================
# RateNameSelector
#
# Finds whether the supplied rate name matches any of the entered
# names
# ================================================================
class RateNameSelector
  def initialize(match_type, rate_names)
    @match_type = match_type
    @comparator = match_type == :exact ? '==' : 'include?'
    @rate_names = rate_names&.map { |rate_name| rate_name.downcase.strip }
  end

  def match?(shipping_rate)
    if @match_type == :all
      true
    else
      @rate_names.any? { |name| shipping_rate.name.downcase.send(@comparator, name) }
    end
  end
end

# ================================================================
# HideRatesForProductCountryCampaign
#
# If the cart contains any "matching" items, and the shipping
# address country matches the entered country, the entered rate(s)
# are hidden.
# ================================================================
class HideRatesForProductCountryCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart, shipping_rates)
    address = cart.shipping_address

    return if address.nil?

    @campaigns.each do |campaign|
      product_selector = ProductSelector.new(
        campaign[:product_selector_match_type],
        campaign[:product_selector_type],
        campaign[:product_selectors],
      )

      country_selector = CountrySelector.new(campaign[:country_codes])
      product_match = cart.line_items.any? { |line_item| product_selector.match?(line_item) }
      country_match = country_selector.match?(address.country_code)

      next unless product_match && country_match

      rate_name_selector = RateNameSelector.new(
        campaign[:rate_match_type],
        campaign[:rate_names],
      )

      shipping_rates.delete_if do |shipping_rate|
        rate_name_selector.match?(shipping_rate)
      end
    end
  end
end

CAMPAIGNS = [
  HideRatesForProductCountryCampaign.new(HIDE_RATES_FOR_PRODUCT_AND_COUNTRY),
]

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

Output.shipping_rates = Input.shipping_rates

ซ่อนอัตราค่าจัดส่งสำหรับตู้ไปรษณีย์

ใช้สคริปต์นี้เพื่อซ่อนอัตราค่าจัดส่งไม่ให้ลูกค้าเห็นหากที่อยู่ที่จัดส่งของพวกเขาเป็นตู้ไปรษณีย์

# ================================ Customizable Settings ================================
# ================================================================
# Hide Rate(s) for PO Box addresses
#
# If the shipping address contains any of the entered "PO Box"
# identifiers, the entered rate(s) are hidden.
#
# - 'po_box_triggers' is a list of possible strings for a PO
# Box address
# - 'province_codes' is a list of province/state abbreviations where
# we care about PO Boxes
# - 2 character abbreviation
# - 'country_code' is the country abbreviation to match the
# provinces above:
# - 2 character abbreviation
# - 'rate_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - ':all' for all rates
# - 'rate_names' is a list of rates to show/hide if the above
# conditions are met
# ================================================================
HIDE_RATES_FOR_PO_BOX = [
  {
    po_box_triggers: [
      "po box", "post office", "p o box", "p.o.box", "p.o. box", "p.o box", "pobox",
      "post office box", "post box", "p. o. box", "po. box", "postal box",
    ],
    province_codes: ["BC", "AB"],
    country_code: "CA",
    rate_match_type: :exact,
    rate_names: ["Shipping Rate", "Other Shipping Rate"],
  },
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# ProvinceSelector
#
# Finds whether the supplied province code matches the entered
# strings.
# ================================================================
class ProvinceSelector
  def initialize(provinces)
    @provinces = provinces.map { |province| province.upcase.strip }
  end

  def match?(province_code)
    @provinces.any? { |province| province_code.upcase.strip == province }
  end
end

# ================================================================
# AddressSelector
#
# Finds whether the supplied address contains any of the entered
# strings.
# ================================================================
class AddressSelector
  def initialize(triggers)
    @triggers = triggers.map { |trigger| trigger.downcase.strip }
  end

  def match?(address)
    address_fields = [address.address1, address.address2].map do |line|
      line.nil? ? "" : line.downcase
    end

    address_fields = address_fields.join(" ")
    @triggers.any? { |trigger| address_fields.include?(trigger) }
  end
end

# ================================================================
# RateNameSelector
#
# Finds whether the supplied rate name matches any of the entered
# names.
# ================================================================
class RateNameSelector
  def initialize(match_type, rate_names)
    @match_type = match_type
    @comparator = match_type == :exact ? '==' : 'include?'
    @rate_names = rate_names.map { |rate_name| rate_name.downcase.strip }
  end

  def match?(shipping_rate)
    if @match_type == :all
      true
    else
      @rate_names.any? { |name| shipping_rate.name.downcase.send(@comparator, name) }
    end
  end
end

# ================================================================
# HideRatesForPOBoxCampaign
#
# If the shipping address contains any of the entered "PO Box"
# identifiers, the entered rate(s) are hidden.
# ================================================================
class HideRatesForPOBoxCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart, shipping_rates)
    address = cart.shipping_address

    return if address.nil?

    @campaigns.each do |campaign|
      country_match = address.country_code.upcase.strip == campaign[:country_code]
      province_match = ProvinceSelector.new(campaign[:province_codes]).match?(address.province_code)
      address_match = AddressSelector.new(campaign[:po_box_triggers]).match?(address)

      next unless country_match && province_match && address_match

      rate_name_selector = RateNameSelector.new(
        campaign[:rate_match_type],
        campaign[:rate_names],
      )

      shipping_rates.delete_if do |shipping_rate|
        rate_name_selector.match?(shipping_rate)
      end
    end
  end
end

CAMPAIGNS = [
  HideRatesForPOBoxCampaign.new(HIDE_RATES_FOR_PO_BOX),
]

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

Output.shipping_rates = Input.shipping_rates

แสดงอัตราค่าจัดส่งตามแถบป้ายลูกค้า

ใช้สคริปต์นี้เพื่อแสดงอัตราค่าจัดส่งแก่ลูกค้าเฉพาะราย

ตัวอย่างเช่น คุณสามารถใช้สคริปต์นี้เพื่อเสนออัตราค่าจัดส่งพิเศษให้แก่ลูกค้าที่มีแถบป้าย VIP

# ================================ Customizable Settings ================================
# ================================================================
# Show Rate(s) for Customer Tag
#
# If a customer is tagged (or not, depending on the setting below)
# with the entered tag(s), the entered rate(s) will be shown, and
# all others will be hidden. Otherwise, the entered rate(s) will
# be hidden.
#
# - '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 customer tags to trigger the
# campaign
# - 'rate_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'rate_names' is a list of strings to identify rates
# ================================================================
SHOW_RATES_FOR_CUSTOMER_TAG = [
  {
    customer_tag_match_type: :include,
    customer_tags: ["customer_tag", "another_tag"],
    rate_match_type: :exact,
    rate_names: ["Shipping Rate", "Other Shipping Rate"],
  },
]

# ================================ 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

# ================================================================
# RateNameSelector
#
# Finds whether the supplied rate name matches any of the entered
# names.
# ================================================================
class RateNameSelector
  def initialize(match_type, rate_names)
    @comparator = match_type == :exact ? '==' : 'include?'
    @rate_names = rate_names.map { |rate_name| rate_name.downcase.strip }
  end

  def match?(shipping_rate)
    @rate_names.any? { |name| shipping_rate.name.downcase.send(@comparator, name) }
  end
end

# ================================================================
# ShowRateForCustomerTagCampaign
#
# If a customer is tagged (or not, depending on the setting below)
# with the entered tag(s), the entered rate(s) will be shown, and
# all others will be hidden. Otherwise, the entered rate(s) will
# be hidden.
# ================================================================
class ShowRateForCustomerTagCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

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

      customer_match = cart.customer.nil? ? false : customer_tag_selector.match?(cart.customer)

      rate_name_selector = RateNameSelector.new(
        campaign[:rate_match_type],
        campaign[:rate_names]
      )

      shipping_rates.delete_if do |shipping_rate|
        rate_name_selector.match?(shipping_rate) != customer_match
      end
    end
  end
end

CAMPAIGNS = [
  ShowRateForCustomerTagCampaign.new(SHOW_RATES_FOR_CUSTOMER_TAG),
]

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

Output.shipping_rates = Input.shipping_rates

จัดลำดับอัตราค่าจัดส่งใหม่

ใช้สคริปต์นี้เพื่อเปลี่ยนแปลงลำดับอัตราค่าจัดส่งเริ่มต้นที่เสนอให้ลูกค้า

# ================================ Customizable Settings ================================
# ================================================================
# The order in which you would like your rates to display
# ================================================================
DESIRED_RATE_ORDER = [
  "Shipping Rate 1", "Shipping Rate 2", "Shipping Rate 3",
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# ReorderRatesCampaign
#
# Reorders rates into the entered order
# ================================================================
class ReorderRatesCampaign
  def initialize(desired_order)
    @desired_order = desired_order.map { |item| item.downcase.strip }
  end

  def run(cart, shipping_rates)
    shipping_rates.sort_by! { |rate| @desired_order.index(rate.name.downcase.strip) || Float::INFINITY }
  end
end

CAMPAIGNS = [
  ReorderRatesCampaign.new(DESIRED_RATE_ORDER),
]

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

Output.shipping_rates = Input.shipping_rates

ส่วนลดอัตราค่าจัดส่งตามรหัสส่วนลด

ใช้สคริปต์นี้เพื่อเสนอส่วนลดอัตราค่าจัดส่งหากลูกค้าใช้รหัสส่วนลดในขั้นตอนการชำระเงิน

ตัวอย่างเช่น หากลูกค้าใช้รหัสส่วนลดขั้นตอนการชำระเงินที่เสนอส่วนลด 15% จากคำสั่งซื้อ คุณสามารถเสนอส่วนลด 15% สำหรับอัตราค่าจัดส่งที่พวกเขาเลือกได้เช่นกัน

# ================================ Customizable Settings ================================
# ================================================================
# Discount Rate(s) by Discount Code(s)
#
# If one of the entered discount codes is used, the entered
# rate(s) are discounted by the entered amount
#
# - 'discount_code_match_type' determines whether the below
# strings should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'discount_codes' is a list of strings to identify discount
# codes
# - 'rate_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'rate_names' is a list of strings to identify rates
# - 'discount_type' is the type of discount to provide. Can be
# either:
# - ':percent'
# - ':dollar'
# - 'discount_amount' is the percentage/dollar discount to
# apply
# - 'discount_message' is the message to show when a discount
# is applied
# ================================================================
DISCOUNTS_FOR_DISCOUNT_CODES = [
  {
    discount_code_match_type: :exact,
    discount_codes: ["TESTCODE1", "TESTCODE2"],
    rate_match_type: :exact,
    rate_names: ["Shipping Rate", "Other Shipping Rate"],
    discount_type: :percent,
    discount_amount: 100,
    discount_message: "Free Shipping with discount code"
  },
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# DiscountCodeSelector
#
# Finds whether the supplied discount code matches any of the
# entered codes.
# ================================================================
class DiscountCodeSelector
  def initialize(match_type, discount_codes)
    @comparator = match_type == :exact ? '==' : 'include?'
    @discount_codes = discount_codes.map { |discount_code| discount_code.upcase.strip }
  end

  def match?(discount_code)
    @discount_codes.any? { |code| discount_code.code.upcase.send(@comparator, code) }
  end
end

# ================================================================
# RateNameSelector
#
# Finds whether the supplied rate name matches any of the entered
# names.
# ================================================================
class RateNameSelector
  def initialize(match_type, rate_names)
    @comparator = match_type == :exact ? '==' : 'include?'
    @rate_names = rate_names.map { |rate_name| rate_name.downcase.strip }
  end

  def match?(shipping_rate)
    @rate_names.any? { |name| shipping_rate.name.downcase.send(@comparator, name) }
  end
end

# ================================================================
# DiscountApplicator
#
# Applies the entered discount to the supplied shipping rate.
# ================================================================
class DiscountApplicator
  def initialize(discount_type, discount_amount, discount_message)
    @discount_type = discount_type
    @discount_message = discount_message

    @discount_amount = if discount_type == :percent
      discount_amount * 0.01
    else
      Money.new(cents: 100) * discount_amount
    end
  end

  def apply(shipping_rate)
    rate_discount = if @discount_type == :percent
      shipping_rate.price * @discount_amount
    else
      @discount_amount
    end

    shipping_rate.apply_discount(rate_discount, message: @discount_message)
  end
end

# ================================================================
# DiscountRatesForDiscountCodeCampaign
#
# Applies the entered discount to the entered rates if the
# any of the entered discount codes are used.
# ================================================================
class DiscountRatesForDiscountCodeCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart, shipping_rates)
    return if cart.discount_code.nil?

    @campaigns.each do |campaign|
      discount_code_selector = DiscountCodeSelector.new(campaign[:discount_code_match_type], campaign[:discount_codes])

      next unless discount_code_selector.match?(cart.discount_code)

      rate_name_selector = RateNameSelector.new(campaign[:rate_match_type], campaign[:rate_names])
      discount_applicator = DiscountApplicator.new(
        campaign[:discount_type],
        campaign[:discount_amount],
        campaign[:discount_message],
      )

      shipping_rates.each do |shipping_rate|
        next unless rate_name_selector.match?(shipping_rate)
        discount_applicator.apply(shipping_rate)
      end
    end
  end
end

CAMPAIGNS = [
  DiscountRatesForDiscountCodeCampaign.new(DISCOUNTS_FOR_DISCOUNT_CODES),
]

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

Output.shipping_rates = Input.shipping_rates

ส่วนลดอัตราค่าจัดส่งตามแถบป้ายลูกค้า

ใช้สคริปต์นี้เพื่อเสนอส่วนลดสำหรับการจัดส่งให้แก่ลูกค้าที่มีแถบป้ายที่ระบุ

ตัวอย่างเช่น คุณสามารถเสนอส่วนลด 10% สำหรับการจัดส่งหากลูกค้ามีแถบป้าย VIP

# ================================ Customizable Settings ================================
# ================================================================
# Discount rates based on customer tag, where:
# - '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 customer tags to qualify for
# a discount
# - 'rate_match_type' determines whether the below strings
# should be an exact or partial match. Can be:
# - ':exact' for an exact match
# - ':partial' for a partial match
# - 'rate_names' is a list of strings to identify rates
# - 'discount_type' is the type of discount to provide. Can be
# either:
# - ':percent'
# - ':dollar'
# - 'discount_amount' is the percentage/dollar discount to
# apply
# - 'discount_message' is the message to show when a discount
# is applied
# ================================================================
DISCOUNTS_FOR_CUSTOMER_TAG = [
  {
    tag_match_type: :include,
    customer_tags: ["customer_tag", "another_tag"],
    rate_match_type: :exact,
    rate_names: ["Shipping Rate", "Other Shipping Rate"],
    discount_type: :percent,
    discount_amount: 10,
    discount_message: "10% off shipping for tagged 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

# ================================================================
# RateNameSelector
#
# Finds whether the supplied rate name matches any of the entered
# names
# ================================================================
class RateNameSelector
  def initialize(match_type, rate_names)
    @comparator = match_type == :exact ? '==' : 'include?'
    @rate_names = rate_names.map { |rate_name| rate_name.downcase.strip }
  end

  def match?(shipping_rate)
    @rate_names.any? { |name| shipping_rate.name.downcase.send(@comparator, name) }
  end
end

# ================================================================
# DiscountApplicator
#
# Applies the entered discount to the supplied shipping rate
# ================================================================
class DiscountApplicator
  def initialize(discount_type, discount_amount, discount_message)
    @discount_type = discount_type
    @discount_message = discount_message

    @discount_amount = if discount_type == :percent
      discount_amount * 0.01
    else
      Money.new(cents: 100) * discount_amount
    end
  end

  def apply(shipping_rate)
    rate_discount = if @discount_type == :percent
      shipping_rate.price * @discount_amount
    else
      @discount_amount
    end

    shipping_rate.apply_discount(rate_discount, message: @discount_message)
  end
end

# ================================================================
# DiscountRatesForCustomerTagCampaign
#
# Applies the entered discount to the entered rates if the
# customer has any of the entered tags
# ================================================================
class DiscountRatesForCustomerTagCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart, shipping_rates)
    return if cart.customer.nil?

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

      next unless customer_tag_selector.match?(cart.customer)

      rate_name_selector = RateNameSelector.new(campaign[:rate_match_type], campaign[:rate_names])
      discount_applicator = DiscountApplicator.new(
        campaign[:discount_type],
        campaign[:discount_amount],
        campaign[:discount_message],
      )

      shipping_rates.each do |shipping_rate|
        next unless rate_name_selector.match?(shipping_rate)
        discount_applicator.apply(shipping_rate)
      end
    end
  end
end

CAMPAIGNS = [
  DiscountRatesForCustomerTagCampaign.new(DISCOUNTS_FOR_CUSTOMER_TAG),
]

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

Output.shipping_rates = Input.shipping_rates

แสดงเฉพาะอัตราที่คำนวณแล้วจากผู้ให้บริการจัดส่ง

ใช้สคริปต์นี้เพื่อแสดงอัตราค่าจัดส่งที่คำนวณแล้วจากผู้ให้บริการจัดส่งเท่านั้น หากอัตราค่าจัดส่งที่คำนวณแล้วจากผู้ให้บริการจัดส่งไม่พร้อมใช้งาน ระบบจะแสดงอัตราค่าจัดส่งที่สร้างด้วยตนเองของคุณ

# ================================================================
# Carrier Calculated Rate Fallback
#
# As long as carrier calculated rates are available, Shopify rates
# will be hidden.
# ================================================================
class CalculatedRateFallbackCampaign
  def run(shipping_rates)
    has_calculated_rates = shipping_rates.any?{ |shipping_rate| shipping_rate.source.downcase != 'shopify' }

    return unless has_calculated_rates

    shipping_rates.delete_if { |shipping_rate| shipping_rate.source.downcase == 'shopify' }
  end
end

CAMPAIGNS = [
  CalculatedRateFallbackCampaign.new(),
]

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

Output.shipping_rates = Input.shipping_rates

พร้อมเริ่มต้นการขายด้วย Shopify แล้วหรือยัง

ทดลองใช้งานฟรี