abstracts
3 years ago
admin
3 years ago
blocks
5 years ago
cli
3 years ago
customizer
3 years ago
data-stores
3 years ago
emails
3 years ago
export
3 years ago
gateways
3 years ago
import
3 years ago
integrations
4 years ago
interfaces
3 years ago
legacy
3 years ago
libraries
3 years ago
log-handlers
4 years ago
payment-tokens
5 years ago
queue
4 years ago
react-admin
3 years ago
rest-api
3 years ago
shipping
4 years ago
shortcodes
3 years ago
theme-support
3 years ago
tracks
3 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
4 years ago
class-wc-ajax.php
3 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
4 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
3 years ago
class-wc-cart-totals.php
4 years ago
class-wc-cart.php
3 years ago
class-wc-checkout.php
3 years ago
class-wc-cli.php
3 years ago
class-wc-comments.php
3 years ago
class-wc-countries.php
3 years ago
class-wc-coupon.php
4 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
4 years ago
class-wc-customer.php
4 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
4 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
4 years ago
class-wc-install.php
3 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
3 years ago
class-wc-order.php
3 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
4 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
4 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
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 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
5 years ago
class-wc-tax.php
4 years ago
class-wc-template-loader.php
3 years ago
class-wc-tracker.php
3 years ago
class-wc-validation.php
3 years ago
class-wc-webhook.php
3 years ago
class-woocommerce.php
3 years ago
wc-account-functions.php
3 years ago
wc-attribute-functions.php
3 years ago
wc-cart-functions.php
3 years ago
wc-conditional-functions.php
3 years ago
wc-core-functions.php
3 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
3 years ago
wc-formatting-functions.php
3 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
3 years ago
wc-order-item-functions.php
3 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
3 years ago
wc-rest-functions.php
3 years ago
wc-stock-functions.php
4 years ago
wc-template-functions.php
3 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
3 years ago
wc-update-functions.php
3 years ago
wc-user-functions.php
3 years ago
wc-webhook-functions.php
4 years ago
wc-widget-functions.php
5 years ago
class-wc-order-item.php
425 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Order Item |
| 4 | * |
| 5 | * A class which represents an item within an order and handles CRUD. |
| 6 | * Uses ArrayAccess to be BW compatible with WC_Orders::get_items(). |
| 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 class. |
| 17 | */ |
| 18 | class WC_Order_Item extends WC_Data implements ArrayAccess { |
| 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 $data = array( |
| 27 | 'order_id' => 0, |
| 28 | 'name' => '', |
| 29 | ); |
| 30 | |
| 31 | /** |
| 32 | * Stores meta in cache for future reads. |
| 33 | * A group must be set to to enable caching. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $cache_group = 'order-items'; |
| 38 | |
| 39 | /** |
| 40 | * Meta type. This should match up with |
| 41 | * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
| 42 | * WP defines 'post', 'user', 'comment', and 'term'. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | protected $meta_type = 'order_item'; |
| 47 | |
| 48 | /** |
| 49 | * This is the name of this object type. |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | protected $object_type = 'order_item'; |
| 54 | |
| 55 | /** |
| 56 | * Constructor. |
| 57 | * |
| 58 | * @param int|object|array $item ID to load from the DB, or WC_Order_Item object. |
| 59 | */ |
| 60 | public function __construct( $item = 0 ) { |
| 61 | parent::__construct( $item ); |
| 62 | |
| 63 | if ( $item instanceof WC_Order_Item ) { |
| 64 | $this->set_id( $item->get_id() ); |
| 65 | } elseif ( is_numeric( $item ) && $item > 0 ) { |
| 66 | $this->set_id( $item ); |
| 67 | } else { |
| 68 | $this->set_object_read( true ); |
| 69 | } |
| 70 | |
| 71 | $type = 'line_item' === $this->get_type() ? 'product' : $this->get_type(); |
| 72 | $this->data_store = WC_Data_Store::load( 'order-item-' . $type ); |
| 73 | if ( $this->get_id() > 0 ) { |
| 74 | $this->data_store->read( $this ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Merge changes with data and clear. |
| 80 | * Overrides WC_Data::apply_changes. |
| 81 | * array_replace_recursive does not work well for order items because it merges taxes instead |
| 82 | * of replacing them. |
| 83 | * |
| 84 | * @since 3.2.0 |
| 85 | */ |
| 86 | public function apply_changes() { |
| 87 | if ( function_exists( 'array_replace' ) ) { |
| 88 | $this->data = array_replace( $this->data, $this->changes ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.array_replaceFound |
| 89 | } else { // PHP 5.2 compatibility. |
| 90 | foreach ( $this->changes as $key => $change ) { |
| 91 | $this->data[ $key ] = $change; |
| 92 | } |
| 93 | } |
| 94 | $this->changes = array(); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | |-------------------------------------------------------------------------- |
| 99 | | Getters |
| 100 | |-------------------------------------------------------------------------- |
| 101 | */ |
| 102 | |
| 103 | /** |
| 104 | * Get order ID this meta belongs to. |
| 105 | * |
| 106 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 107 | * @return int |
| 108 | */ |
| 109 | public function get_order_id( $context = 'view' ) { |
| 110 | return $this->get_prop( 'order_id', $context ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get order item name. |
| 115 | * |
| 116 | * @param string $context What the value is for. Valid values are 'view' and 'edit'. |
| 117 | * @return string |
| 118 | */ |
| 119 | public function get_name( $context = 'view' ) { |
| 120 | return $this->get_prop( 'name', $context ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get order item type. Overridden by child classes. |
| 125 | * |
| 126 | * @return string |
| 127 | */ |
| 128 | public function get_type() { |
| 129 | return ''; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get quantity. |
| 134 | * |
| 135 | * @return int |
| 136 | */ |
| 137 | public function get_quantity() { |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get tax status. |
| 143 | * |
| 144 | * @return string |
| 145 | */ |
| 146 | public function get_tax_status() { |
| 147 | return 'taxable'; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get tax class. |
| 152 | * |
| 153 | * @return string |
| 154 | */ |
| 155 | public function get_tax_class() { |
| 156 | return ''; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get parent order object. |
| 161 | * |
| 162 | * @return WC_Order |
| 163 | */ |
| 164 | public function get_order() { |
| 165 | return wc_get_order( $this->get_order_id() ); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | |-------------------------------------------------------------------------- |
| 170 | | Setters |
| 171 | |-------------------------------------------------------------------------- |
| 172 | */ |
| 173 | |
| 174 | /** |
| 175 | * Set order ID. |
| 176 | * |
| 177 | * @param int $value Order ID. |
| 178 | */ |
| 179 | public function set_order_id( $value ) { |
| 180 | $this->set_prop( 'order_id', absint( $value ) ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Set order item name. |
| 185 | * |
| 186 | * @param string $value Item name. |
| 187 | */ |
| 188 | public function set_name( $value ) { |
| 189 | $this->set_prop( 'name', wp_check_invalid_utf8( $value ) ); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | |-------------------------------------------------------------------------- |
| 194 | | Other Methods |
| 195 | |-------------------------------------------------------------------------- |
| 196 | */ |
| 197 | |
| 198 | /** |
| 199 | * Type checking. |
| 200 | * |
| 201 | * @param string|array $type Type. |
| 202 | * @return boolean |
| 203 | */ |
| 204 | public function is_type( $type ) { |
| 205 | return is_array( $type ) ? in_array( $this->get_type(), $type, true ) : $type === $this->get_type(); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Calculate item taxes. |
| 210 | * |
| 211 | * @since 3.2.0 |
| 212 | * @param array $calculate_tax_for Location data to get taxes for. Required. |
| 213 | * @return bool True if taxes were calculated. |
| 214 | */ |
| 215 | public function calculate_taxes( $calculate_tax_for = array() ) { |
| 216 | if ( ! isset( $calculate_tax_for['country'], $calculate_tax_for['state'], $calculate_tax_for['postcode'], $calculate_tax_for['city'] ) ) { |
| 217 | return false; |
| 218 | } |
| 219 | if ( '0' !== $this->get_tax_class() && 'taxable' === $this->get_tax_status() && wc_tax_enabled() ) { |
| 220 | $calculate_tax_for['tax_class'] = $this->get_tax_class(); |
| 221 | $tax_rates = WC_Tax::find_rates( $calculate_tax_for ); |
| 222 | $taxes = WC_Tax::calc_tax( $this->get_total(), $tax_rates, false ); |
| 223 | |
| 224 | if ( method_exists( $this, 'get_subtotal' ) ) { |
| 225 | $subtotal_taxes = WC_Tax::calc_tax( $this->get_subtotal(), $tax_rates, false ); |
| 226 | $this->set_taxes( |
| 227 | array( |
| 228 | 'total' => $taxes, |
| 229 | 'subtotal' => $subtotal_taxes, |
| 230 | ) |
| 231 | ); |
| 232 | } else { |
| 233 | $this->set_taxes( array( 'total' => $taxes ) ); |
| 234 | } |
| 235 | } else { |
| 236 | $this->set_taxes( false ); |
| 237 | } |
| 238 | |
| 239 | do_action( 'woocommerce_order_item_after_calculate_taxes', $this, $calculate_tax_for ); |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | |-------------------------------------------------------------------------- |
| 246 | | Meta Data Handling |
| 247 | |-------------------------------------------------------------------------- |
| 248 | */ |
| 249 | |
| 250 | /** |
| 251 | * Wrapper for get_formatted_meta_data that includes all metadata by default. See https://github.com/woocommerce/woocommerce/pull/30948 |
| 252 | * |
| 253 | * @param string $hideprefix Meta data prefix, (default: _). |
| 254 | * @param bool $include_all Include all meta data, this stop skip items with values already in the product name. |
| 255 | * @return array |
| 256 | */ |
| 257 | public function get_all_formatted_meta_data( $hideprefix = '_', $include_all = true ) { |
| 258 | return $this->get_formatted_meta_data( $hideprefix, $include_all ); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Expands things like term slugs before return. |
| 263 | * |
| 264 | * @param string $hideprefix Meta data prefix, (default: _). |
| 265 | * @param bool $include_all Include all meta data, this stop skip items with values already in the product name. |
| 266 | * @return array |
| 267 | */ |
| 268 | public function get_formatted_meta_data( $hideprefix = '_', $include_all = false ) { |
| 269 | $formatted_meta = array(); |
| 270 | $meta_data = $this->get_meta_data(); |
| 271 | $hideprefix_length = ! empty( $hideprefix ) ? strlen( $hideprefix ) : 0; |
| 272 | $product = is_callable( array( $this, 'get_product' ) ) ? $this->get_product() : false; |
| 273 | $order_item_name = $this->get_name(); |
| 274 | |
| 275 | foreach ( $meta_data as $meta ) { |
| 276 | if ( empty( $meta->id ) || '' === $meta->value || ! is_scalar( $meta->value ) || ( $hideprefix_length && substr( $meta->key, 0, $hideprefix_length ) === $hideprefix ) ) { |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | $meta->key = rawurldecode( (string) $meta->key ); |
| 281 | $meta->value = rawurldecode( (string) $meta->value ); |
| 282 | $attribute_key = str_replace( 'attribute_', '', $meta->key ); |
| 283 | $display_key = wc_attribute_label( $attribute_key, $product ); |
| 284 | $display_value = wp_kses_post( $meta->value ); |
| 285 | |
| 286 | if ( taxonomy_exists( $attribute_key ) ) { |
| 287 | $term = get_term_by( 'slug', $meta->value, $attribute_key ); |
| 288 | if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) { |
| 289 | $display_value = $term->name; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // Skip items with values already in the product details area of the product name. |
| 294 | if ( ! $include_all && $product && $product->is_type( 'variation' ) && wc_is_attribute_in_product_name( $display_value, $order_item_name ) ) { |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | $formatted_meta[ $meta->id ] = (object) array( |
| 299 | 'key' => $meta->key, |
| 300 | 'value' => $meta->value, |
| 301 | 'display_key' => apply_filters( 'woocommerce_order_item_display_meta_key', $display_key, $meta, $this ), |
| 302 | 'display_value' => wpautop( make_clickable( apply_filters( 'woocommerce_order_item_display_meta_value', $display_value, $meta, $this ) ) ), |
| 303 | ); |
| 304 | } |
| 305 | |
| 306 | return apply_filters( 'woocommerce_order_item_get_formatted_meta_data', $formatted_meta, $this ); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | |-------------------------------------------------------------------------- |
| 311 | | Array Access Methods |
| 312 | |-------------------------------------------------------------------------- |
| 313 | | |
| 314 | | For backwards compatibility with legacy arrays. |
| 315 | | |
| 316 | */ |
| 317 | |
| 318 | /** |
| 319 | * OffsetSet for ArrayAccess. |
| 320 | * |
| 321 | * @param string $offset Offset. |
| 322 | * @param mixed $value Value. |
| 323 | */ |
| 324 | #[\ReturnTypeWillChange] |
| 325 | public function offsetSet( $offset, $value ) { |
| 326 | if ( 'item_meta_array' === $offset ) { |
| 327 | foreach ( $value as $meta_id => $meta ) { |
| 328 | $this->update_meta_data( $meta->key, $meta->value, $meta_id ); |
| 329 | } |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | if ( array_key_exists( $offset, $this->data ) ) { |
| 334 | $setter = "set_$offset"; |
| 335 | if ( is_callable( array( $this, $setter ) ) ) { |
| 336 | $this->$setter( $value ); |
| 337 | } |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | $this->update_meta_data( $offset, $value ); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * OffsetUnset for ArrayAccess. |
| 346 | * |
| 347 | * @param string $offset Offset. |
| 348 | */ |
| 349 | #[\ReturnTypeWillChange] |
| 350 | public function offsetUnset( $offset ) { |
| 351 | $this->maybe_read_meta_data(); |
| 352 | |
| 353 | if ( 'item_meta_array' === $offset || 'item_meta' === $offset ) { |
| 354 | $this->meta_data = array(); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | if ( array_key_exists( $offset, $this->data ) ) { |
| 359 | unset( $this->data[ $offset ] ); |
| 360 | } |
| 361 | |
| 362 | if ( array_key_exists( $offset, $this->changes ) ) { |
| 363 | unset( $this->changes[ $offset ] ); |
| 364 | } |
| 365 | |
| 366 | $this->delete_meta_data( $offset ); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * OffsetExists for ArrayAccess. |
| 371 | * |
| 372 | * @param string $offset Offset. |
| 373 | * @return bool |
| 374 | */ |
| 375 | #[\ReturnTypeWillChange] |
| 376 | public function offsetExists( $offset ) { |
| 377 | $this->maybe_read_meta_data(); |
| 378 | if ( 'item_meta_array' === $offset || 'item_meta' === $offset || array_key_exists( $offset, $this->data ) ) { |
| 379 | return true; |
| 380 | } |
| 381 | return array_key_exists( $offset, wp_list_pluck( $this->meta_data, 'value', 'key' ) ) || array_key_exists( '_' . $offset, wp_list_pluck( $this->meta_data, 'value', 'key' ) ); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * OffsetGet for ArrayAccess. |
| 386 | * |
| 387 | * @param string $offset Offset. |
| 388 | * @return mixed |
| 389 | */ |
| 390 | #[\ReturnTypeWillChange] |
| 391 | public function offsetGet( $offset ) { |
| 392 | $this->maybe_read_meta_data(); |
| 393 | |
| 394 | if ( 'item_meta_array' === $offset ) { |
| 395 | $return = array(); |
| 396 | |
| 397 | foreach ( $this->meta_data as $meta ) { |
| 398 | $return[ $meta->id ] = $meta; |
| 399 | } |
| 400 | |
| 401 | return $return; |
| 402 | } |
| 403 | |
| 404 | $meta_values = wp_list_pluck( $this->meta_data, 'value', 'key' ); |
| 405 | |
| 406 | if ( 'item_meta' === $offset ) { |
| 407 | return $meta_values; |
| 408 | } elseif ( 'type' === $offset ) { |
| 409 | return $this->get_type(); |
| 410 | } elseif ( array_key_exists( $offset, $this->data ) ) { |
| 411 | $getter = "get_$offset"; |
| 412 | if ( is_callable( array( $this, $getter ) ) ) { |
| 413 | return $this->$getter(); |
| 414 | } |
| 415 | } elseif ( array_key_exists( '_' . $offset, $meta_values ) ) { |
| 416 | // Item meta was expanded in previous versions, with prefixes removed. This maintains support. |
| 417 | return $meta_values[ '_' . $offset ]; |
| 418 | } elseif ( array_key_exists( $offset, $meta_values ) ) { |
| 419 | return $meta_values[ $offset ]; |
| 420 | } |
| 421 | |
| 422 | return null; |
| 423 | } |
| 424 | } |
| 425 |