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

WC_Order_Item_Coupon::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Order Line Item (coupon).
4
 *
5
 * @class 		WC_Order_Item_Coupon
6
 * @version		2.6.0
7
 * @since       2.6.0
8
 * @package		WooCommerce/Classes
9
 * @author 		WooThemes
10
 */
11
class WC_Order_Item_Coupon 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
        'code'          => '',
22
        'discount'      => 0,
23
        'discount_tax'  => 0,
24
    );
25
26
    /**
27
     * offsetGet for ArrayAccess/Backwards compatibility.
28
     * @todo Add deprecation notices in future release.
29
     * @param string $offset
30
     * @return mixed
31
     */
32
    public function offsetGet( $offset ) {
33
        if ( 'discount_amount' === $offset ) {
34
            $offset = 'discount';
35
        }
36
        elseif ( 'discount_amount_tax' === $offset ) {
37
            $offset = 'discount_tax';
38
        }
39
        return parent::offsetGet( $offset );
40
    }
41
42
    /**
43
     * offsetExists for ArrayAccess
44
     * @param string $offset
45
     * @return bool
46
     */
47
    public function offsetExists( $offset ) {
48
        if ( in_array( $offset, array( 'discount_amount', 'discount_amount_tax' ) ) ) {
49
            return true;
50
        }
51
        return parent::offsetExists( $offset );
52
    }
53
54
    /**
55
     * Read/populate data properties specific to this order item.
56
     */
57
    public function read( $id ) {
58
        parent::read( $id );
59
        if ( $this->get_order_item_id() ) {
60
            $this->set_discount( get_metadata( 'order_item', $this->get_order_item_id(), 'discount_amount', true ) );
61
            $this->set_discount_tax( get_metadata( 'order_item', $this->get_order_item_id(), 'discount_amount_tax', true ) );
62
        }
63
    }
64
65
    /**
66
     * Save properties specific to this order item.
67
     */
68
    public function save() {
69
        parent::save();
70
        if ( $this->get_order_item_id() ) {
71
            wc_update_order_item_meta( $this->get_order_item_id(), 'discount_amount', $this->get_discount() );
72
            wc_update_order_item_meta( $this->get_order_item_id(), 'discount_amount_tax', $this->get_discount_tax() );
73
        }
74
    }
75
76
    /**
77
     * Internal meta keys we don't want exposed as part of meta_data.
78
     * @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...
79
     */
80
    protected function get_internal_meta_keys() {
81
        return array( 'discount_amount', 'discount_amount_tax' );
82
    }
83
84
    /*
85
	|--------------------------------------------------------------------------
86
	| Setters
87
	|--------------------------------------------------------------------------
88
	*/
89
90
    /**
91
     * Set order item name.
92
     * @param string $value
93
     */
94
    public function set_name( $value ) {
95
        $this->_data['code'] = wc_clean( $value );
96
    }
97
98
    /**
99
     * Set discount amount.
100
     * @param string $value
101
     */
102
    public function set_discount( $value ) {
103
        $this->_data['discount'] =  wc_format_decimal( $value );
104
    }
105
106
    /**
107
     * Set discounted tax amount.
108
     * @param string $value
109
     */
110
    public function set_discount_tax( $value ) {
111
        $this->_data['discount_tax'] = wc_format_decimal( $value );
112
    }
113
114
    /*
115
	|--------------------------------------------------------------------------
116
	| Getters
117
	|--------------------------------------------------------------------------
118
	*/
119
120
    /**
121
     * Get order item type.
122
     * @return string
123
     */
124
    public function get_type() {
125
        return 'coupon';
126
    }
127
128
    /**
129
     * Get order item name.
130
     * @return string
131
     */
132
    public function get_name() {
133
        return $this->_data['code'];
134
    }
135
136
    /**
137
     * Get coupon code.
138
     * @return string
139
     */
140
    public function get_code() {
141
        return $this->get_order_item_name();
0 ignored issues
show
Bug introduced by
The method get_order_item_name() does not seem to exist on object<WC_Order_Item_Coupon>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
    }
143
144
    /**
145
     * Get discount amount.
146
     * @return string
147
     */
148
    public function get_discount() {
149
        return  wc_format_decimal( $this->_data['discount'] );
150
    }
151
152
    /**
153
     * Get discounted tax amount.
154
     * @return string
155
     */
156
    public function get_discount_tax() {
157
        return  wc_format_decimal( $this->_data['discount_tax'] );
158
    }
159
}
160