jQuery(document).ready(function($) { $('#ghn_send_to_ghn').click(function() { var order_id = ghn_order_data.order_id; var shipping_fee = parseInt($('#ghn_shipping_fee').val(), 10) || 0; var cod_amount = parseInt($('#ghn_cod_amount').val(), 10) || 0; var weight = parseInt($('#ghn_weight').val(), 10) || 0; var length = parseInt($('#ghn_length').val(), 10) || 0; var width = parseInt($('#ghn_width').val(), 10) || 0; var height = parseInt($('#ghn_height').val(), 10) || 0; var insurance_value = parseInt($('#ghn_insurance_value').val(), 10) || 0; var service_type_id = parseInt($('#ghn_service_type_id').val(), 10); var payment_type_id = 1; // Mặc định là người gửi trả phí var required_note = 'KHONGCHOXEMHANG'; // Mặc định không cho xem hàng // Chuẩn bị dữ liệu cho yêu cầu var items = []; if (ghn_order_data.items) { ghn_order_data.items.forEach(function(item) { items.push({ "name": item.name, "code": item.sku || "", "quantity": parseInt(item.quantity, 10), "price": parseInt(item.price, 10), "length": length, "width": width, "height": height, "weight": weight, "category": { "level1": item.category || "Khác" } }); }); } var data = { "payment_type_id": payment_type_id, "note": "Đơn hàng #" + order_id, "required_note": required_note, "return_phone": ghn_order_data.from_phone, "return_address": ghn_order_data.from_address, "return_district_id": null, "return_ward_code": "", "client_order_code": order_id.toString(), "from_name": ghn_order_data.from_name, "from_phone": ghn_order_data.from_phone, "from_address": ghn_order_data.from_address, "from_ward_name": ghn_order_data.from_ward_name, "from_district_name": ghn_order_data.from_district_name, "from_province_name": ghn_order_data.from_province_name, "to_name": ghn_order_data.to_name, "to_phone": ghn_order_data.to_phone, "to_address": ghn_order_data.to_address, "to_ward_name": ghn_order_data.to_ward_name, "to_ward_code": "", "to_district_name": ghn_order_data.to_district_name, "to_province_name": ghn_order_data.to_province_name, "cod_amount": cod_amount, "content": "Đơn hàng #" + order_id, "length": length, "width": width, "height": height, "weight": weight, "insurance_value": insurance_value, "service_type_id": service_type_id, "items": items }; $('#ghn_response').html('Đang gửi...'); $.ajax({ url: 'https://online-gateway.ghn.vn/shiip/public-api/v2/shipping-order/create', type: 'POST', contentType: 'application/json', headers: { 'ShopId': ghn_order_data.shop_id, 'Token': ghn_order_data.token }, data: JSON.stringify(data), success: function(response) { if (response.code === 200) { var successMessage = '
Đơn hàng đã được gửi đi thành công!
'; successMessage += 'Lỗi: ' + response.message + '
'; errorMessage += 'Lỗi: ' + (jqXHR.responseJSON ? jqXHR.responseJSON.message : 'Lỗi khi gửi yêu cầu.') + '
'; errorMessage += 'No account yet?
Create an Account