ตัวอย่างสคริปต์การชำระเงิน

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

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

ขั้นตอน:

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

แสดงช่องทางให้กับลูกค้าบางราย

ใช้สคริปต์นี้เพื่อแสดงเฉพาะตัวเลือกช่องทางการเข้าถึงเฉพาะลูกค้าที่มีแท็ก

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

# ================================ Customizable Settings ================================# ================================================================# Show Gateways For Customer Tag## If we have a matching customer, the entered gateway(s) will be# shown, and all others will be hidden. Otherwise, the entered# gateway(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# - 'gateway_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# - 'gateway_names' is a list of strings to identify gateways# ================================================================SHOW_GATEWAYS_FOR_CUSTOMER_TAG=[{customer_tag_match_type: :include,customer_tags:["customer_tag","another_tag"],gateway_match_type: :exact,gateway_names:["Gateway","Other Gateway"],},]# ================================ Script Code (do not edit) ================================# ================================================================# CustomerTagSelector## Finds whether the supplied customer has any of the entered tags.# ================================================================classCustomerTagSelectordefinitialize(match_type,tags)@comparator=match_type==:include?'any?''none?'@tags=tags.map{|tag|tag.downcase.strip}enddefmatch?(customer)customer_tags=customer.tags.map{|tag|tag.downcase.strip}(@tags&customer_tags).send(@comparator)endend# ================================================================# GatewayNameSelector## Finds whether the supplied gateway name matches any of the# entered names.# ================================================================classGatewayNameSelectordefinitialize(match_type,gateway_names)@comparator=match_type==:exact?'==''include?'@gateway_names=gateway_names.map{|name|name.downcase.strip}enddefmatch?(payment_gateway)@gateway_names.any?{|name|payment_gateway.name.downcase.strip.send(@comparator,name)}endend# ================================================================# ShowGatewaysForCustomerTagCampaign## If the customer has any of the entered tags, the entered gateways# are shown/hidden depending on the entered settings# ================================================================classShowGatewaysForCustomerTagCampaigndefinitialize(campaigns)@campaigns=campaignsenddefrun(cart,payment_gateways)@campaigns.eachdo|campaign|customer_tag_selector=CustomerTagSelector.new(campaign[:customer_tag_match_type],campaign[:customer_tags],)customer_match=cart.customer.nil??falsecustomer_tag_selector.match?(cart.customer)gateway_name_selector=GatewayNameSelector.new(campaign[:gateway_match_type],campaign[:gateway_names],)payment_gateways.delete_ifdo|payment_gateway|gateway_name_selector.match?(payment_gateway)!=customer_matchendendendendCAMPAIGNS=[ShowGatewaysForCustomerTagCampaign.new(SHOW_GATEWAYS_FOR_CUSTOMER_TAG),]CAMPAIGNS.eachdo|campaign|campaign.run(Input.cart,Input.payment_gateways)endOutput.payment_gateways=Input.payment_gateways

ซ่อนช่องทางจากลูกค้าบางราย

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

ตัวอย่างเช่น ซ่อนช่องทางใดช่องทางหนึ่งสำหรับลูกค้าที่มีแท็กHIDE_GATEWAY

# ================================ Customizable Settings ================================# ================================================================# Hide Gateways For Customer Tag## If we have a matching customer, the entered gateway(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# - 'gateway_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# - 'gateway_names' is a list of strings to identify gateways# ================================================================HIDE_GATEWAYS_FOR_CUSTOMER_TAG=[{customer_tag_match_type: :include,customer_tags:["customer_tag","another_tag"],gateway_match_type: :exact,gateway_names:["Gateway","Other Gateway"],},]# ================================ Script Code (do not edit) ================================# ================================================================# CustomerTagSelector## Finds whether the supplied customer has any of the entered tags.# ================================================================classCustomerTagSelectordefinitialize(match_type,tags)@comparator=match_type==:include?'any?''none?'@tags=tags.map{|tag|tag.downcase.strip}enddefmatch?(customer)customer_tags=customer.tags.map{|tag|tag.downcase.strip}(@tags&customer_tags).send(@comparator)endend# ================================================================# GatewayNameSelector## Finds whether the supplied gateway name matches any of the# entered names.# ================================================================classGatewayNameSelectordefinitialize(match_type,gateway_names)@comparator=match_type==:exact?'==''include?'@gateway_names=gateway_names.map{|name|name.downcase.strip}enddefmatch?(payment_gateway)@gateway_names.any?{|name|payment_gateway.name.downcase.strip.send(@comparator,name)}endend# ================================================================# HideGatewaysForCustomerTagCampaign## If we have a matching customer, the entered gateway(s) will be# hidden.# ================================================================classHideGatewaysForCustomerTagCampaigndefinitialize(campaigns)@campaigns=campaignsenddefrun(cart,payment_gateways)returnifcart.customer.nil?@campaigns.eachdo|campaign|customer_tag_selector=CustomerTagSelector.new(campaign[:customer_tag_match_type],campaign[:customer_tags],)nextunlesscustomer_tag_selector.match?(cart.customer)gateway_name_selector=GatewayNameSelector.new(campaign[:gateway_match_type],campaign[:gateway_names],)payment_gateways.delete_ifdo|payment_gateway|gateway_name_selector.match?(payment_gateway)endendendendCAMPAIGNS=[HideGatewaysForCustomerTagCampaign.new(HIDE_GATEWAYS_FOR_CUSTOMER_TAG),]CAMPAIGNS.eachdo|campaign|campaign.run(Input.cart,Input.payment_gateways)endOutput.payment_gateways=Input.payment_gateways

