-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShipping.php
127 lines (83 loc) · 4.42 KB
/
Shipping.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php declare(strict_types=1);
function Self_Ship(float $Sale_Price = 100) : float {
$cost = 0;
return $cost;
}
function FBA(float $Sale_Price = 100) : float {
$cost = 0;
return $cost;
}
function Easy_Ship(float $Sale_Price = 0, float $weighs = 0.25 , float $length = 25 ,float $witdth = 25 ,float $height = 25 ) : float {
## Sale Price include shipping charge
// grap json data
$json_url = "EasyShip.json";
$data = file_get_contents($json_url);
$data = json_decode($data, false);
// sort length,witdth,height
$numbers = array($length , $witdth, $height);
rsort($numbers);
$longest= $numbers[0];
$median = $numbers[1];
$shortest= $numbers[2];
//dimensions
$dimensions_longest = (float) $data->dimensions->longest; // json
$dimensions_median = (float) $data->dimensions->median; // json
$dimensions_shortest = (float) $data->dimensions->shortest; // json
$max_weight = (float) $data->dimensions->max_weight; // json
//heavy
$heavy_weight = (float) $data->heavy->weight; // json
$heavy_Longest = (float) $data->heavy->Longest; // json
$heavy_sides = (float) $data->heavy->sides; // json
$heavy_grith = (float) $data->heavy->grith; // json
// calculate weighs
if ($weighs > $max_weight && $weighs < $heavy_weight) { $type = false;} // weighs not standard_size
elseif ($weighs <= $max_weight && $weighs <= $heavy_weight ) {$type = true;} // weighs not heavy
else{ die(json_encode(array('error' => 'الوزن أعلى من المسموح به')));} // weighs not allowed
if ($weighs <= 0){ die(json_encode(array('error' => 'يجب أن الوزن يكون أعلى من صفر')));}
// calculate dimensions
if ($longest < $dimensions_longest && $median < $dimensions_median && $shortest < $dimensions_shortest) { if ($type == true) {$type = true;}} // dimensions standard_size
elseif ($longest >= $heavy_Longest || $median >= $heavy_sides || $shortest >= $heavy_sides) { die(json_encode(array('error' => 'أبعاد المنتج أعلى من المسموح به')));} // dimensions heavy
else{$type= false;} // dimensions oversize
if ($type == true) {
# standard_size Calculations
$shipping_cost = 0;
$standard_size_extra_weight = (float) $data->standard_size->extra_weight;
$standard_size_extra_fee = (float) $data->standard_size->extra_fee;
$standard_size_shipping_fee = (array) $data->standard_size->shipping_fee;
$alength = count($standard_size_shipping_fee); // array alength
if ($weighs >= $standard_size_extra_weight) {
# code... over extra
# code...
$size = $standard_size_shipping_fee[$alength]->size; // json
$fee = $standard_size_shipping_fee[$alength]->fee;
$weighs = $weighs - 2; // extra weighs - First 2KG
$ceil_weighs = floor($weighs); // ceil weighs
$extra = $standard_size_extra_fee * $ceil_weighs; // extra ceil
$shipping_cost = $extra + $fee; // cost
}else{
$x = 1;
while($x <= $alength) {
$size = $standard_size_shipping_fee[$x]->size; // json shipping_fee size
$fee = $standard_size_shipping_fee[$x]->fee; // json shipping_fee fee
if ($weighs > $size) { $shipping_cost = $fee; $x++;} // next loop if $weighs > $size
else{$shipping_cost = $fee; $x = $alength + 1;} // stop loop for max size
}
}
}elseif($type == false){
# oversize Calculations
$oversize_shipping_fee = (float) $data->oversize->shipping_fee; // json
$oversize_extra_weight = (float) $data->oversize->extra_weight; // json
$oversize_extra_fee = (float) $data->oversize->extra_fee; // json
$Volumetric_Weight = ($length * $witdth * $height) / 5000 ; // Volumetric_Weight
$weights_arr = array($weighs,$Volumetric_Weight ); // array Volumetric and Weight
rsort($weights_arr); // sort array
$weighs = $weights_arr[0] - 1; // select bigger value + Remove First 1KG
$ceil_weighs = ceil($weighs); // ceil weighs
$cul_extra_kg = $ceil_weighs * $oversize_extra_fee; // price for extra KG
$shipping_cost = $oversize_shipping_fee + $cul_extra_kg; // shipping_fee + extra kg price
}else{ die(json_encode(array('error' => 'الوزن أعلى من المسموح به*')) );
} // error
return $shipping_cost;
}
//echo Easy_Ship();
?>