PluginProbe ʕ •ᴥ•ʔ
Weight Based Shipping Table Rate for WooCommerce – Flexible Shipping / trunk
Weight Based Shipping Table Rate for WooCommerce – Flexible Shipping vtrunk
6.8.1 6.8.0 6.7.4 6.7.3 6.7.2 6.7.0 4.21.7 4.22.0 4.22.1 4.23.0 4.23.1 4.23.2 4.23.3 4.24.0 4.24.1 4.24.10 4.24.11 4.24.12 4.24.13 4.24.14 4.24.15 4.24.16 4.24.17 4.24.18 4.24.19 4.24.2 4.24.4 4.24.6 4.24.7 4.24.8 4.24.9 4.25.0 4.25.1 4.25.2 4.25.4 4.25.5 4.25.6 4.25.7 4.25.8 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 4.8.0 4.8.1 4.8.3 4.8.4 4.8.5 4.9.0 5.0.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.0 5.1.1 5.1.2 5.1.3 5.2.0 5.3.0 5.3.1 5.3.3 5.3.4 5.3.5 6.0.0 6.1.0 6.1.1 6.1.2 6.2.0 6.2.1 6.2.2 6.2.3 6.2.4 6.3.0 trunk 6.4.0 1.4 6.4.1 1.4.1 6.4.2 1.5 6.4.3 1.6 6.4.5 1.6.1 6.5.0 1.6.2 6.5.1 1.7 6.5.2 1.8 6.5.3 1.9 6.5.4 1.9.1 6.5.5 1.9.10 6.5.6 1.9.11 6.5.7 1.9.12 6.5.8 1.9.2 6.6.0 1.9.3 6.6.1 1.9.4 6.6.2 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0 2.0.1 2.1 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.6 3.0.7 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.11.4 3.12.0 3.13.0 3.14.0 3.14.1 3.14.2 3.14.3 3.14.4 3.15.0 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.2.0 3.3.1 3.3.10 3.3.11 3.3.12 3.3.13 3.3.14 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.8.0 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.9 4.0.0 4.0.1 4.0.10 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.11.0 4.11.1 4.11.2 4.11.3 4.11.4 4.11.5 4.11.6 4.11.7 4.11.8 4.11.9 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.14.0 4.14.1 4.14.2 4.15.0 4.16.0 4.16.1 4.16.2 4.17.0 4.18.0 4.18.1 4.18.2 4.18.3 4.18.4 4.18.5 4.18.6 4.19.0 4.19.1 4.2.0 4.20.0 4.20.1 4.20.2 4.20.3 4.21.0 4.21.1 4.21.2 4.21.3 4.21.4 4.21.5
flexible-shipping / classes / table-rate / shipping-method.php
flexible-shipping / classes / table-rate Last commit date
logger 1 week ago settings 1 week ago views 1 week ago class-shortcode-unit-dimension.php 1 week ago class-shortcode-unit-weight.php 1 week ago class-wpdesk-flexible-shipping-multilingual.php 1 week ago flexible-shipping-settings.php 1 week ago order-item-meta.php 1 week ago shipping-method.php 1 week ago
shipping-method.php
672 lines
1 <?php
2
3 use FSVendor\Psr\Log\LoggerInterface;
4 use FSVendor\WPDesk\Beacon\Beacon\WooCommerceSettingsFieldsModifier;
5 use FSVendor\WPDesk\FS\TableRate\Logger\NoticeLogger;
6 use FSVendor\WPDesk\FS\TableRate\Logger\ShippingMethodLogger;
7 use FSVendor\WPDesk\FS\TableRate\Settings\MethodSettings;
8 use FSVendor\WPDesk\FS\TableRate\Settings\MethodSettingsFactory;
9 use WPDesk\FS\TableRate\Rates\FlexibleShippingRates;
10 use WPDesk\FS\TableRate\RulesSettingsField;
11 use WPDesk\FS\TableRate\ShippingMethod\CommonMethodSettings;
12 use WPDesk\FS\TableRate\ShippingMethod\RateCalculatorFactory;
13 use WPDesk\FS\TableRate\ShippingMethod\SettingsDisplayPreparer;
14 use WPDesk\FS\TableRate\ShippingMethod\SettingsProcessor;
15
16 class WPDesk_Flexible_Shipping extends WC_Shipping_Method {
17
18 const METHOD_ID = 'flexible_shipping';
19
20 const FIELD_METHOD_FREE_SHIPPING = 'method_free_shipping';
21
22 const META_DEFAULT = '_default';
23
24 const WEIGHT_ROUNDING_PRECISION = 6;
25
26 const SETTING_METHOD_RULES = 'method_rules';
27
28 const SETTING_METHOD_FREE_SHIPPING_NOTICE = 'method_free_shipping_cart_notice';
29
30 /**
31 * Logger provided by Flexible Shipping plugin.
32 *
33 * @var LoggerInterface
34 */
35 protected static $fs_logger;
36
37 /**
38 * Message added.
39 *
40 * @var bool
41 */
42 private $message_added = false;
43
44 /**
45 * @var string
46 *
47 * See Active Payments - must be public
48 */
49 public $shipping_methods_option;
50
51 /**
52 * @var string
53 */
54 private $shipping_method_order_option;
55
56 /**
57 * @var string
58 */
59 private $section_name;
60
61 public string $type;
62
63 /**
64 * Constructor for your shipment class
65 *
66 * @access public
67 * @return void
68 */
69 public function __construct( $instance_id = 0 ) {
70 $this->instance_id = absint( $instance_id );
71 $this->id = self::METHOD_ID;
72 $this->shipping_methods_option = 'flexible_shipping_methods_' . $this->instance_id;
73 $this->shipping_method_order_option = 'flexible_shipping_method_order_' . $this->instance_id;
74 $this->section_name = 'flexible_shipping';
75 $this->method_title = __( 'Flexible Shipping Group', 'flexible-shipping' );
76 $this->method_description = __( 'A group of Flexible Shipping methods - useful to organize numerous shipping methods.', 'flexible-shipping' );
77
78 $this->supports = array(
79 'instance-settings',
80 );
81
82 if ( $this->is_allowed_support_shipping_zones() ) {
83 $this->supports[] = 'shipping-zones';
84 }
85
86 $this->instance_form_fields = array(
87 'enabled' => array(
88 'title' => __( 'Enable/Disable', 'flexible-shipping' ),
89 'type' => 'checkbox',
90 'label' => __( 'Enable this shipment method', 'flexible-shipping' ),
91 'default' => 'yes',
92 ),
93 'title' => array(
94 'title' => __( 'Shipping Title', 'flexible-shipping' ),
95 'type' => 'text',
96 'description' => __( 'This controls the title which the user sees during checkout.', 'flexible-shipping' ),
97 'default' => __( 'Flexible Shipping', 'flexible-shipping' ),
98 'desc_tip' => true
99 )
100 );
101
102 if ( version_compare( WC()->version, '2.6' ) < 0 && $this->get_option( 'enabled', 'yes' ) == 'no' ) {
103 $this->enabled = $this->get_option( 'enabled' );
104 }
105
106 $this->title = $this->get_option( 'title' );
107
108 $this->init();
109
110 add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
111 }
112
113 /**
114 * Set logger. This logger is set by Flexible Shipping plugin.
115 *
116 * @param LoggerInterface $fs_logger .
117 */
118 public static function set_fs_logger( LoggerInterface $fs_logger ) {
119 static::$fs_logger = $fs_logger;
120 }
121
122 /**
123 * @param MethodSettings $shipping_method_settings
124 *
125 * @return \WPDesk\FS\TableRate\Logger\ShippingMethodLogger
126 */
127 private function prepare_shipping_method_calculation_logger( $shipping_method_settings ) {
128 $method_debug_mode = $shipping_method_settings->get_debug_mode();
129 $shipping_method_title = $shipping_method_settings->get_title();
130 $shipping_method_url = admin_url(
131 'admin.php?page=wc-settings&tab=shipping&instance_id=' . sanitize_key( $this->instance_id ) . '&action=edit&method_id=' . sanitize_key( $shipping_method_settings->get_id() )
132 );
133 if ( null !== static::$fs_logger ) {
134 $fs_logger = static::$fs_logger;
135 } else {
136 $fs_logger = NullLogger();
137 }
138
139 return new ShippingMethodLogger(
140 $fs_logger,
141 new NoticeLogger(
142 $shipping_method_title,
143 $shipping_method_url,
144 'yes' === $method_debug_mode && current_user_can( 'manage_woocommerce' )
145 )
146 );
147 }
148
149 /**
150 * Init your settings
151 *
152 * @access public
153 * @return void
154 */
155 function init() {
156 $this->instance_form_fields = include( 'settings/flexible-shipping.php' );
157 // Load the settings API
158 $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
159 $this->init_settings(); // This is part of the settings API. Loads settings you previously init.
160
161 // Define user set variables
162 $this->title = $this->get_option( 'title' );
163 $this->tax_status = $this->get_option( 'tax_status' );
164
165 $this->availability = $this->get_option( 'availability' );
166
167 $this->type = $this->get_option( 'type', 'class' );
168 }
169
170 /**
171 * Initialise Settings Form Fields
172 */
173 public function init_form_fields() {
174 $this->form_fields = include( 'settings/flexible-shipping.php' );
175 $this->form_fields = $this->add_beacon_search_data_to_fields( $this->form_fields );
176 }
177
178 public function generate_title_shipping_methods_html( $key, $data ) {
179 $field = $this->get_field_key( $key );
180 $defaults = array(
181 'title' => '',
182 'class' => ''
183 );
184
185 $data = wp_parse_args( $data, $defaults );
186
187 ob_start();
188
189 ?>
190 </table>
191 <h3 class="wc-settings-sub-title <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?>
192 <?php if ( $this->is_allowed_editing() ): ?>
193 <a href="<?php echo esc_url( remove_query_arg( 'added', add_query_arg( 'action', 'new' ) ) ); ?>"
194 class="add-new-h2"><?php echo __( 'Add New', 'flexible-shipping' ); ?></a></h3>
195 <?php endif; ?>
196
197 <?php if ( ! empty( $data['description'] ) ) : ?>
198 <p><?php echo wp_kses_post( $data['description'] ); ?></p>
199 <?php endif; ?>
200 </div><table class="form-table">
201 <?php
202
203 return ob_get_clean();
204 }
205
206 /**
207 * @param unknown $key
208 *
209 * @return string
210 *
211 * Dodane w WooCommerce 2.4
212 * Dodane w celu zachowania kompatybilności z WooCommerce 2.3
213 * Przetestowane na WooCommerce 2.3.9
214 */
215 public function get_field_key( $key ) {
216 return $this->plugin_id . $this->id . '_' . $key;
217 }
218
219 public function generate_shipping_methods_html( $key, $data ) {
220 $shipping_methods = $this->get_shipping_methods();
221 $shipping_method_order = $this->get_shipping_method_order();
222 ob_start();
223 include __DIR__ . '/views/html-shipping-method-settings.php';
224 return ob_get_clean();
225 }
226
227 public function get_shipping_methods( $enabled = false ) {
228 $shipping_methods = $this->get_option_shipping_methods();
229 $shipping_method_order = get_option( $this->shipping_method_order_option, array() );
230 $ret = array();
231 if ( is_array( $shipping_method_order ) ) {
232 foreach ( $shipping_method_order as $method_id ) {
233 if ( isset( $shipping_methods[$method_id] ) ) {$ret[$method_id] = $shipping_methods[$method_id];}
234 }
235 }
236 foreach ( $shipping_methods as $shipping_method ) {
237 if ( !isset( $ret[$shipping_method['id']] ) ) {$ret[$shipping_method['id']] = $shipping_method;}
238 }
239 if ( $enabled ) {
240 foreach ( $ret as $key => $shipping_method ) {
241 if ( isset( $shipping_method['method_enabled'] ) && 'yes' != $shipping_method['method_enabled'] ) {unset($ret[$key]);}
242 }
243 }
244 return $ret;
245 }
246
247 private function get_shipping_method_order() {
248 $shipping_methods = $this->get_option_shipping_methods();
249 $shipping_method_order = get_option( $this->shipping_method_order_option, array() );
250 $ret = array();
251 if ( is_array( $shipping_method_order ) ) {
252 foreach ( $shipping_method_order as $method_id ) {
253 if ( isset( $shipping_methods[$method_id] ) ) {$ret[$method_id] = $method_id;}
254 }
255 }
256 foreach ( $shipping_methods as $shipping_method ) {
257 if ( !isset( $ret[$shipping_method['id']] ) ) {$ret[$shipping_method['id']] = $shipping_method['id'];}
258 }
259 return $ret;
260 }
261
262 /**
263 * Generates method rules field.
264 *
265 * @param string $key .
266 * @param array $data .
267 *
268 * @return string
269 */
270 /**
271 * Renders shipping rules settings.
272 *
273 * @param string $key .
274 * @param array $data .
275 *
276 * @return false|string
277 */
278 public function generate_shipping_rules_html( $key, $data ) {
279 $rules_settings = new RulesSettingsField(
280 $key,
281 self::SETTING_METHOD_RULES,
282 $data['title'],
283 $data,
284 null,
285 $this->instance_settings
286 );
287
288 return $rules_settings->render();
289 }
290
291 public function shipping_method_title_used( $title, $shipping_methods ) {
292 foreach ( $shipping_methods as $shipping_method ) {
293 if ( $title == $shipping_method['method_title'] ) {
294 return true;
295 }
296 }
297 return false;
298 }
299
300 public function shipping_method_next_id( $shipping_methods ) {
301 $next_id = 0;
302 foreach ( $shipping_methods as $shipping_method ) {
303 if ( intval($shipping_method['id'] ) > $next_id ) {
304 $next_id = intval($shipping_method['id'] );
305 }
306 }
307 $next_id++;
308 return $next_id;
309 }
310
311 public function process_admin_options() {
312 $action = false;
313 if ( isset( $_POST['method_action'] ) ) {
314 $action = sanitize_key( $_POST['method_action'] );
315 }
316
317 if ( isset( $_POST['import_action'] ) && $_POST['import_action'] == '1' ) {
318 $this->process_import_action();
319 } elseif ( $action === 'new' || $action === 'edit' ) {
320 $save_rules = new SettingsProcessor(
321 $this->id, $this->instance_id, $this->shipping_methods_option, $this->shipping_method_order_option
322 );
323
324 try {
325 $shipping_method_settings = $save_rules->process_and_save_settings( $action, $_POST );
326 if ( $action === 'new' ) {
327 $this->redirect_new_method( $shipping_method_settings['id'] );
328 }
329 } catch ( Exception $e ) {
330 $this->add_error( $e->getMessage() );
331 }
332
333 } else {
334 parent::process_admin_options();
335
336 if ( isset( $_POST['method_order'] ) ) {
337 $this->process_order_method();
338 }
339 }
340 }
341
342 /**
343 * @param string $method_id .
344 */
345 private function redirect_new_method( $method_id ) {
346 $redirect = add_query_arg( array('added' => $method_id, 'action' => false, 'method_id' => false ));
347 $redirect .= '#method_' . $method_id;
348 $redirect = add_query_arg( array('added' => $method_id, 'action' => 'edit', 'method_id' => $method_id ));
349 wpdesk_redirect( $redirect );
350 }
351
352 private function process_order_method() {
353 $method_order = $_POST['method_order'];
354 $method_order_security_alert = false;
355 foreach ( $method_order as $method_order_key => $method_id ) {
356 if ( strval( $method_order_key ) !== strval( sanitize_key( $method_order_key ) ) || strval( $method_id ) !== strval( sanitize_key( $method_id ) ) ) {
357 $method_order_security_alert = true;
358 }
359 }
360 if ( $method_order_security_alert ) {
361 WC_Admin_Settings::add_error( __( 'Flexible Shipping: security check error. Shipping method order not saved!', 'flexible-shipping' ) );
362 WC_Admin_Settings::show_messages();
363 } else {
364 update_option( $this->shipping_method_order_option, $method_order );
365 }
366 }
367
368 private function process_import_action() {
369 $shipping_methods = $this->get_option_shipping_methods();
370
371 if ( ! is_array( $shipping_methods ) ) {
372 $shipping_methods = array();
373 }
374
375 try {
376 $importer = ( new \WPDesk\FS\TableRate\ImporterExporter\Importer\ImporterFactory( $_FILES['import_file'], $this, $shipping_methods ) )->get_importer();
377
378 $importer->import();
379
380 $imported_shipping_methods = $importer->get_shipping_methods();
381
382 update_option( $this->shipping_methods_option, $imported_shipping_methods );
383 } catch ( Exception $e ) {
384 WC_Admin_Settings::add_error( $e->getMessage() );
385 }
386
387 WC_Admin_Settings::show_messages();
388 }
389
390 public function admin_options() {
391 $action = false;
392 if ( isset( $_GET['action'] ) )
393 {
394 $action = sanitize_key( $_GET['action'] );
395 }
396 $settings_div_class = in_array( $action, array( 'new', 'edit' ), true ) ? '' : 'fs-settings-div';
397 ?>
398 <div class="<?php echo esc_html( $settings_div_class ) ; ?>"><table class="form-table">
399 <?php
400 if ( $action == 'new' || $action == 'edit' ) {
401 $shipping_methods = $this->get_option_shipping_methods();
402 $shipping_method = array(
403 'method_title' => '',
404 'method_description' => '',
405 CommonMethodSettings::METHOD_LOGO_ID => 0,
406 'method_enabled' => 'no',
407 'method_shipping_zone' => '',
408 'method_calculation_method' => 'sum',
409 self::FIELD_METHOD_FREE_SHIPPING => '',
410 'method_free_shipping_label'=> '',
411 'method_visibility' => 'no',
412 'method_default' => 'no',
413 'method_integration' => '',
414 );
415 $method_id = '';
416 if ( $action == 'edit' ) {
417 $method_id = sanitize_key( $_GET['method_id'] );
418 $shipping_method = $shipping_methods[$method_id];
419 $method_id_for_shipping = $this->id . '_' . $this->instance_id . '_' . sanitize_title( $shipping_method['method_title'] );
420 $method_id_for_shipping = apply_filters( 'flexible_shipping_method_rate_id', $method_id_for_shipping, $shipping_method );
421 }
422 else {
423 $method_id_for_shipping = '';
424 }
425 ?>
426 <input type="hidden" name="method_action" value="<?php echo $action; ?>" />
427 <input type="hidden" name="method_id" value="<?php echo $method_id; ?>" />
428 <input type="hidden" name="method_id_for_shipping" value="<?php echo $method_id_for_shipping; ?>" />
429 <?php if ( $action == 'new' ) : ?>
430 <h2><?php _e('New Shipping Method', 'flexible-shipping' ); ?></h2>
431 <?php endif; ?>
432 <?php if ( $action == 'edit' ) : ?>
433 <h2><?php _e('Edit Shipping Method', 'flexible-shipping' ); ?></h2>
434 <?php endif; ?>
435 <?php
436 if ( isset( $_GET['added'] ) ) {
437 $method_id = sanitize_key( $_GET['added'] );
438 $shipping_methods = $this->get_option_shipping_methods();
439 if ( isset( $shipping_methods[$method_id] ) )
440 {
441 if ( ! $this->message_added ) {
442 $shipping_method = $shipping_methods[$method_id];
443 WC_Admin_Settings::add_message( sprintf(__( 'Shipping method %s added.', 'flexible-shipping' ), $shipping_method['method_title'] ) );
444 $this->message_added = true;
445 }
446 }
447 WC_Admin_Settings::show_messages();
448 }
449 $shipping_method['woocommerce_method_instance_id'] = $this->instance_id;
450 $this->get_shipping_method_form($shipping_method);
451 $this->generate_settings_html();
452 }
453 else if ( $action == 'delete' ) {
454 $methods_id = '';
455 if ( isset( $_GET['methods_id'] ) ) {
456 $methods_id = explode( ',' , sanitize_text_field( $_GET['methods_id'] ) );
457 }
458 $shipping_methods = $this->get_option_shipping_methods();
459 $shipping_method_order = get_option( $this->shipping_method_order_option, array() );
460 foreach ( $methods_id as $method_id ) {
461 if ( isset( $shipping_methods[$method_id] ) ) {
462 $shipping_method = $shipping_methods[$method_id];
463 unset( $shipping_methods[$method_id] );
464 if ( isset( $shipping_method_order[$method_id] ) ) {
465 unset( $shipping_method_order[$method_id] );
466 }
467 update_option( $this->shipping_methods_option, $shipping_methods );
468 update_option( $this->shipping_method_order_option, $shipping_method_order );
469 WC_Admin_Settings::add_message( sprintf(__('Shipping method %s deleted.', 'flexible-shipping' ), $shipping_method['method_title'] ) );
470 }
471 else {
472 WC_Admin_Settings::add_error( __( 'Shipping method not found.', 'flexible-shipping' ) );
473 }
474 }
475 WC_Admin_Settings::show_messages();
476 $this->generate_settings_html();
477 }
478 else {
479 if ( isset( $_GET['added'] ) ) {
480 $method_id = sanitize_key( $_GET['added'] );
481 $shipping_methods = $this->get_option_shipping_methods();
482 if ( isset( $shipping_methods[$method_id] ) )
483 {
484 if ( ! $this->message_added ) {
485 $shipping_method = $shipping_methods[$method_id];
486 WC_Admin_Settings::add_message( sprintf(__( 'Shipping method %s added.', 'flexible-shipping' ), $shipping_method['method_title'] ) );
487 $this->message_added = true;
488 }
489 }
490 WC_Admin_Settings::show_messages();
491 }
492 if ( isset( $_GET['updated'] ) ) {
493 $method_id = sanitize_key( $_GET['updated'] );
494 $shipping_methods = $this->get_option_shipping_methods();
495 if ( isset( $shipping_methods[$method_id] ) )
496 {
497 $shipping_method = $shipping_methods[$method_id];
498 WC_Admin_Settings::add_message( sprintf(__( 'Shipping method %s updated.', 'flexible-shipping' ), $shipping_method['method_title'] ) );
499 }
500 WC_Admin_Settings::show_messages();
501 }
502
503 // General Settings
504 $this->generate_settings_html();
505 }
506 ?>
507 </table>
508 <?php include __DIR__ . '/views/html-shipping-method-scripts.php'; ?>
509 <?php do_action( 'flexible_shipping_method_script', self::METHOD_ID, $this->instance_id ); ?>
510 <?php
511 }
512
513 private function get_shipping_method_form( $shipping_method ) {
514 $this->form_fields = ( new CommonMethodSettings() )->get_settings_fields( $shipping_method, true );
515 $this->form_fields = ( new SettingsDisplayPreparer() )->prepare_settings_for_display( $this->form_fields );
516 }
517
518 private function package_weight( $items ) {
519 $weight = 0;
520 foreach( $items as $item ) {
521 $weight += $item['data']->weight * $item['quantity'];
522 }
523 return $weight;
524 }
525
526 private function woocommerce_product_weight( $weight ) {
527 if ( $weight === '' ) {
528 return 0;
529 }
530 return $weight;
531 }
532
533 private function package_item_count( $items ) {
534 $item_count = 0;
535
536 foreach( $items as $item ) {
537 $item_count += $item['quantity'];
538 }
539 return $item_count;
540 }
541
542 private function cart_item_count() {
543 $item_count = 0;
544
545 $cart = WC()->cart;
546 foreach( $cart->cart_contents as $item ) {
547 $item_count += $item['quantity'];
548 }
549
550 return $item_count;
551 }
552
553 /**
554 * @param array $package
555 */
556 public function calculate_shipping( $package = array() ) {
557 $shipping_methods = $this->get_shipping_methods( true );
558
559 $rate_calculator = RateCalculatorFactory::create_for_shipping_method( $this, $package );
560
561 foreach ( $shipping_methods as $shipping_method_settings ) {
562 $method_settings = MethodSettingsFactory::create_from_array_and_tax_status( $shipping_method_settings, $this->tax_status );
563 $logger = $this->prepare_shipping_method_calculation_logger( $method_settings );
564 $rate_calculator->set_logger( $logger );
565 $calculated_rate = $rate_calculator->calculate_rate( $method_settings, $this->prepare_rate_id( $method_settings->get_raw_settings() ), is_user_logged_in() );
566 if ( ! empty( $calculated_rate ) ) {
567 $this->add_rate( $calculated_rate );
568 $logger->debug( __( 'Shipping cost added.', 'flexible-shipping' ), $logger->get_results_context() );
569 }
570 $logger->show_notice_if_enabled();
571 }
572 }
573
574 /**
575 * @param string $suffix
576 *
577 * @return string
578 */
579 public function prepare_rate_id( $shipping_method_settings ) {
580 $id = $this->id . '_' . $this->instance_id . '_' . sanitize_title( $shipping_method_settings['method_title'] );
581 $id = apply_filters( 'flexible_shipping_method_rate_id', $id, $shipping_method_settings );
582
583 return $id;
584 }
585
586 public function set_as_converted() {
587 $shipping_method_key = $this->get_instance_option_key();
588 $shipping_method_options = $this->get_instance_options();
589
590 $shipping_method_options['converted'] = 'yes';
591
592 update_option( $shipping_method_key, $shipping_method_options );
593 }
594
595 /**
596 * @return bool
597 */
598 public function is_converted() {
599 $shipping_method_options = $this->get_instance_options();
600
601 return isset( $shipping_method_options['converted'] ) ? $shipping_method_options['converted'] === 'yes' : false;
602 }
603
604 /**
605 * @return array
606 */
607 private function get_instance_options() {
608 $options = get_option( $this->get_instance_option_key(), array() );
609
610 if ( ! is_array( $options ) ) {
611 $options = array();
612 }
613
614 return $options;
615 }
616
617 /**
618 * @return bool
619 */
620 private function prices_include_tax() {
621 return (bool) apply_filters( 'flexible_shipping_prices_include_tax', WC()->cart->display_prices_including_tax() );
622 }
623
624 /**
625 * @return array
626 */
627 public function get_all_rates() {
628 return FlexibleShippingRates::get_flexible_shipping_rates();
629 }
630
631 /**
632 * Add beacon search data to fields.
633 *
634 * @param array $form_fields .
635 *
636 * @return array
637 */
638 private function add_beacon_search_data_to_fields( array $form_fields ) {
639 $modifier = new WooCommerceSettingsFieldsModifier();
640
641 return $modifier->append_beacon_search_data_to_fields( $form_fields );
642 }
643
644 /**
645 * @return array
646 */
647 private function get_option_shipping_methods() {
648 $shipping_methods = get_option( $this->shipping_methods_option, array() );
649
650 if ( ! is_array( $shipping_methods ) ) {
651 $shipping_methods = array();
652 }
653
654 return $shipping_methods;
655 }
656
657 /**
658 * @return bool
659 */
660 private function is_allowed_support_shipping_zones() {
661 return ! is_admin() || (bool) apply_filters( 'flexible-shipping/group-method/supports/shipping-zones', false );
662 }
663
664 /**
665 * @return bool
666 */
667 private function is_allowed_editing() {
668 return (bool) apply_filters( 'flexible-shipping/group-method/supports/edit', false );
669 }
670
671 }
672