ซ่อนช่องทางสำหรับสินค้าเฉพาะรายการ

ใช้สคริปต์นี้เพื่อซ่อนช่องทางหนึ่งเมื่อมีการเพิ่มสินค้าที่กำหนดไว้ลงในตะกร้าสินค้า

ตัวอย่างเช่น ซ่อนช่องทางใดช่องทางหนึ่งหากลูกค้าสั่งซื้อหมวก

# ================================ Customizable Settings ================================# ================================================================# Hide Gateway(s) for Product## If the cart contains any matching items, the entered gateway(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#——“:订阅”找到子scription products# - 'product_selectors' is a list of strings or numbers to# identify products by the above selector type# - 'gateway_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# - 'gateway_names' is a list of strings to identify gateways# ================================================================HIDE_GATEWAY_FOR_PRODUCT=[{product_selector_match_type: :include,product_selector_type: :product_id,product_selectors:[1234567890987,1234567890986],gateway_match_type: :exact,gateway_names:["Gateway","Other Gateway"],},]# ================================ Script Code (do not edit) ================================# ================================================================# ProductSelector## Finds matching products by the entered criteria.# ================================================================classProductSelectordefinitialize(match_type,selector_type,selectors)@match_type=match_type@comparator=match_type==:include?'any?''none?'@selector_type=selector_type@selectors=selectorsenddefmatch?(line_item)ifself.respond_to?(@selector_type)self.send(@selector_type,line_item)elseraiseRuntimeError.new('Invalid product selector type')endenddeftag(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)enddeftype(line_item)@selectors=@selectors.map{|selector|selector.downcase.strip}(@match_type==:include)==@selectors.include?(line_item.variant.product.product_type.downcase.strip)enddefvendor(line_item)@selectors=@selectors.map{|selector|selector.downcase.strip}(@match_type==:include)==@selectors.include?(line_item.variant.product.vendor.downcase.strip)enddefproduct_id(line_item)(@match_type==:include)==@selectors.include?(line_item.variant.product.id)enddefvariant_id(line_item)(@match_type==:include)==@selectors.include?(line_item.variant.id)enddefsubscription(line_item)!line_item.selling_plan_id.nil?endend# ================================================================# GatewayNameSelector## Finds whether the supplied gateway name matches any of the# entered names.# ================================================================classGatewayNameSelectordefinitialize(match_type,gateway_names)@comparator=match_type==:exact?'==''include?'@gateway_names=gateway_names.map{|name|name.downcase.strip}enddefmatch?(payment_gateway)@gateway_names.any?{|name|payment_gateway.name.downcase.strip.send(@comparator,name)}endend# ================================================================# HideGatewayForProductCampaign## If the cart contains any matching items, the entered gateway(s)# are hidden.# ================================================================classHideGatewayForProductCampaigndefinitialize(campaigns)@campaigns=campaignsenddefrun(cart,payment_gateways)@campaigns.eachdo|campaign|product_selector=ProductSelector.new(campaign[:product_selector_match_type],campaign[:product_selector_type],campaign[:product_selectors],)nextunlesscart.line_items.any?{|line_item|product_selector.match?(line_item)}gateway_name_selector=GatewayNameSelector.new(campaign[:gateway_match_type],campaign[:gateway_names],)payment_gateways.delete_ifdo|payment_gateway|gateway_name_selector.match?(payment_gateway)endendendendCAMPAIGNS=[HideGatewayForProductCampaign.new(HIDE_GATEWAY_FOR_PRODUCT),]CAMPAIGNS.eachdo|campaign|campaign.run(Input.cart,Input.payment_gateways)endOutput.payment_gateways=Input.payment_gateways

แสดงช่องทางสำหรับบางประเทศ

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

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

