abstracts
9 years ago
admin
9 years ago
api
9 years ago
cli
9 years ago
data-stores
9 years ago
emails
9 years ago
gateways
9 years ago
interfaces
9 years ago
legacy
9 years ago
libraries
9 years ago
log-handlers
9 years ago
payment-tokens
9 years ago
shipping
9 years ago
shortcodes
9 years ago
theme-support
9 years ago
vendor
9 years ago
walkers
9 years ago
widgets
9 years ago
class-wc-ajax.php
9 years ago
class-wc-api.php
9 years ago
class-wc-auth.php
9 years ago
class-wc-autoloader.php
9 years ago
class-wc-background-emailer.php
9 years ago
class-wc-background-updater.php
9 years ago
class-wc-breadcrumb.php
9 years ago
class-wc-cache-helper.php
9 years ago
class-wc-cart.php
9 years ago
class-wc-checkout.php
9 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
9 years ago
class-wc-countries.php
9 years ago
class-wc-coupon.php
9 years ago
class-wc-customer-download.php
9 years ago
class-wc-customer.php
9 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
9 years ago
class-wc-datetime.php
9 years ago
class-wc-deprecated-action-hooks.php
9 years ago
class-wc-deprecated-filter-hooks.php
9 years ago
class-wc-download-handler.php
9 years ago
class-wc-emails.php
9 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
9 years ago
class-wc-frontend-scripts.php
9 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
9 years ago
class-wc-https.php
9 years ago
class-wc-install.php
9 years ago
class-wc-integrations.php
9 years ago
class-wc-legacy-api.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
9 years ago
class-wc-order-factory.php
9 years ago
class-wc-order-item-coupon.php
9 years ago
class-wc-order-item-fee.php
9 years ago
class-wc-order-item-meta.php
9 years ago
class-wc-order-item-product.php
9 years ago
class-wc-order-item-shipping.php
9 years ago
class-wc-order-item-tax.php
9 years ago
class-wc-order-item.php
9 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
9 years ago
class-wc-payment-gateways.php
9 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
9 years ago
class-wc-post-types.php
9 years ago
class-wc-product-attribute.php
9 years ago
class-wc-product-download.php
9 years ago
class-wc-product-external.php
9 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
9 years ago
class-wc-product-simple.php
9 years ago
class-wc-product-variable.php
9 years ago
class-wc-product-variation.php
9 years ago
class-wc-query.php
9 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
9 years ago
class-wc-shipping-rate.php
9 years ago
class-wc-shipping-zone.php
9 years ago
class-wc-shipping-zones.php
9 years ago
class-wc-shipping.php
9 years ago
class-wc-shortcodes.php
9 years ago
class-wc-structured-data.php
9 years ago
class-wc-tax.php
9 years ago
class-wc-template-loader.php
9 years ago
class-wc-tracker.php
9 years ago
class-wc-validation.php
9 years ago
class-wc-webhook.php
9 years ago
wc-account-functions.php
9 years ago
wc-attribute-functions.php
9 years ago
wc-cart-functions.php
9 years ago
wc-conditional-functions.php
9 years ago
wc-core-functions.php
9 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
9 years ago
wc-formatting-functions.php
9 years ago
wc-notice-functions.php
9 years ago
wc-order-functions.php
9 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
9 years ago
wc-product-functions.php
9 years ago
wc-rest-functions.php
9 years ago
wc-stock-functions.php
9 years ago
wc-template-functions.php
9 years ago
wc-template-hooks.php
9 years ago
wc-term-functions.php
9 years ago
wc-update-functions.php
9 years ago
wc-user-functions.php
9 years ago
wc-webhook-functions.php
9 years ago
wc-widget-functions.php
9 years ago
class-wc-order-item-fee.php
241 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Order Line Item (fee). |
| 8 | * |
| 9 | * Fee is an amount of money charged for a particular piece of work |
| 10 | * or for a particular right or service, and not supposed to be negative. |
| 11 | * |
| 12 | * @version 3.0.0 |
| 13 | * @since 3.0.0 |
| 14 | * @package WooCommerce/Classes |
| 15 | * @author WooThemes |
| 16 | */ |
| 17 | class WC_Order_Item_Fee extends WC_Order_Item { |
| 18 | |
| 19 | /** |
| 20 | * Order Data array. This is the core order data exposed in APIs since 3.0.0. |
| 21 | * @since 3.0.0 |
| 22 | * @var array |
| 23 | */ |
| 24 | protected $extra_data = array( |
| 25 | 'tax_class' => '', |
| 26 | 'tax_status' => 'taxable', |
| 27 | 'total' => '', |
| 28 | 'total_tax' => '', |
| 29 | 'taxes' => array( |
| 30 | 'total' => array(), |
| 31 | ), |
| 32 | ); |
| 33 | |
| 34 | /* |
| 35 | |-------------------------------------------------------------------------- |
| 36 | | Setters |
| 37 | |-------------------------------------------------------------------------- |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Set tax class. |
| 42 | * |
| 43 | * @param string $value |
| 44 | * @throws WC_Data_Exception |
| 45 | */ |
| 46 | public function set_tax_class( $value ) { |
| 47 | if ( $value && ! in_array( $value, WC_Tax::get_tax_class_slugs() ) ) { |
| 48 | $this->error( 'order_item_fee_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) ); |
| 49 | } |
| 50 | $this->set_prop( 'tax_class', $value ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Set tax_status. |
| 55 | * |
| 56 | * @param string $value |
| 57 | * @throws WC_Data_Exception |
| 58 | */ |
| 59 | public function set_tax_status( $value ) { |
| 60 | if ( in_array( $value, array( 'taxable', 'none' ) ) ) { |
| 61 | $this->set_prop( 'tax_status', $value ); |
| 62 | } else { |
| 63 | $this->set_prop( 'tax_status', 'taxable' ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Set total. |
| 69 | * |
| 70 | * @param string $amount Fee amount (do not enter negative amounts). |
| 71 | * @throws WC_Data_Exception |
| 72 | */ |
| 73 | public function set_total( $amount ) { |
| 74 | $this->set_prop( 'total', wc_format_decimal( $amount ) ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Set total tax. |
| 79 | * |
| 80 | * @param string $amount |
| 81 | * @throws WC_Data_Exception |
| 82 | */ |
| 83 | public function set_total_tax( $amount ) { |
| 84 | $this->set_prop( 'total_tax', wc_format_decimal( $amount ) ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Set taxes. |
| 89 | * |
| 90 | * This is an array of tax ID keys with total amount values. |
| 91 | * @param array $raw_tax_data |
| 92 | * @throws WC_Data_Exception |
| 93 | */ |
| 94 | public function set_taxes( $raw_tax_data ) { |
| 95 | $raw_tax_data = maybe_unserialize( $raw_tax_data ); |
| 96 | $tax_data = array( |
| 97 | 'total' => array(), |
| 98 | ); |
| 99 | if ( ! empty( $raw_tax_data['total'] ) ) { |
| 100 | $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] ); |
| 101 | } |
| 102 | $this->set_prop( 'taxes', $tax_data ); |
| 103 | $this->set_total_tax( array_sum( $tax_data['total'] ) ); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | |-------------------------------------------------------------------------- |
| 108 | | Getters |
| 109 | |-------------------------------------------------------------------------- |
| 110 | */ |
| 111 | |
| 112 | /** |
| 113 | * Get order item name. |
| 114 | * |
| 115 | * @param string $context |
| 116 | * @return string |
| 117 | */ |
| 118 | public function get_name( $context = 'view' ) { |
| 119 | $name = $this->get_prop( 'name', $context ); |
| 120 | if ( 'view' === $context ) { |
| 121 | return $name ? $name : __( 'Fee', 'woocommerce' ); |
| 122 | } else { |
| 123 | return $name; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get order item type. |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | public function get_type() { |
| 133 | return 'fee'; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get tax class. |
| 138 | * |
| 139 | * @param string $context |
| 140 | * @return string |
| 141 | */ |
| 142 | public function get_tax_class( $context = 'view' ) { |
| 143 | return $this->get_prop( 'tax_class', $context ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get tax status. |
| 148 | * |
| 149 | * @param string $context |
| 150 | * @return string |
| 151 | */ |
| 152 | public function get_tax_status( $context = 'view' ) { |
| 153 | return $this->get_prop( 'tax_status', $context ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get total fee. |
| 158 | * |
| 159 | * @param string $context |
| 160 | * @return string |
| 161 | */ |
| 162 | public function get_total( $context = 'view' ) { |
| 163 | return $this->get_prop( 'total', $context ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get total tax. |
| 168 | * |
| 169 | * @param string $context |
| 170 | * @return string |
| 171 | */ |
| 172 | public function get_total_tax( $context = 'view' ) { |
| 173 | return $this->get_prop( 'total_tax', $context ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get fee taxes. |
| 178 | * |
| 179 | * @param string $context |
| 180 | * @return array |
| 181 | */ |
| 182 | public function get_taxes( $context = 'view' ) { |
| 183 | return $this->get_prop( 'taxes', $context ); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | |-------------------------------------------------------------------------- |
| 188 | | Array Access Methods |
| 189 | |-------------------------------------------------------------------------- |
| 190 | | |
| 191 | | For backwards compat with legacy arrays. |
| 192 | | |
| 193 | */ |
| 194 | |
| 195 | /** |
| 196 | * offsetGet for ArrayAccess/Backwards compatibility. |
| 197 | * @deprecated Add deprecation notices in future release. |
| 198 | * @param string $offset |
| 199 | * @return mixed |
| 200 | */ |
| 201 | public function offsetGet( $offset ) { |
| 202 | if ( 'line_total' === $offset ) { |
| 203 | $offset = 'total'; |
| 204 | } elseif ( 'line_tax' === $offset ) { |
| 205 | $offset = 'total_tax'; |
| 206 | } elseif ( 'line_tax_data' === $offset ) { |
| 207 | $offset = 'taxes'; |
| 208 | } |
| 209 | return parent::offsetGet( $offset ); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * offsetSet for ArrayAccess/Backwards compatibility. |
| 214 | * @deprecated Add deprecation notices in future release. |
| 215 | * @param string $offset |
| 216 | * @param mixed $value |
| 217 | */ |
| 218 | public function offsetSet( $offset, $value ) { |
| 219 | if ( 'line_total' === $offset ) { |
| 220 | $offset = 'total'; |
| 221 | } elseif ( 'line_tax' === $offset ) { |
| 222 | $offset = 'total_tax'; |
| 223 | } elseif ( 'line_tax_data' === $offset ) { |
| 224 | $offset = 'taxes'; |
| 225 | } |
| 226 | parent::offsetSet( $offset, $value ); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * offsetExists for ArrayAccess |
| 231 | * @param string $offset |
| 232 | * @return bool |
| 233 | */ |
| 234 | public function offsetExists( $offset ) { |
| 235 | if ( in_array( $offset, array( 'line_total', 'line_tax', 'line_tax_data' ) ) ) { |
| 236 | return true; |
| 237 | } |
| 238 | return parent::offsetExists( $offset ); |
| 239 | } |
| 240 | } |
| 241 |