-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc-multi-tiered-shipping.php
319 lines (291 loc) · 10.7 KB
/
wc-multi-tiered-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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* @package WCMultiTieredShipping
*/
/*
* Plugin Name: WC Multi-Tiered Shipping
* Description: Add a USPS multi-tiered shipping option to the WooCommerce plugin.
* Extended Description:
* This WordPress plugin adds a multi-tiered shipping option to the WooCommerce
* plugin. Clothing units of merchandise are fairly uniform in size such that
* a predetermined number of units can fit into USPS flat-rate boxes: large,
* medium, and small.
* For USPS info: <https://www.usps.com/ship/priority-mail.htm>
* Version: 1.0.0
* Author: M&M Hodges <[email protected]>
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/*
* This plugin is a derivative work. Special thanks to:
* Joni Halabi, http://www.jhalabi.com, author of the WC Tiered Shipping
* plugin.
*
* The WC Multitiered Shipping plugin was written for Emily, co-owner of
* ConfiDANCE, by her parents (lucky girl!).
*/
/**
* Plugin initialization.
*/
function multi_tiered_shipping_init()
{
if (!class_exists('WC_Multi_Tiered_Shipping'))
{
class WC_Multi_Tiered_Shipping extends WC_Shipping_Method
{
/**
* Constructor for the multi-tier shipping class
*
* @access public
* @return void
*/
public function __construct()
{
$this->id = 'multi_tiered_shipping';
$this->method_title = __('Multi-Tiered Shipping'); // Admin settings title
$this->title = __('Multi-Tiered Shipping'); // Shipping method list title
$this->init();
}
/**
* Initialization.
*
* @access public
* @return void
*/
function init()
{
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Save the settings.
add_action('woocommerce_update_options_shipping_' . $this->id,
array(&$this, 'process_admin_options'));
}
/**
* Init Settings Form Fields (overriding default settings API)
*/
function init_form_fields()
{
global $woocommerce;
$this->form_fields = array(
'enabled' => array(
'title' => __('Enabled/Disabled', 'multi_tiered_shipping'),
'type' => 'checkbox',
'label' => 'Enable this shipping method'
),
'usertitle' => array(
'title' => __('Title', 'multi_tiered_shipping'),
'type' => 'text',
'description' => __('Shipping method label that is visible to the user.',
'multi_tiered_shipping'),
'default' => __('USPS Flat Rates', 'multi_tiered_shipping')
),
'availability' => array(
'title' => __('Availability', 'multi_tiered_shipping'),
'type' => 'select',
'class' => 'wc-enhanced-select availability',
'options' => array(
'all' => 'All allowed countries',
'specific' => 'Specific countries'
),
'default' => __('all', 'multi_tiered_shipping')
),
'countries' => array(
'title' => __('Countries', 'multi_tiered_shipping'),
'type' => 'multiselect',
'class' => 'wc-enhanced-select',
'options' => $woocommerce->countries->countries,
'default' => __('', 'multi_tiered_shipping')
),
'cfd_additional_cost' => array(
'title' => __('Per Item Cost', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'For large orders the total cost is the largest flat-rate cost plus the addition per-item shipping cost for the addiitonal items.',
'default' => '1.12'
),
'cfd_tier1_qty' => array(
'title' => __('Small Qty', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'Maximum quantity for USPS small flat-rate box.<br>Take heed: quantities MUST get progressively larger for predictable results!!!',
'default' => '2'
),
'cfd_tier1_amt' => array(
'title' => __('Small Amt', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'Cost for USPS small flat-rate box',
'default' => '5.95'
),
'cfd_tier2_qty' => array(
'title' => __('Medium Qty', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'Maximum quantity for USPS medium flat-rate box.',
'default' => '4'
),
'cfd_tier2_amt' => array(
'title' => __('Medium Amt', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'Cost for USPS medium flat-rate box.',
'default' => '12.65'
),
'cfd_tier3_qty' => array(
'title' => __('Large Qty', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'Maximum quantity for USPS large flat-rate box.',
'default' => '10'
),
'cfd_tier3_amt' => array(
'title' => __('Large Amt', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'Cost for USPS large flat-rate box.',
'default' => '15.90'
),
'cfd_tier4_qty' => array(
'title' => __('Largest Qty', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'Maximum quantity for USPS largest flat-rate box.',
'default' => '15'
),
'cfd_tier4_amt' => array(
'title' => __('Largest Amt', 'multi_tiered_shipping'),
'type' => 'text',
'description' => 'Cost for USPS largest flat-rate box.',
'default' => '17.90'
),
);
}
/**
* Calculate shipping cost.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping($package = array())
{
// Only add the shipping rate for this method if the user's country is
// included.
if (!$this->is_tiered_allowed($package))
{
return;
}
global $woocommerce;
// Get total item count from cart.
$cart_item_quantities = $woocommerce->cart->get_cart_item_quantities();
$cart_total_items = array_sum($cart_item_quantities);
// Set the shipping cost.
$rate = array(
'id' => $this->id,
'label' => $this->get_option('usertitle'),
'cost' => $this->cfd_get_shipping_cost($cart_total_items)
);
$this->add_rate($rate);
}
/**
* is_tiered_allowed function.
*
* @param mixed $package
* @return true|false
*/
function is_tiered_allowed($package = array())
{
// If plugin availability is set to all countries, just return true.
$availability = $this->get_option('availability');
if ($availability == 'all')
{
return true;
}
// Otherwise, if user's country is not set, return false.
// We cannot allow this shipping option if it is not available in all countries
// and we do not know what country the user is in.
$user_country = $package['destination']['country'];
if (!$user_country)
{
return false;
}
// Otherwise, make sure the user's country is in the array of allowed countries.
$countries = $this->get_option('countries');
$in_allowed_country = false;
for ($i = 0; $i < sizeof($countries); $i++)
{
if ($user_country == $countries[$i])
{
$in_allowed_country = true;
break;
}
}
return $in_allowed_country;
}
/**
* Shipping calculation based on average number of units of merchandise
* per package where:
* qty - the maximum quantity of units of merchandise for the tier.
* amt - the fixed USPS flat-rate cost for the tier.
* WARNING: tiers *must* be in ascending order by quantity, 'qty'.
*
* @return mixed
*/
function cfd_get_shipping_tiers()
{
return [
array(
'qty' => $this->get_option('cfd_tier1_qty'),
'amt' => $this->get_option('cfd_tier1_amt')
),
array(
'qty' => $this->get_option('cfd_tier2_qty'),
'amt' => $this->get_option('cfd_tier2_amt')
),
array(
'qty' => $this->get_option('cfd_tier3_qty'),
'amt' => $this->get_option('cfd_tier3_amt')
),
array(
'qty' => $this->get_option('cfd_tier4_qty'),
'amt' => $this->get_option('cfd_tier4_amt')
),
];
}
/**
* Calculate shipping costs by looking for the appropriate shipping tier
* by item quantity. Prorate shipping cost if there are too many items
* for the largest tier.
*
* @param type $cart_total_items
* @return type
*/
function cfd_get_shipping_cost($cart_total_items)
{
$shipping = 0;
$cfd_shipping_tier = $this->cfd_get_shipping_tiers();
// Calculate costs by looking for the appropriate shipping tier by item
// quantity.
foreach ($cfd_shipping_tier as $cfd_tier_no => $cfd_tier_value)
{
if ($cart_total_items <= $cfd_tier_value['qty'])
{
$shipping = $cfd_tier_value['amt'];
break;
}
}
if ($shipping == 0)
{
// If none of the tiers worked, prorate shipping based on the largest
// tier as the base cost, plus an additional per-item cost.
$cfd_extra_per_unit = $this->get_option('cfd_additional_cost');
$cfd_last_tier = max(array_keys($cfd_shipping_tier));
$cfd_last_tier_amt = $cfd_shipping_tier[$cfd_last_tier]['amt'];
$cfd_last_tier_qty = $cfd_shipping_tier[$cfd_last_tier]['qty'];
$shipping = $cfd_last_tier_amt + ($cart_total_items - $cfd_last_tier_qty) * $cfd_extra_per_unit;
}
return $shipping;
}
}
}
}
add_action('woocommerce_shipping_init', 'multi_tiered_shipping_init');
function add_multi_tiered_shipping($methods)
{
$methods[] = 'WC_Multi_Tiered_Shipping';
return $methods;
}
add_filter('woocommerce_shipping_methods', 'add_multi_tiered_shipping');