หมายเหตุ: การใช้countryและcountry_codeนั้นจะเป็นไปตามที่กำหนดไว้เฉพาะภูมิภาคเท่านั้น เช่น ประเทศ/ภูมิภาค

# ================================ Customizable Settings ================================# ================================================================# Show Gateway(s) for Country## If the shipping address country matches any of the entered# countries, the entered gateway(s) will be shown, and all others# will be hidden. Otherwise, the entered gateway(s) will be hidden.## - 'country_code_match_type' determines whether we look for the cart# country to match the entered selectors or not. Can be:# - ':include' to look for a country in the list# - ':exclude' to make sure the country is not in the list# - 'country_codes' is a list of 2-character abbreviations for# the applicable countries# - 'gateway_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# - 'gateway_names' is a list of strings to identify gateways# ================================================================SHOW_GATEWAYS_FOR_COUNTRY=[{country_code_match_type: :include,country_codes:["CA"],gateway_match_type: :exact,gateway_names:["Gateway","Other Gateway"],},]# ================================ Script Code (do not edit) ================================# ================================================================# CountrySelector## Finds whether the supplied country code matches the entered# string.# ================================================================classCountrySelectordefinitialize(match_type,countries)@match_type=match_type@countries=countries.map{|country|country.upcase.strip}enddefmatch?(country_code)(@match_type==:include)==@countries.include?(country_code.upcase.strip)endend# ================================================================# GatewayNameSelector## Finds whether the supplied gateway name matches any of the# entered names.# ================================================================classGatewayNameSelectordefinitialize(match_type,gateway_names)@comparator=match_type==:exact?'==''include?'@gateway_names=gateway_names.map{|name|name.downcase.strip}enddefmatch?(payment_gateway)@gateway_names.any?{|name|payment_gateway.name.downcase.strip.send(@comparator,name)}endend# ================================================================# ShowGatewaysForCountryCampaign## If the shipping address country matches any of the entered# countries, the entered gateway(s) will be shown, and all others# will be hidden. Otherwise, the entered gateway(s) will be hidden.# ================================================================classShowGatewaysForCountryCampaigndefinitialize(campaigns)@campaigns=campaignsenddefrun(cart,payment_gateways)address=cart.shipping_address@campaigns.eachdo|campaign|country_selector=CountrySelector.new(campaign[:country_code_match_type],campaign[:country_codes],)country_match=address.nil??falsecountry_selector.match?(address.country_code)gateway_name_selector=GatewayNameSelector.new(campaign[:gateway_match_type],campaign[:gateway_names],)payment_gateways.delete_ifdo|payment_gateway|gateway_name_selector.match?(payment_gateway)!=country_matchendendendendCAMPAIGNS=[ShowGatewaysForCountryCampaign.new(SHOW_GATEWAYS_FOR_COUNTRY),]CAMPAIGNS.eachdo|campaign|campaign.run(Input.cart,Input.payment_gateways)endOutput.payment_gateways=Input.payment_gateways

ซ่อนช่องทางสำหรับบางประเทศ

ใช้สคริปต์นี้เพื่อซ่อนช่องทางการชำระเงินในประเทศใดประเทศหนึ่ง

ตัวอย่างเช่น ซ่อนช่องทางการชำระเงินที่เฉพาะเจาะจงจากลูกค้าในแคนาดา

หมายเหตุ: การใช้countryและcountry_codeนั้นจะเป็นไปตามที่กำหนดไว้เฉพาะภูมิภาคเท่านั้น เช่น ประเทศ/ภูมิภาค

