Completed
Pull Request — master (#9826)
by Mike
15:37
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 16 and the first side effect is on line 3.

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
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;
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...
94
		$has_coupon         = false;
95
		$has_met_min_amount = false;
96
97 View Code Duplication
		if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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 ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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() {
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...
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