1
|
|
|
<?php |
|
|
|
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
3
|
|
|
exit; |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Free Shipping Method. |
8
|
|
|
* |
9
|
|
|
* A simple shipping method for free shipping. |
10
|
|
|
* |
11
|
|
|
* @class WC_Shipping_Free_Shipping |
12
|
|
|
* @version 2.6.0 |
13
|
|
|
* @package WooCommerce/Classes/Shipping |
14
|
|
|
* @author WooThemes |
15
|
|
|
*/ |
16
|
|
|
class WC_Shipping_Free_Shipping extends WC_Shipping_Method { |
17
|
|
|
|
18
|
|
|
/** @var float Min amount to be valid */ |
19
|
|
|
public $min_amount = 0; |
20
|
|
|
|
21
|
|
|
/** @var string Requires option */ |
22
|
|
|
public $requires = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
*/ |
27
|
|
|
public function __construct( $instance_id = 0 ) { |
28
|
|
|
$this->id = 'free_shipping'; |
29
|
|
|
$this->instance_id = absint( $instance_id ); |
30
|
|
|
$this->method_title = __( 'Free Shipping', 'woocommerce' ); |
31
|
|
|
$this->method_description = __( 'Free Shipping is a special method which can be triggered with coupons and minimum spends.', 'woocommerce' ); |
32
|
|
|
$this->supports = array( |
33
|
|
|
'shipping-zones', |
34
|
|
|
'instance-settings' |
35
|
|
|
); |
36
|
|
|
$this->enabled = $this->get_option( 'enabled' ); |
37
|
|
|
$this->title = $this->get_option( 'title' ); |
38
|
|
|
$this->min_amount = $this->get_option( 'min_amount', 0 ); |
39
|
|
|
$this->requires = $this->get_option( 'requires' ); |
40
|
|
|
|
41
|
|
|
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get setting form fields for instances of this shipping method within zones. |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function get_instance_form_fields() { |
49
|
|
|
return array( |
50
|
|
|
'enabled' => array( |
51
|
|
|
'title' => __( 'Enable/Disable', 'woocommerce' ), |
52
|
|
|
'type' => 'checkbox', |
53
|
|
|
'label' => __( 'Enable Free Shipping', 'woocommerce' ), |
54
|
|
|
'default' => 'no' |
55
|
|
|
), |
56
|
|
|
'title' => array( |
57
|
|
|
'title' => __( 'Title', 'woocommerce' ), |
58
|
|
|
'type' => 'text', |
59
|
|
|
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), |
60
|
|
|
'default' => $this->method_title, |
61
|
|
|
'desc_tip' => true, |
62
|
|
|
), |
63
|
|
|
'requires' => array( |
64
|
|
|
'title' => __( 'Free Shipping Requires...', 'woocommerce' ), |
65
|
|
|
'type' => 'select', |
66
|
|
|
'class' => 'wc-enhanced-select', |
67
|
|
|
'default' => '', |
68
|
|
|
'options' => array( |
69
|
|
|
'' => __( 'N/A', 'woocommerce' ), |
70
|
|
|
'coupon' => __( 'A valid free shipping coupon', 'woocommerce' ), |
71
|
|
|
'min_amount' => __( 'A minimum order amount (defined below)', 'woocommerce' ), |
72
|
|
|
'either' => __( 'A minimum order amount OR a coupon', 'woocommerce' ), |
73
|
|
|
'both' => __( 'A minimum order amount AND a coupon', 'woocommerce' ), |
74
|
|
|
) |
75
|
|
|
), |
76
|
|
|
'min_amount' => array( |
77
|
|
|
'title' => __( 'Minimum Order Amount', 'woocommerce' ), |
78
|
|
|
'type' => 'price', |
79
|
|
|
'placeholder' => wc_format_localized_price( 0 ), |
80
|
|
|
'description' => __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ), |
81
|
|
|
'default' => '0', |
82
|
|
|
'desc_tip' => true |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* See if free shipping is available based on the package and cart. |
89
|
|
|
* @param array $package |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function is_available( $package ) { |
93
|
|
|
$is_available = false; |
|
|
|
|
94
|
|
|
$has_coupon = false; |
95
|
|
|
$has_met_min_amount = false; |
96
|
|
|
|
97
|
|
View Code Duplication |
if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) { |
|
|
|
|
98
|
|
|
if ( $coupons = WC()->cart->get_coupons() ) { |
99
|
|
|
foreach ( $coupons as $code => $coupon ) { |
100
|
|
|
if ( $coupon->is_valid() && $coupon->enable_free_shipping() ) { |
101
|
|
|
$has_coupon = true; |
102
|
|
|
break; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
View Code Duplication |
if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) { |
|
|
|
|
109
|
|
|
if ( WC()->cart->prices_include_tax ) { |
110
|
|
|
$total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes ); |
111
|
|
|
} else { |
112
|
|
|
$total = WC()->cart->cart_contents_total; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ( $total >= $this->min_amount ) { |
116
|
|
|
$has_met_min_amount = true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
switch ( $this->requires ) { |
121
|
|
|
case 'min_amount' : |
122
|
|
|
$is_available = $has_met_min_amount; |
123
|
|
|
break; |
124
|
|
|
case 'coupon' : |
125
|
|
|
$is_available = $has_coupon; |
126
|
|
|
break; |
127
|
|
|
case 'both' : |
128
|
|
|
$is_available = $has_met_min_amount && $has_coupon; |
129
|
|
|
break; |
130
|
|
|
case 'either' : |
131
|
|
|
$is_available = $has_met_min_amount || $has_coupon; |
132
|
|
|
break; |
133
|
|
|
default : |
134
|
|
|
$is_available = true; |
135
|
|
|
break; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Called to calculate shipping rates for this method. Rates can be added using the add_rate() method. |
143
|
|
|
* @uses WC_Shipping_Method::add_rate() |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function calculate_shipping() { |
|
|
|
|
146
|
|
|
$this->add_rate( array( |
147
|
|
|
'id' => $this->id . $this->instance_id, |
148
|
|
|
'label' => $this->title, |
149
|
|
|
'cost' => 0, |
150
|
|
|
'taxes' => false |
151
|
|
|
) ); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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.