折扣指令碼範例

此範例使用商品項目指令碼,根據顧客所在的國家/地區,提供相應的折扣。範例也包含範例 Liquid 程式碼,藉此提供顧客有關折扣的資訊。此範例需要您有存取商店 checkout.liquid 檔案的權限。如果您沒有存取佈景主題程式碼檔案的權限,請使用 Checkout ExtensibilityShopify Functions 來自訂結帳功能。

本範例所使用的虛構增值税具有下列規則:

  • 居住在聯盟國家/地區的客戶必須對所有售出的商品支付加值稅 (VAT) 。
  • 購買商品並出口到非聯盟國家/地區的客戶無須支付加值稅 (VAT) (換言之,銷售給這些客戶的商品為零稅率)。
  • 商店中顯示的商品價格包含加值稅 (VAT) (亦即已在商店的「稅金設定」中啟用「所有稅金都包含在我的價格中」設定)。

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

Cart with tax applied

結帳時,指令碼會檢查運送國家/地區。如果國家/地區是非聯盟國家/地區,那麼總金額會減去增值稅的金額:

已移除稅金的購物車

在下列範例中,指令碼會檢查客戶的國家/地區。如果客戶位於不適用加值稅 (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>

將下列程式碼新增至您的英語地區設定檔

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

此頁面上

深入瞭解

深入瞭解:

準備好開始透過 Shopify 銷售商品了嗎?

免費試用