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