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