ตัวอย่างสคริปต์ส่วนลด

ตัวอย่างนี้ใช้สคริปต์สินค้าเฉพาะรายการเพื่อมอบส่วนลดตามประเทศของลูกค้า นอกจากนี้ ตัวอย่างยังมีโค้ด Liquid ตัวอย่างเพื่อให้ข้อมูลแก่ลูกค้าเกี่ยวกับส่วนลดดังกล่าวด้วย ตัวอย่างนี้กำหนดให้คุณต้องมีสิทธิ์เข้าถึงไฟล์ checkout.liquid ของร้านค้า หากคุณไม่มีสิทธิ์เข้าถึงไฟล์นี้ในโค้ดธีมของคุณ ให้ใช้ ส่วนขยาย Shopify สำหรับการชำระเงิน และ Shopify 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."
    }
  }

ดูข้อมูลเพิ่มเติม

ดูข้อมูลเพิ่มเติมเกี่ยวกับ: