Completed
Push — master ( 09e43c...a8e479 )
by Mike
11:28
created

WC_Order_Item_Tax::set_rate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Order Line Item (tax).
8
 *
9
 * @version     2.7.0
10
 * @since       2.7.0
11
 * @package     WooCommerce/Classes
12
 * @author      WooThemes
13
 */
14
class WC_Order_Item_Tax extends WC_Order_Item {
15
16
	/**
17
	 * Data properties of this order item object.
18
	 * @since 2.7.0
19
	 * @var array
20
	 */
21
	protected $_data = array(
22
		'order_id'           => 0,
23
		'id'                 => 0,
24
		'rate_code'          => '',
25
		'rate_id'            => 0,
26
		'label'              => '',
27
		'compound'           => false,
28
		'tax_total'          => 0,
29
		'shipping_tax_total' => 0
30
	);
31
32
	/**
33
	 * Read/populate data properties specific to this order item.
34
	 */
35
	public function read( $id ) {
36
		parent::read( $id );
37
		if ( $this->get_id() ) {
38
			$this->set_rate_id( get_metadata( 'order_item', $this->get_id(), 'rate_id', true ) );
39
			$this->set_label( get_metadata( 'order_item', $this->get_id(), 'label', true ) );
40
			$this->set_compound( get_metadata( 'order_item', $this->get_id(), 'compound', true ) );
41
			$this->set_tax_total( get_metadata( 'order_item', $this->get_id(), 'tax_amount', true ) );
42
			$this->set_shipping_tax_total( get_metadata( 'order_item', $this->get_id(), 'shipping_tax_amount', true ) );
43
		}
44
	}
45
46
	/**
47
	 * Save properties specific to this order item.
48
	 * @return int Item ID
49
	 */
50
	public function save() {
51
		parent::save();
52
		if ( $this->get_id() ) {
53
			wc_update_order_item_meta( $this->get_id(), 'rate_id', $this->get_rate_id() );
54
			wc_update_order_item_meta( $this->get_id(), 'label', $this->get_label() );
55
			wc_update_order_item_meta( $this->get_id(), 'compound', $this->get_compound() );
56
			wc_update_order_item_meta( $this->get_id(), 'tax_amount', $this->get_tax_total() );
57
			wc_update_order_item_meta( $this->get_id(), 'shipping_tax_amount', $this->get_shipping_tax_total() );
58
		}
59
60
		return $this->get_id();
61
	}
62
63
	/**
64
	 * Internal meta keys we don't want exposed as part of meta_data.
65
	 * @return array()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
66
	 */
67
	protected function get_internal_meta_keys() {
68
		return array( 'rate_id', 'label', 'compound', 'tax_amount', 'shipping_tax_amount' );
69
	}
70
71
	/**
72
	 * Is this a compound tax rate?
73
	 * @return boolean
74
	 */
75
	public function is_compound() {
76
		return $this->get_compound();
77
	}
78
79
	/*
80
	|--------------------------------------------------------------------------
81
	| Setters
82
	|--------------------------------------------------------------------------
83
	*/
84
85
	/**
86
	 * Set order item name.
87
	 * @param string $value
88
	 */
89
	public function set_name( $value ) {
90
		$this->set_rate_code( $value );
91
	}
92
93
	/**
94
	 * Set item name.
95
	 * @param string $value
96
	 */
97
	public function set_rate_code( $value ) {
98
		$this->_data['rate_code'] = wc_clean( $value );
99
	}
100
101
	/**
102
	 * Set item name.
103
	 * @param string $value
104
	 */
105
	public function set_label( $value ) {
106
		$this->_data['label'] = wc_clean( $value );
107
	}
108
109
	/**
110
	 * Set tax rate id.
111
	 * @param int $value
112
	 */
113
	public function set_rate_id( $value ) {
114
		$this->_data['rate_id'] = absint( $value );
115
	}
116
117
	/**
118
	 * Set tax total.
119
	 * @param string $value
120
	 */
121
	public function set_tax_total( $value ) {
122
		$this->_data['tax_total'] = wc_format_decimal( $value );
123
	}
124
125
	/**
126
	 * Set shipping_tax_total
127
	 * @param string $value
128
	 */
129
	public function set_shipping_tax_total( $value ) {
130
		$this->_data['shipping_tax_total'] = wc_format_decimal( $value );
131
	}
132
133
	/**
134
	 * Set compound
135
	 * @param bool $value
136
	 */
137
	public function set_compound( $value ) {
138
		$this->_data['compound'] = (bool) $value;
139
	}
140
141
	/**
142
	 * Set properties based on passed in tax rate by ID.
143
	 * @param int $tax_rate_id
144
	 */
145
	public function set_rate( $tax_rate_id ) {
146
		$this->set_rate_id( $tax_rate_id );
147
		$this->set_rate_code( WC_Tax::get_rate_code( $tax_rate_id ) );
148
		$this->set_label( WC_Tax::get_rate_code( $tax_rate_id ) );
149
		$this->set_compound( WC_Tax::get_rate_code( $tax_rate_id ) );
0 ignored issues
show
Documentation introduced by
\WC_Tax::get_rate_code($tax_rate_id) is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
	}
151
152
	/*
153
	|--------------------------------------------------------------------------
154
	| Getters
155
	|--------------------------------------------------------------------------
156
	*/
157
158
	/**
159
	 * Get order item type.
160
	 * @return string
161
	 */
162
	public function get_type() {
163
		return 'tax';
164
	}
165
166
	/**
167
	 * Get rate code/name.
168
	 * @return string
169
	 */
170
	public function get_name() {
171
		return $this->get_rate_code();
172
	}
173
174
	/**
175
	 * Get rate code/name.
176
	 * @return string
177
	 */
178
	public function get_rate_code() {
179
		return $this->_data['rate_code'];
180
	}
181
182
	/**
183
	 * Get label.
184
	 * @return string
185
	 */
186
	public function get_label() {
187
		return $this->_data['label'] ? $this->_data['label'] : __( 'Tax', 'woocommerce' );
188
	}
189
190
	/**
191
	 * Get tax rate ID.
192
	 * @return int
193
	 */
194
	public function get_rate_id() {
195
		return absint( $this->_data['rate_id'] );
196
	}
197
198
	/**
199
	 * Get tax_total
200
	 * @return string
201
	 */
202
	public function get_tax_total() {
203
		return wc_format_decimal( $this->_data['tax_total'] );
204
	}
205
206
	/**
207
	 * Get shipping_tax_total
208
	 * @return string
209
	 */
210
	public function get_shipping_tax_total() {
211
		return wc_format_decimal( $this->_data['shipping_tax_total'] );
212
	}
213
214
	/**
215
	 * Get compound.
216
	 * @return bool
217
	 */
218
	public function get_compound() {
219
		return (bool) $this->_data['compound'];
220
	}
221
}
222