Completed
Pull Request — master (#9826)
by Mike
21:27
created

WC_Shipping_Free_Shipping::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 17
Ratio 100 %
Metric Value
dl 17
loc 17
rs 9.4286
cc 1
eloc 10
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;
5
}
6
7
/**
8
 * Free Shipping Method.
9
 *
10
 * A simple shipping method for free shipping.
11
 *
12
 * @class   WC_Shipping_Free_Shipping
13
 * @version 2.6.0
14
 * @package WooCommerce/Classes/Shipping
15
 * @author  WooThemes
16
 */
17
class WC_Shipping_Free_Shipping extends WC_Shipping_Method {
18
19
	/** @var float Min amount to be valid */
20
	public $min_amount = 0;
21
22
	/** @var string Requires option */
23
	public $requires   = '';
24
25
	/**
26
	 * Constructor.
27
	 */
28
	public function __construct( $instance_id = 0 ) {
29
		$this->id 			         = 'free_shipping';
30
		$this->instance_id 			 = absint( $instance_id );
31
		$this->method_title          = __( 'Free Shipping', 'woocommerce' );
32
		$this->method_description    = __( 'Free Shipping is a special method which can be triggered with coupons and minimum spends.', 'woocommerce' );
33
		$this->supports              = array(
34
			'shipping-zones',
35
			'instance-settings'
36
		);
37
		$this->enabled		         = $this->get_option( 'enabled' );
38
		$this->title 		         = $this->get_option( 'title' );
39
		$this->min_amount 	         = $this->get_option( 'min_amount', 0 );
40
		$this->requires		         = $this->get_option( 'requires' );
41
42
		add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
43
	}
44
45
	/**
46
	 * Get setting form fields for instances of this shipping method within zones.
47
	 * @return array
48
	 */
49
	public function get_instance_form_fields() {
50
		return array(
51
			'enabled' => array(
52
				'title' 		=> __( 'Enable/Disable', 'woocommerce' ),
53
				'type' 			=> 'checkbox',
54
				'label' 		=> __( 'Enable Free Shipping', 'woocommerce' ),
55
				'default' 		=> 'no'
56
			),
57
			'title' => array(
58
				'title' 		=> __( 'Title', 'woocommerce' ),
59
				'type' 			=> 'text',
60
				'description' 	=> __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
61
				'default'		=> $this->method_title,
62
				'desc_tip'		=> true,
63
			),
64
			'requires' => array(
65
				'title' 		=> __( 'Free Shipping Requires...', 'woocommerce' ),
66
				'type' 			=> 'select',
67
				'class'         => 'wc-enhanced-select',
68
				'default' 		=> '',
69
				'options'		=> array(
70
					'' 				=> __( 'N/A', 'woocommerce' ),
71
					'coupon'		=> __( 'A valid free shipping coupon', 'woocommerce' ),
72
					'min_amount' 	=> __( 'A minimum order amount (defined below)', 'woocommerce' ),
73
					'either' 		=> __( 'A minimum order amount OR a coupon', 'woocommerce' ),
74
					'both' 			=> __( 'A minimum order amount AND a coupon', 'woocommerce' ),
75
				)
76
			),
77
			'min_amount' => array(
78
				'title' 		=> __( 'Minimum Order Amount', 'woocommerce' ),
79
				'type' 			=> 'price',
80
				'placeholder'	=> wc_format_localized_price( 0 ),
81
				'description' 	=> __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ),
82
				'default' 		=> '0',
83
				'desc_tip'		=> true
84
			)
85
		);
86
	}
87
88
	/**
89
	 * See if free shipping is available based on the package and cart.
90
	 * @param array $package
91
	 * @return bool
92
	 */
93
	public function is_available( $package ) {
94
		$is_available       = false;
0 ignored issues
show
Unused Code introduced by
$is_available is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
		$has_coupon         = false;
96
		$has_met_min_amount = false;
97
98
		if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) {
99
			if ( $coupons = WC()->cart->get_coupons() ) {
100
				foreach ( $coupons as $code => $coupon ) {
101
					if ( $coupon->is_valid() && $coupon->enable_free_shipping() ) {
102
						$has_coupon = true;
103
						break;
104
					}
105
				}
106
			}
107
		}
108
109
		if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) {
110
			if ( WC()->cart->prices_include_tax ) {
111
				$total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes );
112
			} else {
113
				$total = WC()->cart->cart_contents_total;
114
			}
115
116
			if ( $total >= $this->min_amount ) {
117
				$has_met_min_amount = true;
118
			}
119
		}
120
121
		switch ( $this->requires ) {
122
			case 'min_amount' :
123
				$is_available = $has_met_min_amount;
124
			break;
125
			case 'coupon' :
126
				$is_available = $has_coupon;
127
			break;
128
			case 'both' :
129
				$is_available = $has_met_min_amount && $has_coupon;
130
			break;
131
			case 'either' :
132
				$is_available = $has_met_min_amount || $has_coupon;
133
			break;
134
			default :
135
				$is_available = true;
136
			break;
137
		}
138
139
		return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package );
140
	}
141
142
	/**
143
	 * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method.
144
	 * @uses WC_Shipping_Method::add_rate()
145
	 */
146
	public function calculate_shipping() {
147
		$this->add_rate( array(
148
			'id' 	 => $this->id . $this->instance_id,
149
			'label'  => $this->title,
150
			'cost' 	 => 0,
151
			'taxes'  => false
152
		) );
153
	}
154
}
155