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