-
Notifications
You must be signed in to change notification settings - Fork 1
/
shipping.php
executable file
·144 lines (117 loc) · 4.41 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Vvveb
*
* Copyright (C) 2022 Ziadin Givan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Vvveb\Plugins\Shipping;
use function Vvveb\__;
use Vvveb\Sql\Region_groupSQL;
use Vvveb\System\Cart\Currency;
use Vvveb\System\Core\View;
use Vvveb\System\ShippingMethod;
if (! defined('V_VERSION')) {
die('Invalid request!');
}
class Shipping extends ShippingMethod {
private $namespace = 'shipping';
private $method_data = [
'name' => 'shipping',
'title' => 'Shipping',
'description' => 'Default shipping',
'region_id' => 0,
'cost' => 0,
'tax_type_id' => 0,
'region_group_id' => 0,
'free_shipping' => 0,
'difference' => 0,
'text' => '',
'free_message' => 'Add more products worth at least %s to get free shipping!',
];
private function getCost() {
$subtotal = $this->cart->getSubTotal(); //without totals/taxes
$total = $this->cart->getGrandTotal();
if (($this->method_data['cost'] == 0) ||
($this->method_data['free_shipping'] && ($total > $this->method_data['free_shipping']))) {
$this->method_data['cost'] = 0;
}
if (isset($this->method_data['weight'])) {
$cartWeight = $this->cart->getWeight();
$base = $this->method_data['base-weight'];
$additionalPrice = 0;
$additionalWeight = $cartWeight - $base;
if ($additionalWeight) {
//get heaviest first
$weights = array_reverse($this->method_data['weight']);
foreach ($weights as $weight) {
if (($cartWeight) > $weight['above_weight']) {
$additionalPrice = floatval($weight['price']);
break;
}
}
if ($additionalPrice) {
//multiply exceeding weight by weight unit price
$this->method_data['cost'] += $additionalWeight * $additionalPrice;
}
}
}
if ($this->method_data['cost'] == 0) {
$this->method_data['text'] = __('Free shipping');
}
$this->method_data['difference'] = max($this->method_data['free_shipping'] - $total - $this->method_data['cost'], 0);
}
public function getMethod($checkoutInfo = [], $options = []) {
$this->method_data = $options + $this->method_data;
$this->method_data['cost'] = floatval($this->method_data['cost']);
//if set only for specific region(s) check if address matches region
$region_group_id = $this->method_data['region_group_id'];
$shipping_country_id = $checkoutInfo['shipping_country_id'] ?? false;
$shipping_region_id = $checkoutInfo['shipping_region_id'] ?? 0;
if ($region_group_id != 0) {
$region = new Region_groupSQL();
$regions = [];
$params = ['region_group_id' => (int)$region_group_id, 'country_id' => (int)$shipping_country_id, 'region_id' => (int)$shipping_region_id];
$regions = $region->isRegion($params);
if (isset($regions['count']) && $regions['count'] == 0) {
return [];
}
}
$this->getCost();
return $this->method_data;
}
public function init() {
//remove previous total and tax
$this->cart->removeTotal($this->namespace);
$this->cart->removeTax($this->namespace);
}
public function setMethod() {
$view = View ::getInstance();
if ($this->method_data['cost'] || $this->method_data['text'] || $this->method_data['difference']) {
if ($this->method_data['free_message'] && $this->method_data['difference']) {
$currency = Currency :: getInstance($options);
$view->info['shipping-message'] = sprintf($this->method_data['free_message'], $currency->format($this->method_data['difference']));
}
if ($this->method_data['tax_type_id']) {
$this->cart->addTax($this->namespace, $this->method_data['cost'], $this->method_data['tax_type_id']);
}
$this->cart->addTotal($this->namespace, $this->method_data['title'] , $this->method_data['cost'], $this->method_data['text']);
}
}
public function ship() {
return true;
}
}