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

WC_Order_Item_Shipping::set_shipping_rate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 7
rs 9.4285
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Order Line Item (shipping).
8
 *
9
 * @version     2.7.0
10
 * @since       2.7.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.7.0
19
	 * @var array
20
	 */
21
	protected $_data = array(
22
		'order_id'     => 0,
23
		'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
	 * offsetSet for ArrayAccess/Backwards compatibility.
48
	 * @deprecated Add deprecation notices in future release.
49
	 * @param string $offset
50
	 * @param mixed $value
51
	 */
52
	public function offsetSet( $offset, $value ) {
53
		if ( 'cost' === $offset ) {
54
			$offset = 'total';
55
		}
56
		parent::offsetSet( $offset, $value );
57
	}
58
59
	/**
60
	 * offsetExists for ArrayAccess
61
	 * @param string $offset
62
	 * @return bool
63
	 */
64
	public function offsetExists( $offset ) {
65
		if ( in_array( $offset, array( 'cost' ) ) ) {
66
			return true;
67
		}
68
		return parent::offsetExists( $offset );
69
	}
70
71
	/**
72
	 * Read/populate data properties specific to this order item.
73
	 */
74
	public function read( $id ) {
75
		parent::read( $id );
76
		if ( $this->get_id() ) {
77
			$this->set_method_id( get_metadata( 'order_item', $this->get_id(), 'method_id', true ) );
78
			$this->set_total( get_metadata( 'order_item', $this->get_id(), 'cost', true ) );
79
			$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), 'total_tax', true ) );
80
			$this->set_taxes( get_metadata( 'order_item', $this->get_id(), 'taxes', true ) );
81
		}
82
	}
83
84
	/**
85
	 * Save properties specific to this order item.
86
	 * @return int Item ID
87
	 */
88
	public function save() {
89
		parent::save();
90
		if ( $this->get_id() ) {
91
			wc_update_order_item_meta( $this->get_id(), 'method_id', $this->get_method_id() );
92
			wc_update_order_item_meta( $this->get_id(), 'cost', $this->get_total() );
93
			wc_update_order_item_meta( $this->get_id(), 'total_tax', $this->get_total_tax() );
94
			wc_update_order_item_meta( $this->get_id(), 'taxes', $this->get_taxes() );
95
		}
96
97
		return $this->get_id();
98
	}
99
100
	/**
101
	 * Internal meta keys we don't want exposed as part of meta_data.
102
	 * @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...
103
	 */
104
	protected function get_internal_meta_keys() {
105
		return array( 'method_id', 'cost', 'total_tax', 'taxes' );
106
	}
107
108
	/*
109
	|--------------------------------------------------------------------------
110
	| Setters
111
	|--------------------------------------------------------------------------
112
	*/
113
114
	/**
115
	 * Set order item name.
116
	 * @param string $value
117
	 */
118
	public function set_name( $value ) {
119
		$this->set_method_title( $value );
120
	}
121
122
	/**
123
	 * Set code.
124
	 * @param string $value
125
	 */
126
	public function set_method_title( $value ) {
127
		$this->_data['method_title'] = wc_clean( $value );
128
	}
129
130
	/**
131
	 * Set shipping method id.
132
	 * @param string $value
133
	 */
134
	public function set_method_id( $value ) {
135
		$this->_data['method_id'] = wc_clean( $value );
136
	}
137
138
	/**
139
	 * Set total.
140
	 * @param string $value
141
	 */
142
	public function set_total( $value ) {
143
		$this->_data['total'] = wc_format_decimal( $value );
144
	}
145
146
	/**
147
	 * Set total tax.
148
	 * @param string $value
149
	 */
150
	public function set_total_tax( $value ) {
151
		$this->_data['total_tax'] = wc_format_decimal( $value );
152
	}
153
154
	/**
155
	 * Set taxes.
156
	 *
157
	 * This is an array of tax ID keys with total amount values.
158
	 * @param array $raw_tax_data
159
	 */
160 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...
161
		$raw_tax_data = maybe_unserialize( $raw_tax_data );
162
		$tax_data     = array(
163
			'total'    => array()
164
		);
165
		if ( ! empty( $raw_tax_data['total'] ) ) {
166
			$tax_data['total']    = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
167
		}
168
		$this->_data['taxes'] = $tax_data;
169
		$this->set_total_tax( array_sum( $tax_data['total'] ) );
170
	}
171
172
	/**
173
	 * Set properties based on passed in shipping rate object.
174
	 * @param WC_Shipping_Rate $tax_rate_id
0 ignored issues
show
Bug introduced by
There is no parameter named $tax_rate_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
175
	 */
176
	public function set_shipping_rate( $shipping_rate ) {
177
		$this->set_method_title( $shipping_rate->label );
178
		$this->set_method_id( $shipping_rate->id );
179
		$this->set_total( $shipping_rate->cost );
180
		$this->set_taxes( $shipping_rate->taxes );
181
		$this->set_meta_data( $shipping_rate->get_meta_data() );
182
	}
183
184
	/*
185
	|--------------------------------------------------------------------------
186
	| Getters
187
	|--------------------------------------------------------------------------
188
	*/
189
190
	/**
191
	 * Get order item type.
192
	 * @return string
193
	 */
194
	public function get_type() {
195
		return 'shipping';
196
	}
197
198
	/**
199
	 * Get order item name.
200
	 * @return string
201
	 */
202
	public function get_name() {
203
		return $this->get_method_title();
204
	}
205
206
	/**
207
	 * Get title.
208
	 * @return string
209
	 */
210
	public function get_method_title() {
211
		return $this->_data['method_title'] ? $this->_data['method_title'] : __( 'Shipping', 'woocommerce' );
212
	}
213
214
	/**
215
	 * Get method ID.
216
	 * @return string
217
	 */
218
	public function get_method_id() {
219
		return $this->_data['method_id'];
220
	}
221
222
	/**
223
	 * Get total cost.
224
	 * @return string
225
	 */
226
	public function get_total() {
227
		return wc_format_decimal( $this->_data['total'] );
228
	}
229
230
	/**
231
	 * Get total tax.
232
	 * @return string
233
	 */
234
	public function get_total_tax() {
235
		return wc_format_decimal( $this->_data['total_tax'] );
236
	}
237
238
	/**
239
	 * Get taxes.
240
	 * @return array
241
	 */
242
	public function get_taxes() {
243
		return $this->_data['taxes'];
244
	}
245
}
246