let discounts = false;
let shipping = false;
let temp_cart = false;
let order_submitting = false;
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function load_cart() {
$.ajax({
type: "GET",
url: window.Shopify.routes.root + "cart.js",
dataType: "text",
success: function (data) {
$("#table-summary").empty();
data = JSON.parse(data);
if (data["item_count"] > 0) {
data.items.forEach((item) => {
let subtotal_cell = `
RM ${(item.final_price / 100).toFixed(2)}
`;
if (item.final_price == 0) {
subtotal_cell = ``;
}
$("#table-summary").append(`
${item.product_title}
${item.variant_title || ""}
${subtotal_cell}
`);
});
data["total_price"] = (parseFloat(data["total_price"]) / 100).toFixed(2);
$("#text-subtotal").html(`RM ${numberWithCommas(data["total_price"])}`);
let total = parseFloat(data["total_price"]);
if (discounts) {
total = parseFloat(data["total_price"]) - parseFloat(discounts["amount"]);
}
$("#text-total").html(`RM ${numberWithCommas(total.toFixed(2))}`);
credit_left = parseFloat(details["credits"]) / 100 - total;
$("#text-credits").html(`CR ${numberWithCommas(credit_left.toFixed(2))}`);
temp_cart = data;
} else {
location.replace("https://mandarin.club");
}
},
error: function (data) {
toastr.error("load failed");
console.log(data);
},
});
}
function discount_apply() {
let code = $("#TextField7").val();
let price = temp_cart["total_price"];
let items = []
temp_cart["items"].forEach(item => {
items.push(item.id)
});
$.ajax({
type: "GET",
url: `https://vip.mandarin.club/discount?code=${code}&price=${price}&email=${details["email"]}&items=${JSON.stringify(items)}`,
dataType: "text",
success: function (data) {
data = JSON.parse(data);
$("#section-discount").hide();
$("#text-discount").text(data["code"]);
$("#text-discount-value").html(`− RM ${data["amount"]}.00`);
$("#row-discount-header").show();
$("#row-discount").css("display", "grid");
discounts = data;
order_summary();
},
error: function (data) {
alert(data.responseText);
console.log(data);
},
});
}
function order_submit() {
let shipping_details = {
first_name: $("#TextField0").val(),
last_name: $("#TextField1").val(),
company: $("#TextField2").val(),
address1: $("#TextField3").val(),
address2: $("#TextField9").val(),
zip: $("#TextField4").val(),
city: $("#TextField5").val(),
phone: $("#TextField6").val(),
country: $("#Select0").val(),
province: $("#Select1").val(),
};
$.ajax({
type: "GET",
url: window.Shopify.routes.root + "cart.js",
dataType: "text",
success: function (data) {
data = JSON.parse(data);
total_price = (parseFloat(data["total_price"]) / 100).toFixed(2);
let total = total_price;
if (discounts) {
total = total_price - parseFloat(discounts["amount"]);
}
if (shipping) {
if (parseInt(shipping["price"]) > 0) {
total -= parseFloat(shipping["price"]);
$("#text-shipping").html(`-RM ${shipping["price"]}`);
} else if (shipping.name == "Free Shipping" || shipping.name == "FREE SHIPPING" || shipping.name == "Shipping") {
$("#text-shipping").html(shipping.name);
} else {
$("#text-shipping").text("No Shipping Rate");
shipping = false;
}
}
credit_left = parseFloat(details["credits"]) / 100 - total;
if (credit_left < 0) {
alert("余额不足 Insufficient Credit");
} else if (shipping == false) {
alert("无可用运费,请检查地址 No Shipping Rate");
} else if (order_submitting) {
} else {
order_submitting = true;
$("#text-submit-btn").text("Submitting Order");
let POST_data = data;
POST_data["shipping_address"] = shipping_details;
POST_data["shipping_line"] = shipping;
POST_data["shipping_price"] = shipping.price;
POST_data["discount_code"] = discounts;
POST_data["credit_use"] = total;
POST_data["credit_left"] = credit_left;
POST_data["customer_email"] = details["email"];
console.log(POST_data);
$.ajax({
type: "POST",
url: "https://vip.mandarin.club/order_submit",
data: JSON.stringify(POST_data),
dataType: "text",
contentType: "text/plain",
success: (data) => {
$.ajax({
type: "POST",
url: "/cart/clear.js",
data: "",
dataType: "json",
success: function () {
Shopify.clear();
location.assign(data);
},
error: function (XMLHttpRequest, textStatus) {
/* error code */
},
});
},
error: (data) => {
alert("无法提交 Error");
order_submitting = false;
$("#text-submit-btn").text("Pay now");
},
});
}
},
error: function (data) {
toastr.error("load failed");
console.log(data);
},
});
}
function order_summary() {
let shipping_details = {
first_name: $("#TextField0").val(),
last_name: $("#TextField1").val(),
company: $("#TextField2").val(),
address1: $("#TextField3").val(),
address2: $("#TextField9").val(),
zip: $("#TextField4").val(),
city: $("#TextField5").val(),
phone: $("#TextField6").val(),
country: $("#Select0").val(),
province: $("#Select1").val(),
};
$.ajax({
type: "GET",
url: window.Shopify.routes.root + "cart.js",
dataType: "text",
success: function (data) {
data = JSON.parse(data);
total_price = (parseFloat(data["total_price"]) / 100).toFixed(2);
let total = total_price;
if (discounts) {
total = total_price - parseFloat(discounts["amount"]);
}
final_total = total;
if (shipping) {
if (shipping.name == "Shipping" || shipping.name == "Standard" || shipping.name == "STANDARD") {
final_total = parseFloat(total) + parseFloat(shipping["price"]);
$("#text-shipping").html(`RM ${shipping["price"]}`);
console.log(final_total);
console.log(shipping["price"]);
} else if (shipping.name == "Free Shipping" || shipping.name == "FREE SHIPPING" || shipping.name == "Shipping") {
$("#text-shipping").html(shipping.name);
} else {
$("#text-shipping").text("No Shipping Rate");
shipping = false;
}
} else {
$("#text-shipping").text("No Shipping Rate");
shipping = false;
}
credit_left = parseFloat(details["credits"]) / 100 - final_total;
$("#text-total").html(`RM ${numberWithCommas(parseFloat(final_total).toFixed(2))}`);
$("#text-credits").html(`CR ${numberWithCommas(credit_left.toFixed(2))}`);
},
error: function (data) {
toastr.error("load failed");
console.log(data);
},
});
}
function get_shipping() {
// Set the locale (e.g., 'en' for English)
const locale = "en";
// Set the base URL for your Shopify store
const baseUrl = "https://mandarin.club";
// Define the shipping address
let shippingAddress = {
country: $("#Select0").val(),
zip: $("#TextField4").val(),
province: "",
};
if ($("#Select0").val() == "MY") {
shippingAddress["province"] = $("#Select1").val();
}
// Make the GET request to retrieve shipping rates
fetch(
`${baseUrl}/${locale}/cart/shipping_rates.json?shipping_address[zip]=${shippingAddress.zip}&shipping_address[country]=${shippingAddress.country}&shipping_address[province]=${shippingAddress.province}`,
{
method: "GET",
credentials: "same-origin", // Include cookies for the current domain
headers: {
"Content-Type": "application/json",
},
}
)
.then((response) => response.json())
.then((data) => {
console.log("Shipping rates:", data);
if (data["shipping_rates"][0]["name"] == "SEA SHIPPING") {
shipping = data["shipping_rates"][1];
} else {
shipping = data["shipping_rates"][0];
}
order_summary();
})
.catch((error) => {
console.error("Error fetching shipping rates:", error);
});
}
$(document).ready(function () {
$("#Select0").on("change", () => {
if ($("#Select0").val() != "MY") {
$("#wrapper-state").hide();
} else {
$("#wrapper-state").show();
}
});
$("#Select1").on("change", () => {
get_shipping();
});
$("#TextField4").on("change", () => {
get_shipping();
});
load_cart();
});