WC_Shipping_Free_Shipping   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 18
lcom 3
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
B get_instance_form_fields() 0 44 1
A calculate_shipping() 0 8 1
D is_available() 0 43 15
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
			'instance-settings-modal',
36
		);
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
			'title' => array(
51
				'title' 		=> __( 'Title', 'woocommerce' ),
52
				'type' 			=> 'text',
53
				'description' 	=> __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
54
				'default'		=> $this->method_title,
55
				'desc_tip'		=> true,
56
			),
57
			'requires' => array(
58
				'title' 		=> __( 'Free Shipping Requires...', 'woocommerce' ),
59
				'type' 			=> 'select',
60
				'class'         => 'wc-enhanced-select',
61
				'default' 		=> '',
62
				'options'		=> array(
63
					'' 				=> __( 'N/A', 'woocommerce' ),
64
					'coupon'		=> __( 'A valid free shipping coupon', 'woocommerce' ),
65
					'min_amount' 	=> __( 'A minimum order amount', 'woocommerce' ),
66
					'either' 		=> __( 'A minimum order amount OR a coupon', 'woocommerce' ),
67
					'both' 			=> __( 'A minimum order amount AND a coupon', 'woocommerce' ),
68
				)
69
			),
70
			'min_amount' => array(
71
				'title' 		=> __( 'Minimum Order Amount', 'woocommerce' ),
72
				'type' 			=> 'price',
73
				'placeholder'	=> wc_format_localized_price( 0 ),
74
				'description' 	=> __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ),
75
				'default' 		=> '0',
76
				'desc_tip'		=> true
77
			)
78
		);
79
80
		wc_enqueue_js( "
0 ignored issues
show
Unused Code introduced by
wc_enqueue_js(' jQuer....change(); }); '); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
81
			jQuery( function( $ ) {
82
				$('#woocommerce_free_shipping_requires').change(function(){
83
					if ( $(this).val() === 'coupon' || $(this).val() === '' ) {
84
						$('#woocommerce_free_shipping_min_amount').closest('tr').hide();
85
					} else {
86
						$('#woocommerce_free_shipping_min_amount').closest('tr').show();
87
					}
88
				}).change();
89
			});
90
		" );
91
	}
92
93
	/**
94
	 * See if free shipping is available based on the package and cart.
95
	 * @param array $package
96
	 * @return bool
97
	 */
98
	public function is_available( $package ) {
99
		$has_coupon         = false;
100
		$has_met_min_amount = false;
101
102
		if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) {
103
			if ( $coupons = WC()->cart->get_coupons() ) {
104
				foreach ( $coupons as $code => $coupon ) {
105
					if ( $coupon->is_valid() && $coupon->enable_free_shipping() ) {
106
						$has_coupon = true;
107
						break;
108
					}
109
				}
110
			}
111
		}
112
113
		if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) {
114
			$total = WC()->cart->get_displayed_subtotal();
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( $package = array() ) {
147
		$this->add_rate( array(
148
			'label'   => $this->title,
149
			'cost' 	  => 0,
150
			'taxes'   => false,
151
			'package' => $package,
152
		) );
153
	}
154
}
155