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

WC_Order_Item_Shipping::get_method_id()   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 (shipping).
4
 *
5
 * @class 		WC_Order_Item_Shipping
6
 * @version		2.6.0
7
 * @since       2.6.0
8
 * @package		WooCommerce/Classes
9
 * @author 		WooThemes
10
 */
11
class WC_Order_Item_Shipping 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
        'method_id'     => '',
23
        'total'         => 0,
24
        'total_tax'     => 0,
25
        'taxes'         => array(
26
            'total' => array()
27
        )
28
    );
29
30
    /**
31
     * offsetGet for ArrayAccess/Backwards compatibility.
32
     * @todo Add deprecation notices in future release.
33
     * @param string $offset
34
     * @return mixed
35
     */
36
    public function offsetGet( $offset ) {
37
        if ( 'cost' === $offset ) {
38
            $offset = 'subtotal';
39
        }
40
        return parent::offsetGet( $offset );
41
    }
42
43
    /**
44
     * offsetExists for ArrayAccess
45
     * @param string $offset
46
     * @return bool
47
     */
48
    public function offsetExists( $offset ) {
49
        if ( in_array( $offset, array( 'cost' ) ) ) {
50
            return true;
51
        }
52
        return parent::offsetExists( $offset );
53
    }
54
55
    /**
56
     * Read/populate data properties specific to this order item.
57
     */
58
    public function read( $id ) {
59
        parent::read( $id );
60
        if ( $this->get_order_item_id() ) {
61
            $this->set_method_id( get_metadata( 'order_item', $this->get_order_item_id(), 'method_id', true ) );
62
            $this->set_total( get_metadata( 'order_item', $this->get_order_item_id(), 'cost', true ) );
63
            $this->set_total_tax( get_metadata( 'order_item', $this->get_order_item_id(), 'total_tax', true ) );
64
            $this->set_taxes( get_metadata( 'order_item', $this->get_order_item_id(), 'taxes', true ) );
65
        }
66
    }
67
68
    /**
69
     * Save properties specific to this order item.
70
     */
71
    public function save() {
72
        parent::save();
73
        if ( $this->get_order_item_id() ) {
74
            wc_update_order_item_meta( $this->get_order_item_id(), 'method_id', $this->get_method_id() );
75
            wc_update_order_item_meta( $this->get_order_item_id(), 'cost', $this->get_total() );
76
            wc_update_order_item_meta( $this->get_order_item_id(), 'total_tax', $this->get_total_tax() );
77
            wc_update_order_item_meta( $this->get_order_item_id(), 'taxes', $this->get_taxes() );
78
        }
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( 'method_id', 'cost', 'total_tax', 'taxes' );
87
    }
88
89
    /*
90
	|--------------------------------------------------------------------------
91
	| Setters
92
	|--------------------------------------------------------------------------
93
	*/
94
95
    /**
96
     * Set shipping method id.
97
     * @param string $value
98
     */
99
    public function set_method_id( $value ) {
100
        $this->_data['method_id'] = wc_clean( $value );
101
    }
102
103
    /**
104
     * Set total.
105
     * @param string $value
106
     */
107
    public function set_total( $value ) {
108
        $this->_data['total'] = wc_format_decimal( $value );
109
    }
110
111
    /**
112
     * Set total tax.
113
     * @param string $value
114
     */
115
    public function set_total_tax( $value ) {
116
        $this->_data['total_tax'] = wc_format_decimal( $value );
117
    }
118
119
    /**
120
     * Set taxes.
121
     *
122
     * This is an array of tax ID keys with total amount values.
123
     * @param array $raw_tax_data
124
     */
125 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...
126
        $raw_tax_data = maybe_unserialize( $raw_tax_data );
127
        $tax_data     = array(
128
            'total'    => array()
129
        );
130
        if ( ! empty( $raw_tax_data['total'] ) ) {
131
            $tax_data['total']    = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
132
        }
133
        $this->_data['taxes'] = $tax_data;
134
        $this->set_total_tax( array_sum( $tax_data['total'] ) );
135
    }
136
137
    /*
138
	|--------------------------------------------------------------------------
139
	| Getters
140
	|--------------------------------------------------------------------------
141
	*/
142
143
    /**
144
     * Get order item type.
145
     * @return string
146
     */
147
    public function get_type() {
148
        return 'shipping';
149
    }
150
151
    /**
152
     * Get method ID.
153
     * @return string
154
     */
155
    public function get_method_id() {
156
        return $this->meta_data['method_id'];
157
    }
158
159
    /**
160
     * Get total cost.
161
     * @return string
162
     */
163
    public function get_total() {
164
        return wc_format_decimal( $this->_data['total'] );
165
    }
166
167
    /**
168
     * Get total tax.
169
     * @return string
170
     */
171
    public function get_total_tax() {
172
        return wc_format_decimal( $this->_data['total_tax'] );
173
    }
174
175
    /**
176
     * Get taxes.
177
     * @return array
178
     */
179
    public function get_taxes() {
180
        return $this->_data['taxes'];
181
    }
182
}
183