# ================================ Customizable Settings ================================# ================================================================# Hide Gateway(s) for Country## If the shipping address country matches any of the entered# countries, the entered gateway(s) will be hidden.## - 'country_code_match_type' determines whether we look for the cart# country to match the entered selectors or not. Can be:# - ':include' to look for a country in the list# - ':exclude' to make sure the country is not in the list# - 'country_codes' is a list of 2-character abbreviations for# the applicable countries# - 'gateway_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# - 'gateway_names' is a list of strings to identify gateways# ================================================================HIDE_GATEWAYS_FOR_COUNTRY=[{country_code_match_type: :include,country_codes:["CA"],gateway_match_type: :exact,gateway_names:["Gateway","Other Gateway"],},]# ================================ Script Code (do not edit) ================================# ================================================================# CountrySelector## Finds whether the supplied country code matches the entered# string.# ================================================================classCountrySelectordefinitialize(match_type,countries)@match_type=match_type@countries=countries.map{|country|country.upcase.strip}enddefmatch?(country_code)(@match_type==:include)==@countries.include?(country_code.upcase.strip)endend# ================================================================# GatewayNameSelector## Finds whether the supplied gateway name matches any of the# entered names.# ================================================================classGatewayNameSelectordefinitialize(match_type,gateway_names)@comparator=match_type==:exact?'==''include?'@gateway_names=gateway_names.map{|name|name.downcase.strip}enddefmatch?(payment_gateway)@gateway_names.any?{|name|payment_gateway.name.downcase.strip.send(@comparator,name)}endend# ================================================================# HideGatewaysForCountryCampaign## If the shipping address country matches any of the entered# countries, the entered gateway(s) will be hidden.# ================================================================classHideGatewaysForCountryCampaigndefinitialize(campaigns)@campaigns=campaignsenddefrun(cart,payment_gateways)address=cart.shipping_addressreturnifaddress.nil?@campaigns.eachdo|campaign|country_selector=CountrySelector.new(campaign[:country_code_match_type],campaign[:country_codes],)nextunlesscountry_selector.match?(address.country_code)gateway_name_selector=GatewayNameSelector.new(campaign[:gateway_match_type],campaign[:gateway_names],)payment_gateways.delete_ifdo|payment_gateway|gateway_name_selector.match?(payment_gateway)endendendendCAMPAIGNS=[HideGatewaysForCountryCampaign.new(HIDE_GATEWAYS_FOR_COUNTRY),]CAMPAIGNS.eachdo|campaign|campaign.run(Input.cart,Input.payment_gateways)endOutput.payment_gateways=Input.payment_gateways

แสดงเกตเวย์โดยใช้จ่าย

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

ตัวอย่างเช่น คุณสามารถแสดงช่องทางหนึ่งได้หากลูกค้าใช้จ่ายไป $1000 หรือมากกว่า แต่ถ้าหากน้อยกว่านั้นก็ให้ซ่อนช่องทางนั้นไว้

# ================================ Customizable Settings ================================# ================================================================# Show Gateway(s) for Spend Threshold## If the cart total is greater than, or equal to, the entered# threshold, the entered gateway(s) are shown.## - 'threshold' is the dollar amount the customer must spend in# order to see the entered gateway(s)# - 'gateway_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# - 'gateway_names' is a list of strings to identify gateways# ================================================================SHOW_GATEWAYS_FOR_THRESHOLD=[{threshold:500,gateway_match_type: :exact,gateway_names:["Gateway","Other Gateway"],},]# ================================ Script Code (do not edit) ================================# ================================================================# GatewayNameSelector## Finds whether the supplied gateway name matches any of the# entered names.# ================================================================classGatewayNameSelectordefinitialize(match_type,gateway_names)@comparator=match_type==:exact?'==''include?'@gateway_names=gateway_names.map{|name|name.downcase.strip}enddefmatch?(payment_gateway)@gateway_names.any?{|name|payment_gateway.name.downcase.strip.send(@comparator,name)}endend# ================================================================# ShowGatewaysForThresholdCampaign## If the cart total is greater than, or equal to, the entered# threshold, the entered gateway(s) are shown.# ================================================================classShowGatewaysForThresholdCampaigndefinitialize(campaigns)@campaigns=campaignsenddefrun(cart,payment_gateways)@campaigns.eachdo|campaign|nextunlesscart.subtotal_price<(Money.new(cents:100)*campaign[:threshold])gateway_name_selector=GatewayNameSelector.new(campaign[:gateway_match_type],campaign[:gateway_names],)payment_gateways.delete_ifdo|payment_gateway|gateway_name_selector.match?(payment_gateway)endendendendCAMPAIGNS=[ShowGatewaysForThresholdCampaign.new(SHOW_GATEWAYS_FOR_THRESHOLD),]CAMPAIGNS.eachdo|campaign|campaign.run(Input.cart,Input.payment_gateways)endOutput.payment_gateways=Input.payment_gateways

จัดลำดับช่องทาง

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

# ================================ Customizable Settings ================================# ================================================================# Reorder Gateways## The order in which you would like your gateways to display# ================================================================DESIRED_GATEWAY_ORDER=["Payment Gateway 1","Payment Gateway 2","Payment Gateway 3",]# ================================ Script Code (do not edit) ================================# ================================================================# ReorderGatewaysCampaign## Reorders gateways into the entered order# ================================================================classReorderGatewaysCampaigndefinitialize(desired_order)@desired_order=desired_order.map{|item|item.downcase.strip}enddefrun(cart,payment_gateways)payment_gateways.sort_by!{|payment_gateway|@desired_order.index(payment_gateway.name.downcase.strip)||Float::INFINITY}endendCAMPAIGNS=[ReorderGatewaysCampaign.new(DESIRED_GATEWAY_ORDER),]CAMPAIGNS.eachdo|campaign|campaign.run(Input.cart,Input.payment_gateways)endOutput.payment_gateways=Input.payment_gateways

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

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