Completed
Pull Request — master (#11208)
by Mike
09:06
created

WC_Order_Item_Fee::get_tax_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Order Line Item (fee).
8
 *
9
 * @version     2.7.0
10
 * @since       2.7.0
11
 * @package     WooCommerce/Classes
12
 * @author      WooThemes
13
 */
14
class WC_Order_Item_Fee 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
		'order_item_id' => 0,
24
		'name'          => '',
25
		'tax_class'     => '',
26
		'tax_status'    => 'taxable',
27
		'total'         => '',
28
		'total_tax'     => '',
29
		'taxes'         => array(
30
			'total' => array()
31
		)
32
	);
33
34
	/**
35
	 * offsetGet for ArrayAccess/Backwards compatibility.
36
	 * @deprecated Add deprecation notices in future release.
37
	 * @param string $offset
38
	 * @return mixed
39
	 */
40 View Code Duplication
	public function offsetGet( $offset ) {
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...
41
		if ( 'line_total' === $offset ) {
42
			$offset = 'total';
43
		} elseif ( 'line_tax' === $offset ) {
44
			$offset = 'total_tax';
45
		} elseif ( 'line_tax_data' === $offset ) {
46
			$offset = 'taxes';
47
		}
48
		return parent::offsetGet( $offset );
49
	}
50
51
	/**
52
	 * offsetSet for ArrayAccess/Backwards compatibility.
53
	 * @deprecated Add deprecation notices in future release.
54
	 * @param string $offset
55
	 * @param mixed $value
56
	 */
57 View Code Duplication
	public function offsetSet( $offset, $value ) {
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...
58
		if ( 'line_total' === $offset ) {
59
			$offset = 'total';
60
		} elseif ( 'line_tax' === $offset ) {
61
			$offset = 'total_tax';
62
		} elseif ( 'line_tax_data' === $offset ) {
63
			$offset = 'taxes';
64
		}
65
		parent::offsetSet( $offset, $value );
66
	}
67
68
	/**
69
	 * offsetExists for ArrayAccess
70
	 * @param string $offset
71
	 * @return bool
72
	 */
73
	public function offsetExists( $offset ) {
74
		if ( in_array( $offset, array( 'line_total', 'line_tax', 'line_tax_data' ) ) ) {
75
			return true;
76
		}
77
		return parent::offsetExists( $offset );
78
	}
79
80
	/**
81
	 * Read/populate data properties specific to this order item.
82
	 */
83
	public function read( $id ) {
84
		parent::read( $id );
85
		if ( $this->get_id() ) {
86
			$this->set_tax_class( get_metadata( 'order_item', $this->get_id(), '_tax_class', true ) );
87
			$this->set_tax_status( get_metadata( 'order_item', $this->get_id(), '_tax_status', true ) );
88
			$this->set_total( get_metadata( 'order_item', $this->get_id(), '_line_total', true ) );
89
			$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), '_line_tax', true ) );
90
			$this->set_taxes( get_metadata( 'order_item', $this->get_id(), '_line_tax_data', true ) );
91
		}
92
	}
93
94
	/**
95
	 * Save properties specific to this order item.
96
	 * @return int Item ID
97
	 */
98
	public function save() {
99
		parent::save();
100
		if ( $this->get_id() ) {
101
			wc_update_order_item_meta( $this->get_id(), '_tax_class', $this->get_tax_class() );
102
			wc_update_order_item_meta( $this->get_id(), '_tax_status', $this->get_tax_status() );
103
			wc_update_order_item_meta( $this->get_id(), '_line_total', $this->get_total() );
104
			wc_update_order_item_meta( $this->get_id(), '_line_tax', $this->get_total_tax() );
105
			wc_update_order_item_meta( $this->get_id(), '_line_tax_data', $this->get_taxes() );
106
		}
107
108
		return $this->get_id();
109
	}
110
111
	/*
112
	|--------------------------------------------------------------------------
113
	| Setters
114
	|--------------------------------------------------------------------------
115
	*/
116
117
	/**
118
	 * Set tax class.
119
	 * @param string $value
120
	 */
121
	public function set_tax_class( $value ) {
122
		$this->_data['tax_class'] = $value;
123
	}
124
125
	/**
126
	 * Set tax_status.
127
	 * @param string $value
128
	 */
129
	public function set_tax_status( $value ) {
130
		if ( in_array( $value, array( 'taxable', 'none' ) ) ) {
131
			$this->_data['tax_status'] = $value;
132
		} else {
133
			$this->_data['tax_status'] = 'taxable';
134
		}
135
	}
136
137
	/**
138
	 * Set total.
139
	 * @param string $value
140
	 */
141
	public function set_total( $value ) {
142
		$this->_data['total'] = wc_format_decimal( $value );
143
	}
144
145
	/**
146
	 * Set total tax.
147
	 * @param string $value
148
	 */
149
	public function set_total_tax( $value ) {
150
		$this->_data['total_tax'] = wc_format_decimal( $value );
151
	}
152
153
	/**
154
	 * Set taxes.
155
	 *
156
	 * This is an array of tax ID keys with total amount values.
157
	 * @param array $raw_tax_data
158
	 */
159 View Code Duplication
	public function set_taxes( $raw_tax_data ) {
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...
160
		$raw_tax_data = maybe_unserialize( $raw_tax_data );
161
		$tax_data     = array(
162
			'total' => array(),
163
		);
164
		if ( ! empty( $raw_tax_data['total'] ) ) {
165
			$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
166
		}
167
		$this->_data['taxes'] = $tax_data;
168
	}
169
170
	/*
171
	|--------------------------------------------------------------------------
172
	| Getters
173
	|--------------------------------------------------------------------------
174
	*/
175
176
	/**
177
	 * Get order item name.
178
	 * @return string
179
	 */
180
	public function get_name() {
181
		return $this->_data['name'] ? $this->_data['name'] : __( 'Fee', 'woocommerce' );
182
	}
183
184
	/**
185
	 * Get order item type.
186
	 * @return string
187
	 */
188
	public function get_type() {
189
		return 'fee';
190
	}
191
192
	/**
193
	 * Get tax class.
194
	 * @return string
195
	 */
196
	public function get_tax_class() {
197
		return $this->_data['tax_class'];
198
	}
199
200
	/**
201
	 * Get tax status.
202
	 * @return string
203
	 */
204
	public function get_tax_status() {
205
		return $this->_data['tax_status'];
206
	}
207
208
	/**
209
	 * Get total fee.
210
	 * @return string
211
	 */
212
	public function get_total() {
213
		return wc_format_decimal( $this->_data['total'] );
214
	}
215
216
	/**
217
	 * Get total tax.
218
	 * @return string
219
	 */
220
	public function get_total_tax() {
221
		return wc_format_decimal( $this->_data['total_tax'] );
222
	}
223
224
	/**
225
	 * Get fee taxes.
226
	 * @return array
227
	 */
228
	public function get_taxes() {
229
		return $this->_data['taxes'];
230
	}
231
}
232