Completed
Pull Request — master (#10259)
by Mike
13:41
created

WC_Order_Item_Fee::get_total_tax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

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