折扣指令碼範例

本範例使用商品項目指令碼,依顧客所屬國家/地區提供折扣。此外也包含 Liquid 範例程式碼,向顧客說明折扣資訊。本範例需要您能存取商店的 checkout.liquid 檔案。若您的 theme code 中無法存取此檔案,請改用 Shopify Extensions for checkoutShopify Functions 自訂結帳頁面。

本範例採用虛構的加值稅 (VAT),並遵循以下規則:

  • 居住於某個國家聯盟的顧客,所有銷售的商品均需徵收加值稅 (VAT)。
  • 顧客購買商品並出口至非聯盟國家/地區時,無需支付加值稅 (VAT)(亦即對這些顧客的銷售為零稅率)。
  • 當商店的 tax settings 啟用「我的價格已含所有稅費」設定時,商店商品價格會包含加值稅 (VAT)。

在線上店面,所有顧客都會看到商品價格已包含加值稅 (VAT)。將品項加入購物車時,會顯示含稅價格:

購物車已套用稅額

在結帳頁面,指令碼會檢查運送國家/地區。若該國家/地區為非聯盟成員,總金額將減去加值稅 (VAT) 金額:

購物車已移除稅額

在以下範例中,系統會檢查顧客的國家/地區。若顧客居住於不適用加值稅 (VAT) 的國家/地區,訂單總金額將減去加值稅金額。

# Set VAT equal to the amount of the VAT rate.
# For example, if the VAT rate is 20%, then VAT=20
VAT = 20

# Message that appears beside the discount in the checkout
VAT_REMOVAL_MESSAGE = "VAT removed"

# List of countries where the VAT is charged to orders
COUNTRY_CODES_EU = %w[
AT BE BG CY CZ DK EE FI FR DE GR HU IE IT
LV LT LU MT NL PL PT RO SK SI ES SE GB
]

if Input.cart.shipping_address
  unless COUNTRY_CODES_EU.include?(Input.cart.shipping_address.country_code)
    Input.cart.line_items.each do |line_item|
      product = line_item.variant.product
      next if product.gift_card?
      vat_only_fraction = VAT / (100.0 + VAT)
      vat =  line_item.line_price * vat_only_fraction
      ex_vat_price = line_item.line_price - vat
      line_item.change_line_price(ex_vat_price, message: VAT_REMOVAL_MESSAGE)
    end
  end
end

Output.cart = Input.cart

以下 Liquid 程式碼會搭配上述指令碼,說明購物車中的變更。

將此程式碼加入 checkout.liquid

<style>
  .checkout__vat-exemption-message {
    padding: 1.25em 0;
    display: none;
  }
  @media (min-width: 1000px) {
    .checkout__vat-exemption-message {
      padding-top: 0;
      padding-bottom: 2.5em;
    }
  }
</style>

<div class="checkout__vat-exemption-message">
  <span>{{ 'plus.checkout.vat_exemption_message' | t }}</span>
</div>

<script>
  $(document).on('ready page:load page:change', function() {
    var country = '';

    if(Shopify.Checkout.step === 'contact_information') {
      $country = $('[data-step] select[name="checkout[shipping_address][country]"]');
      country = $country.find(':selected').data('code');
    } else {
      country = "{{ checkout.shipping_address.country_code }}";
    }

    var eu_countries = ['AT','BE','BG','CY','CZ','DK','EE','FI','FR','DE','GR','HU','IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES','SE','GB'];

    if (eu_countries.includes(country)) {
      $('.checkout__vat-exemption-message').css('display', 'none');
    } else {
      $('.checkout__vat-exemption-message').css('display', 'block');
    }
  });
  
</script>

將以下程式碼加入您的 English locale file

"plus":{
    "checkout": {
      "vat_exemption_message": "As we're shipping outside the EU the VAT has been removed from items in your cart."
    }
  }

本頁內容

瞭解詳情

深入瞭解: