1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Free 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.4.0 |
14
|
|
|
* @package WooCommerce/Classes/Shipping |
15
|
|
|
* @author WooThemes |
16
|
|
|
*/ |
17
|
|
|
class WC_Shipping_Legacy_Free_Shipping extends WC_Shipping_Method { |
18
|
|
|
|
19
|
|
|
/** @var float Min amount to be valid */ |
20
|
|
|
public $min_amount; |
21
|
|
|
|
22
|
|
|
/** @var string Requires option */ |
23
|
|
|
public $requires; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function __construct() { |
|
|
|
|
29
|
|
|
$this->id = 'legacy_free_shipping'; |
30
|
|
|
$this->method_title = __( 'Free Shipping (Legacy)', 'woocommerce' ); |
31
|
|
|
$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' ) ); |
32
|
|
|
$this->init(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return the name of the option in the WP DB. |
37
|
|
|
* @since 2.6.0 |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function get_option_key() { |
41
|
|
|
return $this->plugin_id . 'free_shipping' . '_settings'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* init function. |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function init() { |
|
|
|
|
48
|
|
|
|
49
|
|
|
// Load the settings. |
50
|
|
|
$this->init_form_fields(); |
51
|
|
|
$this->init_settings(); |
52
|
|
|
|
53
|
|
|
// Define user set variables |
54
|
|
|
$this->enabled = $this->get_option( 'enabled' ); |
55
|
|
|
$this->title = $this->get_option( 'title' ); |
56
|
|
|
$this->min_amount = $this->get_option( 'min_amount', 0 ); |
57
|
|
|
$this->availability = $this->get_option( 'availability' ); |
|
|
|
|
58
|
|
|
$this->countries = $this->get_option( 'countries' ); |
|
|
|
|
59
|
|
|
$this->requires = $this->get_option( 'requires' ); |
60
|
|
|
|
61
|
|
|
// Actions |
62
|
|
|
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Initialise Gateway Settings Form Fields. |
67
|
|
|
*/ |
68
|
|
|
public function init_form_fields() { |
69
|
|
|
$this->form_fields = array( |
70
|
|
|
'enabled' => array( |
71
|
|
|
'title' => __( 'Enable/Disable', 'woocommerce' ), |
72
|
|
|
'type' => 'checkbox', |
73
|
|
|
'label' => __( 'Enable Free Shipping', 'woocommerce' ), |
74
|
|
|
'default' => 'no' |
75
|
|
|
), |
76
|
|
|
'title' => array( |
77
|
|
|
'title' => __( 'Method Title', 'woocommerce' ), |
78
|
|
|
'type' => 'text', |
79
|
|
|
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), |
80
|
|
|
'default' => __( 'Free Shipping', 'woocommerce' ), |
81
|
|
|
'desc_tip' => true, |
82
|
|
|
), |
83
|
|
|
'availability' => array( |
84
|
|
|
'title' => __( 'Method availability', 'woocommerce' ), |
85
|
|
|
'type' => 'select', |
86
|
|
|
'default' => 'all', |
87
|
|
|
'class' => 'availability wc-enhanced-select', |
88
|
|
|
'options' => array( |
89
|
|
|
'all' => __( 'All allowed countries', 'woocommerce' ), |
90
|
|
|
'specific' => __( 'Specific Countries', 'woocommerce' ) |
91
|
|
|
) |
92
|
|
|
), |
93
|
|
|
'countries' => array( |
94
|
|
|
'title' => __( 'Specific Countries', 'woocommerce' ), |
95
|
|
|
'type' => 'multiselect', |
96
|
|
|
'class' => 'wc-enhanced-select', |
97
|
|
|
'css' => 'width: 450px;', |
98
|
|
|
'default' => '', |
99
|
|
|
'options' => WC()->countries->get_shipping_countries(), |
100
|
|
|
'custom_attributes' => array( |
101
|
|
|
'data-placeholder' => __( 'Select some countries', 'woocommerce' ) |
102
|
|
|
) |
103
|
|
|
), |
104
|
|
|
'requires' => array( |
105
|
|
|
'title' => __( 'Free Shipping Requires...', 'woocommerce' ), |
106
|
|
|
'type' => 'select', |
107
|
|
|
'class' => 'wc-enhanced-select', |
108
|
|
|
'default' => '', |
109
|
|
|
'options' => array( |
110
|
|
|
'' => __( 'N/A', 'woocommerce' ), |
111
|
|
|
'coupon' => __( 'A valid free shipping coupon', 'woocommerce' ), |
112
|
|
|
'min_amount' => __( 'A minimum order amount (defined below)', 'woocommerce' ), |
113
|
|
|
'either' => __( 'A minimum order amount OR a coupon', 'woocommerce' ), |
114
|
|
|
'both' => __( 'A minimum order amount AND a coupon', 'woocommerce' ), |
115
|
|
|
) |
116
|
|
|
), |
117
|
|
|
'min_amount' => array( |
118
|
|
|
'title' => __( 'Minimum Order Amount', 'woocommerce' ), |
119
|
|
|
'type' => 'price', |
120
|
|
|
'placeholder' => wc_format_localized_price( 0 ), |
121
|
|
|
'description' => __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ), |
122
|
|
|
'default' => '0', |
123
|
|
|
'desc_tip' => true |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* is_available function. |
130
|
|
|
* @param array $package |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function is_available( $package ) { |
134
|
|
|
if ( 'no' == $this->enabled ) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ( 'specific' == $this->availability ) { |
|
|
|
|
139
|
|
|
$ship_to_countries = $this->countries; |
|
|
|
|
140
|
|
|
} else { |
141
|
|
|
$ship_to_countries = array_keys( WC()->countries->get_shipping_countries() ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Enabled logic |
149
|
|
|
$is_available = false; |
150
|
|
|
$has_coupon = false; |
151
|
|
|
$has_met_min_amount = false; |
152
|
|
|
|
153
|
|
View Code Duplication |
if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) { |
|
|
|
|
154
|
|
|
|
155
|
|
|
if ( $coupons = WC()->cart->get_coupons() ) { |
156
|
|
|
foreach ( $coupons as $code => $coupon ) { |
157
|
|
|
if ( $coupon->is_valid() && $coupon->enable_free_shipping() ) { |
158
|
|
|
$has_coupon = true; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
View Code Duplication |
if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) { |
|
|
|
|
165
|
|
|
if ( WC()->cart->prices_include_tax ) { |
166
|
|
|
$total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes ); |
167
|
|
|
} else { |
168
|
|
|
$total = WC()->cart->cart_contents_total; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ( $total >= $this->min_amount ) { |
172
|
|
|
$has_met_min_amount = true; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
switch ( $this->requires ) { |
177
|
|
|
case 'min_amount' : |
178
|
|
|
if ( $has_met_min_amount ) { |
179
|
|
|
$is_available = true; |
180
|
|
|
} |
181
|
|
|
break; |
182
|
|
|
case 'coupon' : |
183
|
|
|
if ( $has_coupon ) { |
184
|
|
|
$is_available = true; |
185
|
|
|
} |
186
|
|
|
break; |
187
|
|
|
case 'both' : |
188
|
|
|
if ( $has_met_min_amount && $has_coupon ) { |
189
|
|
|
$is_available = true; |
190
|
|
|
} |
191
|
|
|
break; |
192
|
|
|
case 'either' : |
193
|
|
|
if ( $has_met_min_amount || $has_coupon ) { |
194
|
|
|
$is_available = true; |
195
|
|
|
} |
196
|
|
|
break; |
197
|
|
|
default : |
198
|
|
|
$is_available = true; |
199
|
|
|
break; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* calculate_shipping function. |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
View Code Duplication |
public function calculate_shipping() { |
|
|
|
|
210
|
|
|
$args = array( |
211
|
|
|
'id' => $this->id, |
212
|
|
|
'label' => $this->title, |
213
|
|
|
'cost' => 0, |
214
|
|
|
'taxes' => false |
215
|
|
|
); |
216
|
|
|
$this->add_rate( $args ); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
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.