Completed
Pull Request — master (#10259)
by Mike
08:16
created

WC_Order_Item_Shipping::get_total()   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
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 14 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
 * Order Line Item (shipping).
8
 *
9
 * @version     2.6.0
10
 * @since       2.6.0
11
 * @package     WooCommerce/Classes
12
 * @author      WooThemes
13
 */
14
class WC_Order_Item_Shipping extends WC_Order_Item {
15
16
	/**
17
	 * Data properties of this order item object.
18
	 * @since 2.6.0
19
	 * @var array
20
	 */
21
	protected $_data = array(
22
		'order_id'      => 0,
23
		'order_item_id' => 0,
24
		'method_title'  => '',
25
		'method_id'     => '',
26
		'total'         => 0,
27
		'total_tax'     => 0,
28
		'taxes'         => array(
29
			'total' => array()
30
		),
31
	);
32
33
	/**
34
	 * offsetGet for ArrayAccess/Backwards compatibility.
35
	 * @deprecated Add deprecation notices in future release.
36
	 * @param string $offset
37
	 * @return mixed
38
	 */
39
	public function offsetGet( $offset ) {
40
		if ( 'cost' === $offset ) {
41
			$offset = 'total';
42
		}
43
		return parent::offsetGet( $offset );
44
	}
45
46
	/**
47
	 * offsetExists for ArrayAccess
48
	 * @param string $offset
49
	 * @return bool
50
	 */
51
	public function offsetExists( $offset ) {
52
		if ( in_array( $offset, array( 'cost' ) ) ) {
53
			return true;
54
		}
55
		return parent::offsetExists( $offset );
56
	}
57
58
	/**
59
	 * Read/populate data properties specific to this order item.
60
	 */
61
	public function read( $id ) {
62
		parent::read( $id );
63
		if ( $this->get_id() ) {
64
			$this->set_method_id( get_metadata( 'order_item', $this->get_id(), 'method_id', true ) );
65
			$this->set_total( get_metadata( 'order_item', $this->get_id(), 'cost', true ) );
66
			$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), 'total_tax', true ) );
67
			$this->set_taxes( get_metadata( 'order_item', $this->get_id(), 'taxes', true ) );
68
		}
69
	}
70
71
	/**
72
	 * Save properties specific to this order item.
73
	 * @return int Item ID
74
	 */
75
	public function save() {
76
		parent::save();
77
		if ( $this->get_id() ) {
78
			wc_update_order_item_meta( $this->get_id(), 'method_id', $this->get_method_id() );
79
			wc_update_order_item_meta( $this->get_id(), 'cost', $this->get_total() );
80
			wc_update_order_item_meta( $this->get_id(), 'total_tax', $this->get_total_tax() );
81
			wc_update_order_item_meta( $this->get_id(), 'taxes', $this->get_taxes() );
82
		}
83
84
		return $this->get_id();
85
	}
86
87
	/**
88
	 * Internal meta keys we don't want exposed as part of meta_data.
89
	 * @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...
90
	 */
91
	protected function get_internal_meta_keys() {
92
		return array( 'method_id', 'cost', 'total_tax', 'taxes' );
93
	}
94
95
	/*
96
	|--------------------------------------------------------------------------
97
	| Setters
98
	|--------------------------------------------------------------------------
99
	*/
100
101
	/**
102
	 * Set order item name.
103
	 * @param string $value
104
	 */
105
	public function set_name( $value ) {
106
		$this->set_method_title( $value );
107
	}
108
109
	/**
110
	 * Set code.
111
	 * @param string $value
112
	 */
113
	public function set_method_title( $value ) {
114
		$this->_data['method_title'] = wc_clean( $value );
115
	}
116
117
	/**
118
	 * Set shipping method id.
119
	 * @param string $value
120
	 */
121
	public function set_method_id( $value ) {
122
		$this->_data['method_id'] = wc_clean( $value );
123
	}
124
125
	/**
126
	 * Set total.
127
	 * @param string $value
128
	 */
129
	public function set_total( $value ) {
130
		$this->_data['total'] = wc_format_decimal( $value );
131
	}
132
133
	/**
134
	 * Set total tax.
135
	 * @param string $value
136
	 */
137
	public function set_total_tax( $value ) {
138
		$this->_data['total_tax'] = wc_format_decimal( $value );
139
	}
140
141
	/**
142
	 * Set taxes.
143
	 *
144
	 * This is an array of tax ID keys with total amount values.
145
	 * @param array $raw_tax_data
146
	 */
147 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...
148
		$raw_tax_data = maybe_unserialize( $raw_tax_data );
149
		$tax_data     = array(
150
			'total'    => array()
151
		);
152
		if ( ! empty( $raw_tax_data['total'] ) ) {
153
			$tax_data['total']    = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
154
		}
155
		$this->_data['taxes'] = $tax_data;
156
		$this->set_total_tax( array_sum( $tax_data['total'] ) );
157
	}
158
159
	/*
160
	|--------------------------------------------------------------------------
161
	| Getters
162
	|--------------------------------------------------------------------------
163
	*/
164
165
	/**
166
	 * Get order item type.
167
	 * @return string
168
	 */
169
	public function get_type() {
170
		return 'shipping';
171
	}
172
173
	/**
174
	 * Get order item name.
175
	 * @return string
176
	 */
177
	public function get_name() {
178
		return $this->get_method_title();
179
	}
180
181
	/**
182
	 * Get title.
183
	 * @return string
184
	 */
185
	public function get_method_title() {
186
		return $this->_data['method_title'] ? $this->_data['method_title'] : __( 'Shipping', 'woocommerce' );
187
	}
188
189
	/**
190
	 * Get method ID.
191
	 * @return string
192
	 */
193
	public function get_method_id() {
194
		return $this->_data['method_id'];
195
	}
196
197
	/**
198
	 * Get total cost.
199
	 * @return string
200
	 */
201
	public function get_total() {
202
		return wc_format_decimal( $this->_data['total'] );
203
	}
204
205
	/**
206
	 * Get total tax.
207
	 * @return string
208
	 */
209
	public function get_total_tax() {
210
		return wc_format_decimal( $this->_data['total_tax'] );
211
	}
212
213
	/**
214
	 * Get taxes.
215
	 * @return array
216
	 */
217
	public function get_taxes() {
218
		return $this->_data['taxes'];
219
	}
220
}
221