折扣脚本示例

此示例使用订单项目脚本来根据客户所在国家/地区提供折扣。此示例还包含示例 Liquid 代码,用于向客户提供有关折扣的信息。此示例要求您具有商店的 checkout.liquid 文件的访问权限。如果您无法在模板代码中访问此文件,请使用 Checkout ExtensibilityShopify Functions 来自定义结账流程。

此示例使用具有以下规则的虚构增值税:

  • 居住在联盟国家的客户需要支付售出的所有产品的增值税。
  • 购买产品并将其出口到非联盟国家/地区的客户无需缴纳增值税(换句话说,对这些客户的销售实施零税率)。
  • 商店中的产品价格包含增值税(即在商店的税费设置中启用在价格中包含所有税费)。

在店面中,所有客户都能看到产品价格中包含的 VAT。将商品添加到购物车时,会显示包含 VAT 的价格:

Cart with tax applied

在结账时,脚本会检查收货国家/地区。如果该国家/地区是非联盟的国家/地区,则总价会减去增值税金额:

删除税金的购物车

在以下示例中,会检查客户的国家/地区。如果客户所在的国家/地区不征收增值税,则订单的总价格将减去增值税金额。

# 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 进行销售了吗?

免费试用