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