abstracts
2 years ago
admin
2 years ago
blocks
5 years ago
cli
3 years ago
customizer
3 years ago
data-stores
2 years ago
emails
2 years ago
export
2 years ago
gateways
2 years ago
import
2 years ago
integrations
4 years ago
interfaces
3 years ago
legacy
2 years ago
libraries
3 years ago
log-handlers
4 years ago
payment-tokens
5 years ago
queue
4 years ago
react-admin
2 years ago
rest-api
2 years ago
shipping
2 years ago
shortcodes
2 years ago
theme-support
3 years ago
tracks
2 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
2 years ago
widgets
3 years ago
class-wc-ajax.php
2 years ago
class-wc-api.php
4 years ago
class-wc-auth.php
4 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
3 years ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
2 years ago
class-wc-cart-totals.php
2 years ago
class-wc-cart.php
2 years ago
class-wc-checkout.php
2 years ago
class-wc-cli.php
3 years ago
class-wc-comments.php
3 years ago
class-wc-countries.php
2 years ago
class-wc-coupon.php
3 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
4 years ago
class-wc-customer.php
2 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
3 years ago
class-wc-discounts.php
3 years ago
class-wc-download-handler.php
3 years ago
class-wc-emails.php
3 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
2 years ago
class-wc-frontend-scripts.php
3 years ago
class-wc-geo-ip.php
4 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
3 years ago
class-wc-https.php
2 years ago
class-wc-install.php
2 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
4 years ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
3 years ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
4 years ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
4 years ago
class-wc-order-item-shipping.php
4 years ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
4 years ago
class-wc-order-query.php
4 years ago
class-wc-order-refund.php
2 years ago
class-wc-order.php
2 years ago
class-wc-payment-gateways.php
4 years ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
3 years ago
class-wc-post-types.php
3 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
4 years ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
2 years ago
class-wc-product-attribute.php
4 years ago
class-wc-product-download.php
4 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
3 years ago
class-wc-product-variable.php
3 years ago
class-wc-product-variation.php
4 years ago
class-wc-query.php
3 years ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
3 years ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
3 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
2 years ago
class-wc-shipping-rate.php
3 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
4 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
3 years ago
class-wc-tax.php
2 years ago
class-wc-template-loader.php
2 years ago
class-wc-tracker.php
2 years ago
class-wc-validation.php
3 years ago
class-wc-webhook.php
2 years ago
class-woocommerce.php
2 years ago
wc-account-functions.php
2 years ago
wc-attribute-functions.php
3 years ago
wc-cart-functions.php
2 years ago
wc-conditional-functions.php
2 years ago
wc-core-functions.php
2 years ago
wc-coupon-functions.php
3 years ago
wc-deprecated-functions.php
3 years ago
wc-formatting-functions.php
2 years ago
wc-notice-functions.php
3 years ago
wc-order-functions.php
2 years ago
wc-order-item-functions.php
3 years ago
wc-page-functions.php
2 years ago
wc-product-functions.php
3 years ago
wc-rest-functions.php
3 years ago
wc-stock-functions.php
3 years ago
wc-template-functions.php
2 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
3 years ago
wc-update-functions.php
2 years ago
wc-user-functions.php
2 years ago
wc-webhook-functions.php
4 years ago
wc-widget-functions.php
5 years ago
class-wc-order-item-fee.php
342 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Order Line Item (fee) |
| 4 | * |
| 5 | * Fee is an amount of money charged for a particular piece of work |
| 6 | * or for a particular right or service, and not supposed to be negative. |
| 7 | * |
| 8 | * @package WooCommerce\Classes |
| 9 | * @version 3.0.0 |
| 10 | * @since 3.0.0 |
| 11 | */ |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Order item fee. |
| 17 | */ |
| 18 | class WC_Order_Item_Fee extends WC_Order_Item { |
| 19 | |
| 20 | /** |
| 21 | * Order Data array. This is the core order data exposed in APIs since 3.0.0. |
| 22 | * |
| 23 | * @since 3.0.0 |
| 24 | * @var array |
| 25 | */ |
| 26 | protected $extra_data = array( |
| 27 | 'tax_class' => '', |
| 28 | 'tax_status' => 'taxable', |
| 29 | 'amount' => '', |
| 30 | 'total' => '', |
| 31 | 'total_tax' => '', |
| 32 | 'taxes' => array( |
| 33 | 'total' => array(), |
| 34 | ), |
| 35 | ); |
| 36 | |
| 37 | /** |
| 38 | * Get item costs grouped by tax class. |
| 39 | * |
| 40 | * @since 3.2.0 |
| 41 | * @param WC_Order $order Order object. |
| 42 | * @return array |
| 43 | */ |
| 44 | protected function get_tax_class_costs( $order ) { |
| 45 | $order_item_tax_classes = $order->get_items_tax_classes(); |
| 46 | $costs = array_fill_keys( $order_item_tax_classes, 0 ); |
| 47 | $costs['non-taxable'] = 0; |
| 48 | |
| 49 | foreach ( $order->get_items( array( 'line_item', 'fee', 'shipping' ) ) as $item ) { |
| 50 | if ( 0 > $item->get_total() ) { |
| 51 | continue; |
| 52 | } |
| 53 | if ( 'taxable' !== $item->get_tax_status() ) { |
| 54 | $costs['non-taxable'] += $item->get_total(); |
| 55 | } elseif ( 'inherit' === $item->get_tax_class() ) { |
| 56 | $inherit_class = reset( $order_item_tax_classes ); |
| 57 | $costs[ $inherit_class ] += $item->get_total(); |
| 58 | } else { |
| 59 | $costs[ $item->get_tax_class() ] += $item->get_total(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return array_filter( $costs ); |
| 64 | } |
| 65 | /** |
| 66 | * Calculate item taxes. |
| 67 | * |
| 68 | * @since 3.2.0 |
| 69 | * @param array $calculate_tax_for Location data to get taxes for. Required. |
| 70 | * @return bool True if taxes were calculated. |
| 71 | */ |
| 72 | public function calculate_taxes( $calculate_tax_for = array() ) { |
| 73 | if ( ! isset( $calculate_tax_for['country'], $calculate_tax_for['state'], $calculate_tax_for['postcode'], $calculate_tax_for['city'] ) ) { |
| 74 | return false; |
| 75 | } |
| 76 | // Use regular calculation unless the fee is negative. |
| 77 | if ( 0 <= $this->get_total() ) { |
| 78 | return parent::calculate_taxes( $calculate_tax_for ); |
| 79 | } |
| 80 | |
| 81 | if ( wc_tax_enabled() && $this->get_order() ) { |
| 82 | // Apportion taxes to order items, shipping, and fees. |
| 83 | $order = $this->get_order(); |
| 84 | $tax_class_costs = $this->get_tax_class_costs( $order ); |
| 85 | $total_costs = array_sum( $tax_class_costs ); |
| 86 | $discount_taxes = array(); |
| 87 | if ( $total_costs ) { |
| 88 | foreach ( $tax_class_costs as $tax_class => $tax_class_cost ) { |
| 89 | if ( 'non-taxable' === $tax_class ) { |
| 90 | continue; |
| 91 | } |
| 92 | $proportion = $tax_class_cost / $total_costs; |
| 93 | $cart_discount_proportion = $this->get_total() * $proportion; |
| 94 | $calculate_tax_for['tax_class'] = $tax_class; |
| 95 | $tax_rates = WC_Tax::find_rates( $calculate_tax_for ); |
| 96 | $discount_taxes = wc_array_merge_recursive_numeric( $discount_taxes, WC_Tax::calc_tax( $cart_discount_proportion, $tax_rates ) ); |
| 97 | } |
| 98 | } |
| 99 | $this->set_taxes( array( 'total' => $discount_taxes ) ); |
| 100 | } else { |
| 101 | $this->set_taxes( false ); |
| 102 | } |
| 103 | |
| 104 | do_action( 'woocommerce_order_item_fee_after_calculate_taxes', $this, $calculate_tax_for ); |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | |-------------------------------------------------------------------------- |
| 111 | | Setters |
| 112 | |-------------------------------------------------------------------------- |
| 113 | */ |
| 114 | |
| 115 | /** |
| 116 | * Set fee amount. |
| 117 | * |
| 118 | * @param string $value Amount. |
| 119 | */ |
| 120 | public function set_amount( $value ) { |
| 121 | $this->set_prop( 'amount', wc_format_decimal( $value ) ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Set tax class. |
| 126 | * |
| 127 | * @param string $value Tax class. |
| 128 | */ |
| 129 | public function set_tax_class( $value ) { |
| 130 | if ( $value && ! in_array( $value, WC_Tax::get_tax_class_slugs(), true ) ) { |
| 131 | $this->error( 'order_item_fee_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) ); |
| 132 | } |
| 133 | $this->set_prop( 'tax_class', $value ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Set tax_status. |
| 138 | * |
| 139 | * @param string $value Tax status. |
| 140 | */ |
| 141 | public function set_tax_status( $value ) { |
| 142 | if ( in_array( $value, array( 'taxable', 'none' ), true ) ) { |
| 143 | $this->set_prop( 'tax_status', $value ); |
| 144 | } else { |
| 145 | $this->set_prop( 'tax_status', 'taxable' ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Set total. |
| 151 | * |
| 152 | * @param string $amount Fee amount (do not enter negative amounts). |
| 153 | */ |
| 154 | public function set_total( $amount ) { |
| 155 | $this->set_prop( 'total', wc_format_decimal( $amount ) ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Set total tax. |
| 160 | * |
| 161 | * @param string $amount Amount. |
| 162 | */ |
| 163 | public function set_total_tax( $amount ) { |
| 164 | $this->set_prop( 'total_tax', wc_format_decimal( $amount ) ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Set taxes. |
| 169 | * |
| 170 | * This is an array of tax ID keys with total amount values. |
| 171 | * |
| 172 | * @param array $raw_tax_data Raw tax data. |
| 173 | */ |
| 174 | public function set_taxes( $raw_tax_data ) { |
| 175 | $raw_tax_data = maybe_unserialize( $raw_tax_data ); |
| 176 | $tax_data = array( |
| 177 | 'total' => array(), |
| 178 | ); |
| 179 | if ( ! empty( $raw_tax_data['total'] ) ) { |
| 180 | $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] ); |
| 181 | } |
| 182 | $this->set_prop( 'taxes', $tax_data ); |
| 183 | |
| 184 | if ( 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ) ) { |
| 185 | $this->set_total_tax( array_sum( $tax_data['total'] ) ); |
| 186 | } else { |
| 187 | $this->set_total_tax( array_sum( array_map( 'wc_round_tax_total', $tax_data['total'] ) ) ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | |-------------------------------------------------------------------------- |
| 193 | | Getters |
| 194 | |-------------------------------------------------------------------------- |
| 195 | */ |
| 196 | |
| 197 | /** |
| 198 | * Get fee amount. |
| 199 | * |
| 200 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 201 | * @return string |
| 202 | */ |
| 203 | public function get_amount( $context = 'view' ) { |
| 204 | return $this->get_prop( 'amount', $context ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get order item name. |
| 209 | * |
| 210 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 211 | * @return string |
| 212 | */ |
| 213 | public function get_name( $context = 'view' ) { |
| 214 | $name = $this->get_prop( 'name', $context ); |
| 215 | if ( 'view' === $context ) { |
| 216 | return $name ? $name : __( 'Fee', 'woocommerce' ); |
| 217 | } else { |
| 218 | return $name; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get order item type. |
| 224 | * |
| 225 | * @return string |
| 226 | */ |
| 227 | public function get_type() { |
| 228 | return 'fee'; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Get tax class. |
| 233 | * |
| 234 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 235 | * @return string |
| 236 | */ |
| 237 | public function get_tax_class( $context = 'view' ) { |
| 238 | return $this->get_prop( 'tax_class', $context ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Get tax status. |
| 243 | * |
| 244 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 245 | * @return string |
| 246 | */ |
| 247 | public function get_tax_status( $context = 'view' ) { |
| 248 | return $this->get_prop( 'tax_status', $context ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Get total fee. |
| 253 | * |
| 254 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 255 | * @return string |
| 256 | */ |
| 257 | public function get_total( $context = 'view' ) { |
| 258 | return $this->get_prop( 'total', $context ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Get total tax. |
| 263 | * |
| 264 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 265 | * @return string |
| 266 | */ |
| 267 | public function get_total_tax( $context = 'view' ) { |
| 268 | return $this->get_prop( 'total_tax', $context ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Get fee taxes. |
| 273 | * |
| 274 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 275 | * @return array |
| 276 | */ |
| 277 | public function get_taxes( $context = 'view' ) { |
| 278 | return $this->get_prop( 'taxes', $context ); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | |-------------------------------------------------------------------------- |
| 283 | | Array Access Methods |
| 284 | |-------------------------------------------------------------------------- |
| 285 | | |
| 286 | | For backwards compatibility with legacy arrays. |
| 287 | | |
| 288 | */ |
| 289 | |
| 290 | /** |
| 291 | * OffsetGet for ArrayAccess/Backwards compatibility. |
| 292 | * |
| 293 | * @param string $offset Offset. |
| 294 | * @return mixed |
| 295 | */ |
| 296 | #[\ReturnTypeWillChange] |
| 297 | public function offsetGet( $offset ) { |
| 298 | if ( 'line_total' === $offset ) { |
| 299 | $offset = 'total'; |
| 300 | } elseif ( 'line_tax' === $offset ) { |
| 301 | $offset = 'total_tax'; |
| 302 | } elseif ( 'line_tax_data' === $offset ) { |
| 303 | $offset = 'taxes'; |
| 304 | } |
| 305 | return parent::offsetGet( $offset ); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * OffsetSet for ArrayAccess/Backwards compatibility. |
| 310 | * |
| 311 | * @deprecated 4.4.0 |
| 312 | * @param string $offset Offset. |
| 313 | * @param mixed $value Value. |
| 314 | */ |
| 315 | #[\ReturnTypeWillChange] |
| 316 | public function offsetSet( $offset, $value ) { |
| 317 | wc_deprecated_function( 'WC_Order_Item_Fee::offsetSet', '4.4.0', '' ); |
| 318 | if ( 'line_total' === $offset ) { |
| 319 | $offset = 'total'; |
| 320 | } elseif ( 'line_tax' === $offset ) { |
| 321 | $offset = 'total_tax'; |
| 322 | } elseif ( 'line_tax_data' === $offset ) { |
| 323 | $offset = 'taxes'; |
| 324 | } |
| 325 | parent::offsetSet( $offset, $value ); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * OffsetExists for ArrayAccess |
| 330 | * |
| 331 | * @param string $offset Offset. |
| 332 | * @return bool |
| 333 | */ |
| 334 | #[\ReturnTypeWillChange] |
| 335 | public function offsetExists( $offset ) { |
| 336 | if ( in_array( $offset, array( 'line_total', 'line_tax', 'line_tax_data' ), true ) ) { |
| 337 | return true; |
| 338 | } |
| 339 | return parent::offsetExists( $offset ); |
| 340 | } |
| 341 | } |
| 342 |