· 5 years ago · Nov 05, 2020, 03:28 PM
1let xmastree = false;
2for (let p in cart['_embedded']['fx:items']) {
3 let item = cart['_embedded']['fx:items'][p];
4 for (let o in item['_embedded']['fx:item_options']) {
5 let item_option = item['_embedded']['fx:item_options'][o];
6 if (item_option['name'] == "xmastree" && item_option['value'] == "Integromat christmas tree notifications") {
7 xmastree = true;
8 }
9 }
10}
11
12// Google Maps API key
13const google_maps_api_key = 'AIzaSyDkHjYQgi0q_XkY0umU4wL5iVBxH-jwD6Y';
14// Shipping Origin Address
15const origin_address = '63 Woodthorpe Drive, Nottingham, Nottinghamshire, United Kingdom, NG5 4GY';
16// Unit type (either 'metric' or 'imperial')
17const unit_type = 'metric';
18// Distance will be in metres in the response from Google Maps
19// Set this to true to convert the value to kilometres/miles based on the unit type above
20const convert_distance = true;
21
22
23// Get address details
24const shipment = cart['_embedded']['fx:shipment'];
25const country = shipment['country'];
26const region = shipment['region'];
27const city = shipment['city'];
28const postal_code = shipment['postal_code'];
29let address1 = '';
30for (let c in cart['_embedded']['fx:custom_fields']) {
31 if (cart['_embedded']['fx:custom_fields'][c]['name'] == 'shipping_address1') {
32 address1 = cart['_embedded']['fx:custom_fields'][c]['value'];
33 break;
34 }
35}
36
37const customer_address = address1 + ', ' + city + ', ' + region + ', ' + country + ', ' + postal_code;
38
39// Get address coordinates from Google Maps
40const maps_url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=' + origin_address + '&destinations=' + customer_address + '&units=' + unit_type + '&key=' + google_maps_api_key;
41let distance = -1;
42await request(maps_url, function (error, response, body) {
43 let payload = JSON.parse(body);
44 if (response && response.statusCode == 200 && payload.status == 'OK' && payload.rows[0].elements[0].status == 'OK') {
45 distance = payload.rows[0].elements[0].distance.value;
46 if (convert_distance) {
47 // Convert distance from metres to kilometres/miles based on unit_type if configured to
48 distance = (unit_type == 'metric') ? distance/1000 : distance/1609.34;
49 }
50 }
51});
52
53// Begin Custom Logic
54let has_shippable_products = false;
55let non_shippable_products = [];
56
57for (let p in cart['_embedded']['fx:items']) {
58 let item = cart['_embedded']['fx:items'][p];
59 switch (item['_embedded']['fx:item_category']['code']) {
60 case "Allow Postal delivery":
61 has_shippable_products = true;
62 break;
63 default:
64 non_shippable_products.push(item.name);
65 }
66}
67
68if (distance < 60) {
69 let order_total = shipment['total_item_price'];
70 let shipping = 5;
71 let description = 'Standard Delivery: 5-7 Working Days';
72
73 if (distance <= 25 && order_total > 15) {
74 shipping = 0;
75 description = 'Free Delivery: 5-7 Working Days';
76 }
77 rates.add(10000, shipping, '', description);
78 if (xmastree) {
79 rates.add(10002, 0, '', 'Select this option if you would like our staff to pick your items for you and have them ready for you to collect in store');
80 rates.add(10003, 0, '', 'Select this option If you would rather pay now and choose your tree when you get to our store');
81 }
82} else {
83 rates.hide();
84 if (has_shippable_products && non_shippable_products.length == 0) {
85 // Only has shippable products
86 rates.add(10001, 5, '', 'Postal Delivery');
87 } else if (has_shippable_products && non_shippable_products.length > 0) {
88 // Has both shippable and non shippable products
89 rates.error("Sorry, we're unable to ship some of your products to your address. Please remove the following product(s) to calculate shipping: " + non_shippable_products.join(", "));
90 } else {
91 // Doesn't have shippable products
92 rates.error("Sorry, we're unable to ship these products to your address. Please contact us to discuss your order");
93 }
94}