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

WC_Order_Item_Coupon::get_code()   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 (coupon).
8
 *
9
 * @version     2.6.0
10
 * @since       2.6.0
11
 * @package     WooCommerce/Classes
12
 * @author      WooThemes
13
 */
14
class WC_Order_Item_Coupon 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
		'code'          => '',
25
		'discount'      => 0,
26
		'discount_tax'  => 0,
27
	);
28
29
	/**
30
	 * offsetGet for ArrayAccess/Backwards compatibility.
31
	 * @deprecated Add deprecation notices in future release.
32
	 * @param string $offset
33
	 * @return mixed
34
	 */
35
	public function offsetGet( $offset ) {
36
		if ( 'discount_amount' === $offset ) {
37
			$offset = 'discount';
38
		} elseif ( 'discount_amount_tax' === $offset ) {
39
			$offset = 'discount_tax';
40
		}
41
		return parent::offsetGet( $offset );
42
	}
43
44
	/**
45
	 * offsetExists for ArrayAccess
46
	 * @param string $offset
47
	 * @return bool
48
	 */
49
	public function offsetExists( $offset ) {
50
		if ( in_array( $offset, array( 'discount_amount', 'discount_amount_tax' ) ) ) {
51
			return true;
52
		}
53
		return parent::offsetExists( $offset );
54
	}
55
56
	/**
57
	 * Read/populate data properties specific to this order item.
58
	 */
59
	public function read( $id ) {
60
		parent::read( $id );
61
		if ( $this->get_id() ) {
62
			$this->set_discount( get_metadata( 'order_item', $this->get_id(), 'discount_amount', true ) );
63
			$this->set_discount_tax( get_metadata( 'order_item', $this->get_id(), 'discount_amount_tax', true ) );
64
		}
65
	}
66
67
	/**
68
	 * Save properties specific to this order item.
69
	 * @return int Item ID
70
	 */
71
	public function save() {
72
		parent::save();
73
		if ( $this->get_id() ) {
74
			wc_update_order_item_meta( $this->get_id(), 'discount_amount', $this->get_discount() );
75
			wc_update_order_item_meta( $this->get_id(), 'discount_amount_tax', $this->get_discount_tax() );
76
		}
77
78
		return $this->get_id();
79
	}
80
81
	/**
82
	 * Internal meta keys we don't want exposed as part of meta_data.
83
	 * @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...
84
	 */
85
	protected function get_internal_meta_keys() {
86
		return array( 'discount_amount', 'discount_amount_tax' );
87
	}
88
89
	/*
90
	|--------------------------------------------------------------------------
91
	| Setters
92
	|--------------------------------------------------------------------------
93
	*/
94
95
	/**
96
	 * Set order item name.
97
	 * @param string $value
98
	 */
99
	public function set_name( $value ) {
100
		$this->set_code( $value );
101
	}
102
103
	/**
104
	 * Set code.
105
	 * @param string $value
106
	 */
107
	public function set_code( $value ) {
108
		$this->_data['code'] = wc_clean( $value );
109
	}
110
111
	/**
112
	 * Set discount amount.
113
	 * @param string $value
114
	 */
115
	public function set_discount( $value ) {
116
		$this->_data['discount'] =  wc_format_decimal( $value );
117
	}
118
119
	/**
120
	 * Set discounted tax amount.
121
	 * @param string $value
122
	 */
123
	public function set_discount_tax( $value ) {
124
		$this->_data['discount_tax'] = wc_format_decimal( $value );
125
	}
126
127
	/*
128
	|--------------------------------------------------------------------------
129
	| Getters
130
	|--------------------------------------------------------------------------
131
	*/
132
133
	/**
134
	 * Get order item type.
135
	 * @return string
136
	 */
137
	public function get_type() {
138
		return 'coupon';
139
	}
140
141
	/**
142
	 * Get order item name.
143
	 * @return string
144
	 */
145
	public function get_name() {
146
		return $this->get_code();
147
	}
148
149
	/**
150
	 * Get coupon code.
151
	 * @return string
152
	 */
153
	public function get_code() {
154
		return $this->_data['code'];
155
	}
156
157
	/**
158
	 * Get discount amount.
159
	 * @return string
160
	 */
161
	public function get_discount() {
162
		return wc_format_decimal( $this->_data['discount'] );
163
	}
164
165
	/**
166
	 * Get discounted tax amount.
167
	 * @return string
168
	 */
169
	public function get_discount_tax() {
170
		return wc_format_decimal( $this->_data['discount_tax'] );
171
	}
172
}
173