Completed
Pull Request — master (#9826)
by Mike
15:37
created

WC_Shipping_Legacy_Local_Delivery::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 6
Ratio 100 %
Metric Value
dl 6
loc 6
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 4.

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.

Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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' );
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$availability has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
54
		$this->countries    = $this->get_option( 'countries' );
0 ignored issues
show
Deprecated Code introduced by
The property WC_Shipping_Method::$countries has been deprecated with message: 2.6.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
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