折扣脚本示例

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

此示例使用虚构的增值税 (VAT),其规则如下:

  • 居住在国家联盟内的客户购买的所有产品都需缴纳 VAT。
  • 购买产品并将其出口到非联盟国家/地区的客户无需缴纳 VAT(换句话说,向这些客户的销售为零税率)。
  • 商店中的产品价格包含 VAT,即商店的税费设置已启用我的价格中包含所有税费设置。

在在线店面中,所有客户都会看到产品价格中包含 VAT。将商品添加到购物车后,将显示含 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>

将以下代码添加到您的英语区域设置文件

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

在此页面上

详细了解

详细了解: