1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Local Delivery Shipping Method. |
9
|
|
|
* |
10
|
|
|
* This class is here for backwards commpatility for methods existing before zones existed. |
11
|
|
|
* |
12
|
|
|
* @deprecated 2.6.0 |
13
|
|
|
* @version 2.3.0 |
14
|
|
|
* @package WooCommerce/Classes/Shipping |
15
|
|
|
* @author WooThemes |
16
|
|
|
*/ |
17
|
|
|
class WC_Shipping_Legacy_Local_Delivery extends WC_Shipping_Local_Pickup { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor. |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public function __construct() { |
|
|
|
|
23
|
|
|
$this->id = 'legacy_local_delivery'; |
24
|
|
|
$this->method_title = __( 'Local Delivery (Legacy)', 'woocommerce' ); |
25
|
|
|
$this->method_description = sprintf( __( '<strong>This method is deprecated in 2.6.0 and will be removed in future versions - we recommend disabling it and instead setting up a new rate within your <a href="%s">Shipping Zones</a>.</strong>', 'woocommerce' ), admin_url( 'admin.php?page=wc-shipping' ) ); |
26
|
|
|
$this->init(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return the name of the option in the WP DB. |
31
|
|
|
* @since 2.6.0 |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function get_option_key() { |
35
|
|
|
return $this->plugin_id . 'local_delivery' . '_settings'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* init function. |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function init() { |
|
|
|
|
42
|
|
|
|
43
|
|
|
// Load the settings. |
44
|
|
|
$this->init_form_fields(); |
45
|
|
|
$this->init_settings(); |
46
|
|
|
|
47
|
|
|
// Define user set variables |
48
|
|
|
$this->title = $this->get_option( 'title' ); |
49
|
|
|
$this->type = $this->get_option( 'type' ); |
50
|
|
|
$this->fee = $this->get_option( 'fee' ); |
51
|
|
|
$this->type = $this->get_option( 'type' ); |
52
|
|
|
$this->codes = $this->get_option( 'codes' ); |
53
|
|
|
$this->availability = $this->get_option( 'availability' ); |
|
|
|
|
54
|
|
|
$this->countries = $this->get_option( 'countries' ); |
|
|
|
|
55
|
|
|
|
56
|
|
|
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* calculate_shipping function. |
61
|
|
|
* |
62
|
|
|
* @param array $package (default: array()) |
63
|
|
|
*/ |
64
|
|
|
public function calculate_shipping( $package = array() ) { |
65
|
|
|
$shipping_total = 0; |
66
|
|
|
|
67
|
|
|
switch ( $this->type ) { |
68
|
|
|
case 'fixed' : |
69
|
|
|
$shipping_total = $this->fee; |
70
|
|
|
break; |
71
|
|
|
case 'percent' : |
72
|
|
|
$shipping_total = $package['contents_cost'] * ( $this->fee / 100 ); |
73
|
|
|
break; |
74
|
|
|
case 'product' : |
75
|
|
|
foreach ( $package['contents'] as $item_id => $values ) { |
76
|
|
|
if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) { |
77
|
|
|
$shipping_total += $this->fee * $values['quantity']; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$rate = array( |
84
|
|
|
'id' => $this->id, |
85
|
|
|
'label' => $this->title, |
86
|
|
|
'cost' => $shipping_total |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$this->add_rate( $rate ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Init form fields. |
94
|
|
|
*/ |
95
|
|
|
public function init_form_fields() { |
96
|
|
|
$this->form_fields = array( |
97
|
|
|
'enabled' => array( |
98
|
|
|
'title' => __( 'Enable', 'woocommerce' ), |
99
|
|
|
'type' => 'checkbox', |
100
|
|
|
'label' => __( 'Enable local delivery', 'woocommerce' ), |
101
|
|
|
'default' => 'no' |
102
|
|
|
), |
103
|
|
|
'title' => array( |
104
|
|
|
'title' => __( 'Title', 'woocommerce' ), |
105
|
|
|
'type' => 'text', |
106
|
|
|
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), |
107
|
|
|
'default' => __( 'Local Delivery', 'woocommerce' ), |
108
|
|
|
'desc_tip' => true, |
109
|
|
|
), |
110
|
|
|
'type' => array( |
111
|
|
|
'title' => __( 'Fee Type', 'woocommerce' ), |
112
|
|
|
'type' => 'select', |
113
|
|
|
'class' => 'wc-enhanced-select', |
114
|
|
|
'description' => __( 'How to calculate delivery charges', 'woocommerce' ), |
115
|
|
|
'default' => 'fixed', |
116
|
|
|
'options' => array( |
117
|
|
|
'fixed' => __( 'Fixed amount', 'woocommerce' ), |
118
|
|
|
'percent' => __( 'Percentage of cart total', 'woocommerce' ), |
119
|
|
|
'product' => __( 'Fixed amount per product', 'woocommerce' ), |
120
|
|
|
), |
121
|
|
|
'desc_tip' => true, |
122
|
|
|
), |
123
|
|
|
'fee' => array( |
124
|
|
|
'title' => __( 'Delivery Fee', 'woocommerce' ), |
125
|
|
|
'type' => 'price', |
126
|
|
|
'description' => __( 'What fee do you want to charge for local delivery, disregarded if you choose free. Leave blank to disable.', 'woocommerce' ), |
127
|
|
|
'default' => '', |
128
|
|
|
'desc_tip' => true, |
129
|
|
|
'placeholder' => wc_format_localized_price( 0 ) |
130
|
|
|
), |
131
|
|
|
'codes' => array( |
132
|
|
|
'title' => __( 'Allowed ZIP/Post Codes', 'woocommerce' ), |
133
|
|
|
'type' => 'text', |
134
|
|
|
'desc_tip' => __( 'What ZIP/post codes are available for local delivery?', 'woocommerce' ), |
135
|
|
|
'default' => '', |
136
|
|
|
'description' => __( 'Separate codes with a comma. Accepts wildcards, e.g. <code>P*</code> will match a postcode of PE30. Also accepts a pattern, e.g. <code>NG1___</code> would match NG1 1AA but not NG10 1AA', 'woocommerce' ), |
137
|
|
|
'placeholder' => 'e.g. 12345, 56789' |
138
|
|
|
), |
139
|
|
|
'availability' => array( |
140
|
|
|
'title' => __( 'Method availability', 'woocommerce' ), |
141
|
|
|
'type' => 'select', |
142
|
|
|
'default' => 'all', |
143
|
|
|
'class' => 'availability wc-enhanced-select', |
144
|
|
|
'options' => array( |
145
|
|
|
'all' => __( 'All allowed countries', 'woocommerce' ), |
146
|
|
|
'specific' => __( 'Specific Countries', 'woocommerce' ) |
147
|
|
|
) |
148
|
|
|
), |
149
|
|
|
'countries' => array( |
150
|
|
|
'title' => __( 'Specific Countries', 'woocommerce' ), |
151
|
|
|
'type' => 'multiselect', |
152
|
|
|
'class' => 'wc-enhanced-select', |
153
|
|
|
'css' => 'width: 450px;', |
154
|
|
|
'default' => '', |
155
|
|
|
'options' => WC()->countries->get_shipping_countries(), |
156
|
|
|
'custom_attributes' => array( |
157
|
|
|
'data-placeholder' => __( 'Select some countries', 'woocommerce' ) |
158
|
|
|
) |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.