1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 6 7 8 9 10 11 12 13 14
15 class WC_Shipping_Local_Delivery extends WC_Shipping_Method {
16
17 18 19 20 21 22
23 function __construct() {
24 $this->id = 'local_delivery';
25 $this->method_title = __( 'Local Delivery', 'woocommerce' );
26 $this->init();
27 }
28
29 30 31 32 33 34
35 function init() {
36
37
38 $this->init_form_fields();
39 $this->init_settings();
40
41
42 $this->title = $this->get_option( 'title' );
43 $this->type = $this->get_option( 'type' );
44 $this->fee = $this->get_option( 'fee' );
45 $this->type = $this->get_option( 'type' );
46 $this->codes = $this->get_option( 'codes' );
47 $this->availability = $this->get_option( 'availability' );
48 $this->countries = $this->get_option( 'countries' );
49
50 add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
51 }
52
53 54 55 56 57 58 59
60 function calculate_shipping( $package = array() ) {
61
62 $shipping_total = 0;
63 $fee = ( trim( $this->fee ) == '' ) ? 0 : $this->fee;
64
65 if ( $this->type =='fixed' ) $shipping_total = $this->fee;
66
67 if ( $this->type =='percent' ) $shipping_total = $package['contents_cost'] * ( $this->fee / 100 );
68
69 if ( $this->type == 'product' ) {
70 foreach ( WC()->cart->get_cart() as $item_id => $values ) {
71 $_product = $values['data'];
72
73 if ( $values['quantity'] > 0 && $_product->needs_shipping() ) {
74 $shipping_total += $this->fee * $values['quantity'];
75 }
76 }
77 }
78
79 $rate = array(
80 'id' => $this->id,
81 'label' => $this->title,
82 'cost' => $shipping_total
83 );
84
85 $this->add_rate($rate);
86 }
87
88 89 90 91 92 93
94 function init_form_fields() {
95 $this->form_fields = array(
96 'enabled' => array(
97 'title' => __( 'Enable', 'woocommerce' ),
98 'type' => 'checkbox',
99 'label' => __( 'Enable local delivery', 'woocommerce' ),
100 'default' => 'no'
101 ),
102 'title' => array(
103 'title' => __( 'Title', 'woocommerce' ),
104 'type' => 'text',
105 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
106 'default' => __( 'Local Delivery', 'woocommerce' ),
107 'desc_tip' => true,
108 ),
109 'type' => array(
110 'title' => __( 'Fee Type', 'woocommerce' ),
111 'type' => 'select',
112 'description' => __( 'How to calculate delivery charges', 'woocommerce' ),
113 'default' => 'fixed',
114 'options' => array(
115 'fixed' => __( 'Fixed amount', 'woocommerce' ),
116 'percent' => __( 'Percentage of cart total', 'woocommerce' ),
117 'product' => __( 'Fixed amount per product', 'woocommerce' ),
118 ),
119 'desc_tip' => true,
120 ),
121 'fee' => array(
122 'title' => __( 'Delivery Fee', 'woocommerce' ),
123 'type' => 'price',
124 'description' => __( 'What fee do you want to charge for local delivery, disregarded if you choose free. Leave blank to disable.', 'woocommerce' ),
125 'default' => '',
126 'desc_tip' => true,
127 'placeholder' => wc_format_localized_price( 0 )
128 ),
129 'codes' => array(
130 'title' => __( 'Zip/Post Codes', 'woocommerce' ),
131 'type' => 'textarea',
132 'description' => __( 'What zip/post codes would you like to offer delivery to? Separate codes with a comma. Accepts wildcards, e.g. P* will match a postcode of PE30.', 'woocommerce' ),
133 'default' => '',
134 'desc_tip' => true,
135 'placeholder' => '12345, 56789 etc'
136 ),
137 'availability' => array(
138 'title' => __( 'Method availability', 'woocommerce' ),
139 'type' => 'select',
140 'default' => 'all',
141 'class' => 'availability',
142 'options' => array(
143 'all' => __( 'All allowed countries', 'woocommerce' ),
144 'specific' => __( 'Specific Countries', 'woocommerce' )
145 )
146 ),
147 'countries' => array(
148 'title' => __( 'Specific Countries', 'woocommerce' ),
149 'type' => 'multiselect',
150 'class' => 'chosen_select',
151 'css' => 'width: 450px;',
152 'default' => '',
153 'options' => WC()->countries->get_shipping_countries(),
154 'custom_attributes' => array(
155 'data-placeholder' => __( 'Select some countries', 'woocommerce' )
156 )
157 )
158 );
159 }
160
161 162 163 164 165 166
167 function admin_options() {
168 global $woocommerce; ?>
169 <h3><?php echo $this->method_title; ?></h3>
170 <p><?php _e( 'Local delivery is a simple shipping method for delivering orders locally.', 'woocommerce' ); ?></p>
171 <table class="form-table">
172 <?php $this->generate_settings_html(); ?>
173 </table> <?php
174 }
175
176
177 178 179 180 181 182 183
184 function is_available( $package ) {
185
186 if ($this->enabled=="no") return false;
187
188
189 $codes = '';
190 if ( $this->codes != '' ) {
191 foreach( explode( ',', $this->codes ) as $code ) {
192 $codes[] = $this->clean( $code );
193 }
194 }
195
196 if ( is_array( $codes ) ) {
197
198 $found_match = false;
199
200 if ( in_array( $this->clean( $package['destination']['postcode'] ), $codes ) ) {
201 $found_match = true;
202 }
203
204
205
206 if ( ! $found_match ) {
207
208 $customer_postcode = $this->clean( $package['destination']['postcode'] );
209 foreach ($codes as $c) {
210 $pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', $c ) . '$/i';
211 if ( preg_match( $pattern, $customer_postcode ) ) {
212 $found_match = true;
213 break;
214 }
215 }
216
217 }
218
219
220
221 if ( ! $found_match ) {
222
223 $customer_postcode = $this->clean( $package['destination']['postcode'] );
224 $customer_postcode_length = strlen( $customer_postcode );
225
226 for ( $i = 0; $i <= $customer_postcode_length; $i++ ) {
227
228 if ( in_array( $customer_postcode, $codes ) ) {
229 $found_match = true;
230 }
231
232 $customer_postcode = substr( $customer_postcode, 0, -2 ) . '*';
233 }
234 }
235
236 if ( ! $found_match ) {
237 return false;
238 }
239 }
240
241
242 if ( $this->availability == 'specific' ) {
243 $ship_to_countries = $this->countries;
244 } else {
245 $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
246 }
247
248 if (is_array($ship_to_countries)) {
249 if (!in_array( $package['destination']['country'] , $ship_to_countries)){
250 return false;
251 }
252 }
253
254
255 return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true, $package );
256 }
257
258
259 260 261 262 263 264 265
266 function clean( $code ) {
267 return str_replace( '-', '', sanitize_title( $code ) ) . ( strstr( $code, '*' ) ? '*' : '' );
268 }
269
270 }
271