Completed
Push — master ( 22133a...2ef553 )
by Mike
22:40
created

WC_Product_Variation::__construct()   C

Complexity

Conditions 7
Paths 36

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
c 1
b 0
f 0
nc 36
nop 2
dl 0
loc 24
rs 6.7272
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Product Variation Class.
9
 *
10
 * The WooCommerce product variation class handles product variation data.
11
 *
12
 * @class       WC_Product_Variation
13
 * @version     2.2.0
14
 * @package     WooCommerce/Classes
15
 * @category    Class
16
 * @author      WooThemes
17
 */
18
class WC_Product_Variation extends WC_Product {
19
20
	/** @public int ID of the variation itself. */
21
	public $variation_id;
22
23
	/** @public object Parent Variable product object. */
24
	public $parent;
25
26
	/** @public string Stores the shipping class of the variation. */
27
	public $variation_shipping_class         = '';
28
29
	/** @public int Stores the shipping class ID of the variation. */
30
	public $variation_shipping_class_id      = 0;
31
32
	/** @public unused vars @deprecated in 2.2 */
33
	public $variation_has_sku                = true;
34
	public $variation_has_length             = true;
35
	public $variation_has_width              = true;
36
	public $variation_has_height             = true;
37
	public $variation_has_weight             = true;
38
	public $variation_has_tax_class          = true;
39
	public $variation_has_downloadable_files = true;
40
41
	/** @private array Data which is only at variation level - no inheritance plus their default values if left blank. */
42
	protected $variation_level_meta_data = array(
43
		'downloadable'          => 'no',
44
		'virtual'               => 'no',
45
		'manage_stock'          => 'no',
46
		'sale_price_dates_from' => '',
47
		'sale_price_dates_to'   => '',
48
		'price'                 => '',
49
		'regular_price'         => '',
50
		'sale_price'            => '',
51
		'stock'                 => 0,
52
		'stock_status'          => 'instock',
53
		'downloadable_files'    => array()
54
	);
55
56
	/** @private array Data which can be at variation level, otherwise fallback to parent if not set. */
57
	protected $variation_inherited_meta_data = array(
58
		'tax_class'  => '',
59
		'backorders' => 'no',
60
		'sku'        => '',
61
		'weight'     => '',
62
		'length'     => '',
63
		'width'      => '',
64
		'height'     => ''
65
	);
66
67
	/**
68
	 * Loads required variation data.
69
	 *
70
	 * @param int $variation ID of the variation to load
71
	 * @param array $args Array of the arguments containing parent product data
72
	 */
73
	public function __construct( $variation, $args = array() ) {
74
		if ( is_object( $variation ) ) {
75
			$this->variation_id = absint( $variation->ID );
76
		} else {
77
			$this->variation_id = absint( $variation );
78
		}
79
80
		/* Get main product data from parent (args) */
81
		$this->id = ! empty( $args['parent_id'] ) ? intval( $args['parent_id'] ) : wp_get_post_parent_id( $this->variation_id );
82
83
		// The post doesn't have a parent id, therefore its invalid and we should prevent this being created.
84
		if ( empty( $this->id ) ) {
85
			throw new Exception( sprintf( 'No parent product set for variation #%d', $this->variation_id ), 422 );
86
		}
87
88
		$this->product_type = 'variation';
89
		$this->parent       = ! empty( $args['parent'] ) ? $args['parent'] : wc_get_product( $this->id );
90
		$this->post         = ! empty( $this->parent->post ) ? $this->parent->post : array();
91
92
		// The post parent is not a valid variable product so we should prevent this being created.
93
		if ( ! is_a( $this->parent, 'WC_Product' ) ) {
94
			throw new Exception( sprintf( 'Invalid parent for variation #%d', $this->variation_id ), 422 );
95
		}
96
	}
97
98
	/**
99
	 * __isset function.
100
	 *
101
	 * @param mixed $key
102
	 * @return bool
103
	 */
104
	public function __isset( $key ) {
105
		if ( in_array( $key, array_keys( $this->variation_level_meta_data ) ) ) {
106
			return metadata_exists( 'post', $this->variation_id, '_' . $key );
107
		} elseif ( in_array( $key, array_keys( $this->variation_inherited_meta_data ) ) ) {
108
			return metadata_exists( 'post', $this->variation_id, '_' . $key ) || metadata_exists( 'post', $this->id, '_' . $key );
109
		} else {
110
			return metadata_exists( 'post', $this->id, '_' . $key );
111
		}
112
	}
113
114
	/**
115
	 * Get method returns variation meta data if set, otherwise in most cases the data from the parent.
116
	 *
117
	 * @param string $key
118
	 * @return mixed
119
	 */
120
	public function __get( $key ) {
121
		if ( in_array( $key, array_keys( $this->variation_level_meta_data ) ) ) {
122
123
			$value = get_post_meta( $this->variation_id, '_' . $key, true );
124
125
			if ( '' === $value ) {
126
				$value = $this->variation_level_meta_data[ $key ];
127
			}
128
129
		} elseif ( in_array( $key, array_keys( $this->variation_inherited_meta_data ) ) ) {
130
131
			$value = metadata_exists( 'post', $this->variation_id, '_' . $key ) ? get_post_meta( $this->variation_id, '_' . $key, true ) : get_post_meta( $this->id, '_' . $key, true );
132
133
			// Handle meta data keys which can be empty at variation level to cause inheritance
134
			if ( '' === $value && in_array( $key, array( 'sku', 'weight', 'length', 'width', 'height' ) ) ) {
135
				$value = get_post_meta( $this->id, '_' . $key, true );
136
			}
137
138
			if ( '' === $value ) {
139
				$value = $this->variation_inherited_meta_data[ $key ];
140
			}
141
142
		} elseif ( 'variation_data' === $key ) {
143
			return $this->variation_data = wc_get_product_variation_attributes( $this->variation_id );
144
145
		} elseif ( 'variation_has_stock' === $key ) {
146
			return $this->managing_stock();
147
148
		} else {
149
			$value = metadata_exists( 'post', $this->variation_id, '_' . $key ) ? get_post_meta( $this->variation_id, '_' . $key, true ) : parent::__get( $key );
150
		}
151
152
		return $value;
153
	}
154
155
	/**
156
	 * Return the variation ID
157
	 *
158
	 * @since 2.5.0
159
	 * @return int variation (post) ID
160
	 */
161
	public function get_id() {
162
		return $this->variation_id;
163
	}
164
165
	/**
166
	 * Returns whether or not the product post exists.
167
	 *
168
	 * @return bool
169
	 */
170
	public function exists() {
171
		return ! empty( $this->id );
172
	}
173
174
	/**
175
	 * Wrapper for get_permalink. Adds this variations attributes to the URL.
176
	 *
177
	 * @param  $item_object item array If a cart or order item is passed, we can get a link containing the exact attributes selected for the variation, rather than the default attributes.
178
	 * @return string
179
	 */
180
	public function get_permalink( $item_object = null ) {
181
		if ( ! empty( $item_object['variation'] ) ) {
182
			$data = $item_object['variation'];
183
		} elseif ( ! empty( $item_object['item_meta_array'] ) ) {
184
			$data_keys    = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) );
185
			$data_values  = wp_list_pluck( $item_object['item_meta_array'], 'value' );
186
			$data         = array_intersect_key( array_combine( $data_keys, $data_values ), $this->variation_data );
187
		} else {
188
			$data = $this->variation_data;
189
		}
190
		return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), get_permalink( $this->id ) );
191
	}
192
193
	/**
194
	 * Get the add to url used mainly in loops.
195
	 *
196
	 * @return string
197
	 */
198
	public function add_to_cart_url() {
199
		$variation_data = array_map( 'urlencode', $this->variation_data );
200
		$url            = $this->is_purchasable() && $this->is_in_stock() ? remove_query_arg( 'added-to-cart', add_query_arg( array_merge( array( 'variation_id' => $this->variation_id, 'add-to-cart' => $this->id ), $variation_data ) ) ) : get_permalink( $this->id );
201
202
		return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
203
	}
204
205
	/**
206
	 * Get the add to cart button text.
207
	 *
208
	 * @return string
209
	 */
210
	public function add_to_cart_text() {
211
		$text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Add to cart', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
212
213
		return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this );
214
	}
215
216
	/**
217
	 * Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed.
218
	 * Instead, a suitable 'unavailable' message is displayed.
219
	 * Invisible by default: Disabled variations and variations with an empty price.
220
	 *
221
	 * @return bool
222
	 */
223
	public function variation_is_visible() {
224
		$visible = true;
225
226
		// Published == enabled checkbox
227
		if ( get_post_status( $this->variation_id ) != 'publish' ) {
228
			$visible = false;
229
		}
230
231
		// Price not set
232
		elseif ( $this->get_price() === "" ) {
233
			$visible = false;
234
		}
235
236
		return apply_filters( 'woocommerce_variation_is_visible', $visible, $this->variation_id, $this->id, $this );
237
	}
238
239
	/**
240
	 * Controls whether this particular variation will appear greyed-out (inactive) or not (active).
241
	 * Used by extensions to make incompatible variations appear greyed-out, etc.
242
	 * Other possible uses: prevent out-of-stock variations from being selected.
243
	 *
244
	 * @return bool
245
	 */
246
	public function variation_is_active() {
247
		return apply_filters( 'woocommerce_variation_is_active', true, $this );
248
	}
249
250
	/**
251
	 * Returns false if the product cannot be bought.
252
	 * Override abstract method so that: i) Disabled variations are not be purchasable by admins. ii) Enabled variations are not purchasable if the parent product is not purchasable.
253
	 *
254
	 * @return bool
255
	 */
256
	public function is_purchasable() {
257
		// Published == enabled checkbox
258
		if ( get_post_status( $this->variation_id ) != 'publish' ) {
259
			$purchasable = false;
260
		} else {
261
			$purchasable = parent::is_purchasable();
262
		}
263
		return apply_filters( 'woocommerce_variation_is_purchasable', $purchasable, $this );
264
	}
265
266
	/**
267
	 * Returns whether or not the variations parent is visible.
268
	 *
269
	 * @return bool
270
	 */
271
	public function parent_is_visible() {
272
		return $this->is_visible();
273
	}
274
275
	/**
276
	 * Get variation ID.
277
	 *
278
	 * @return int
279
	 */
280
	public function get_variation_id() {
281
		return absint( $this->variation_id );
282
	}
283
284
	/**
285
	 * Get variation attribute values.
286
	 *
287
	 * @return array of attributes and their values for this variation
288
	 */
289
	public function get_variation_attributes() {
290
		return $this->variation_data;
291
	}
292
293
	/**
294
	 * Check if all variation's attributes are set.
295
	 *
296
	 * @return boolean
297
	 */
298
	public function has_all_attributes_set() {
299
300
		$set = true;
301
302
		// undefined attributes have null strings as array values
303
		foreach( $this->get_variation_attributes() as $att ){
304
			if( ! $att ){
305
				$set = false;
306
				break;
307
			}
308
		}
309
310
		return $set;
311
312
	}
313
314
	/**
315
	 * Get variation price HTML. Prices are not inherited from parents.
316
	 *
317
	 * @return string containing the formatted price
318
	 */
319
	public function get_price_html( $price = '' ) {
320
321
		$display_price         = $this->get_display_price();
322
		$display_regular_price = $this->get_display_price( $this->get_regular_price() );
323
		$display_sale_price    = $this->get_display_price( $this->get_sale_price() );
324
325
		if ( $this->get_price() !== '' ) {
326
			if ( $this->is_on_sale() ) {
327
				$price = apply_filters( 'woocommerce_variation_sale_price_html', '<del>' . wc_price( $display_regular_price ) . '</del> <ins>' . wc_price( $display_sale_price ) . '</ins>' . $this->get_price_suffix(), $this );
328
			} elseif ( $this->get_price() > 0 ) {
329
				$price = apply_filters( 'woocommerce_variation_price_html', wc_price( $display_price ) . $this->get_price_suffix(), $this );
330
			} else {
331
				$price = apply_filters( 'woocommerce_variation_free_price_html', __( 'Free!', 'woocommerce' ), $this );
332
			}
333
		} else {
334
			$price = apply_filters( 'woocommerce_variation_empty_price_html', '', $this );
335
		}
336
337
		return apply_filters( 'woocommerce_get_variation_price_html', $price, $this );
338
	}
339
340
	/**
341
	 * Gets the main product image ID.
342
	 *
343
	 * @return int
344
	 */
345
	public function get_image_id() {
346
		if ( $this->variation_id && has_post_thumbnail( $this->variation_id ) ) {
347
			$image_id = get_post_thumbnail_id( $this->variation_id );
348
		} elseif ( has_post_thumbnail( $this->id ) ) {
349
			$image_id = get_post_thumbnail_id( $this->id );
350 View Code Duplication
		} elseif ( ( $parent_id = wp_get_post_parent_id( $this->id ) ) && has_post_thumbnail( $parent_id ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
351
			$image_id = get_post_thumbnail_id( $parent_id );
352
		} else {
353
			$image_id = 0;
354
		}
355
		return $image_id;
356
	}
357
358
	/**
359
	 * Gets the main product image.
360
	 *
361
	 * @param string $size (default: 'shop_thumbnail')
362
	 * @param bool True to return $placeholder if no image is found, or false to return an empty string.
363
	 * @return string
364
	 */
365
	public function get_image( $size = 'shop_thumbnail', $attr = array(), $placeholder = true ) {
366
		if ( $this->variation_id && has_post_thumbnail( $this->variation_id ) ) {
367
			$image = get_the_post_thumbnail( $this->variation_id, $size, $attr );
368
		} elseif ( has_post_thumbnail( $this->id ) ) {
369
			$image = get_the_post_thumbnail( $this->id, $size, $attr );
370 View Code Duplication
		} elseif ( ( $parent_id = wp_get_post_parent_id( $this->id ) ) && has_post_thumbnail( $parent_id ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
371
			$image = get_the_post_thumbnail( $parent_id, $size , $attr);
372
		} elseif ( $placeholder ) {
373
			$image = wc_placeholder_img( $size );
374
		} else {
375
			$image = '';
376
		}
377
		return $image;
378
	}
379
380
	/**
381
	 * Returns whether or not the product (or variation) is stock managed.
382
	 *
383
	 * @return bool|string Bool if managed at variation level, 'parent' if managed by the parent.
384
	 */
385
	public function managing_stock() {
386
		if ( 'yes' === get_option( 'woocommerce_manage_stock', 'yes' ) ) {
387
			if ( 'no' === $this->manage_stock ) {
388
				if ( $this->parent && $this->parent->managing_stock() ) {
389
					return 'parent';
390
				}
391
			} else {
392
				return true;
393
			}
394
		}
395
		return false;
396
	}
397
398
	/**
399
	 * Returns number of items available for sale from the variation, or parent.
400
	 *
401
	 * @return int
402
	 */
403
	public function get_stock_quantity() {
404
		return apply_filters( 'woocommerce_variation_get_stock_quantity', true === $this->managing_stock() ? wc_stock_amount( $this->stock ) : $this->parent->get_stock_quantity(), $this );
405
	}
406
407
	/**
408
	 * Get total stock - This is the stock of parent and children combined.
409
	 *
410
	 * @return int
411
	 */
412
	public function get_total_stock() {
413
		return $this->get_stock_quantity();
414
	}
415
416
	/**
417
	 * Returns the tax status. Always use parent data.
418
	 *
419
	 * @return string
420
	 */
421
	public function get_tax_status() {
422
		return $this->parent->get_tax_status();
423
	}
424
425
	/**
426
	 * Returns whether or not the product is in stock.
427
	 *
428
	 * @return bool
429
	 */
430
	public function is_in_stock() {
431
		return apply_filters( 'woocommerce_variation_is_in_stock', $this->stock_status === 'instock', $this );
432
	}
433
434
	/**
435
	 * Set stock level of the product variation.
436
	 *
437
	 * Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues).
438
	 * We cannot rely on the original loaded value in case another order was made since then.
439
	 *
440
	 * @param int $amount
441
	 * @param string $mode can be set, add, or subtract
442
	 * @return int new stock level
443
	 */
444
	public function set_stock( $amount = null, $mode = 'set' ) {
445
		global $wpdb;
446
447
		if ( ! is_null( $amount ) && true === $this->managing_stock() ) {
448
449
			// Ensure key exists
450
			add_post_meta( $this->variation_id, '_stock', 0, true );
451
452
			// Update stock in DB directly
453 View Code Duplication
			switch ( $mode ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
454
				case 'add' :
455
					$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = meta_value + %f WHERE post_id = %d AND meta_key='_stock'", $amount, $this->variation_id ) );
456
				break;
457
				case 'subtract' :
458
					$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = meta_value - %f WHERE post_id = %d AND meta_key='_stock'", $amount, $this->variation_id ) );
459
				break;
460
				default :
461
					$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='_stock'", $amount, $this->variation_id ) );
462
				break;
463
			}
464
465
			// Clear caches
466
			wp_cache_delete( $this->variation_id, 'post_meta' );
467
468
			// Clear total stock transient
469
			delete_transient( 'wc_product_total_stock_' . $this->id . WC_Cache_Helper::get_transient_version( 'product' ) );
470
471
			// Stock status
472
			$this->check_stock_status();
473
474
			// Sync the parent
475
			WC_Product_Variable::sync( $this->id );
476
477
			// Trigger action
478
			do_action( 'woocommerce_variation_set_stock', $this );
479
480
		} elseif ( ! is_null( $amount ) ) {
481
			return $this->parent->set_stock( $amount, $mode );
482
		}
483
484
		return $this->get_stock_quantity();
485
	}
486
487
	/**
488
	 * Set stock status.
489
	 *
490
	 * @param string $status
491
	 */
492
	public function set_stock_status( $status ) {
493
		$status = 'outofstock' === $status ? 'outofstock' : 'instock';
494
495
		// Sanity check
496
		if ( true === $this->managing_stock() ) {
497
			if ( ! $this->backorders_allowed() && $this->get_stock_quantity() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
498
				$status = 'outofstock';
499
			}
500
		} elseif ( 'parent' === $this->managing_stock() ) {
501 View Code Duplication
			if ( ! $this->parent->backorders_allowed() && $this->parent->get_stock_quantity() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
502
				$status = 'outofstock';
503
			}
504
		}
505
506
		if ( update_post_meta( $this->variation_id, '_stock_status', $status ) ) {
507
			do_action( 'woocommerce_variation_set_stock_status', $this->variation_id, $status );
508
509
			WC_Product_Variable::sync_stock_status( $this->id );
510
		}
511
	}
512
513
	/**
514
	 * Reduce stock level of the product.
515
	 *
516
	 * @param int $amount (default: 1) Amount to reduce by
517
	 * @return int stock level
518
	 */
519
	public function reduce_stock( $amount = 1 ) {
520
		if ( true === $this->managing_stock() ) {
521
			return $this->set_stock( $amount, 'subtract' );
522
		} else {
523
			return $this->parent->reduce_stock( $amount );
524
		}
525
	}
526
527
	/**
528
	 * Increase stock level of the product.
529
	 *
530
	 * @param int $amount (default: 1) Amount to increase by
531
	 * @return int stock level
532
	 */
533
	public function increase_stock( $amount = 1 ) {
534
		if ( true === $this->managing_stock() ) {
535
			return $this->set_stock( $amount, 'add' );
536
		} else {
537
			return $this->parent->increase_stock( $amount );
538
		}
539
	}
540
541
	/**
542
	 * Returns whether or not the product needs to notify the customer on backorder.
543
	 *
544
	 * @return bool
545
	 */
546
	public function backorders_require_notification() {
547
		if ( true === $this->managing_stock() ) {
548
			return parent::backorders_require_notification();
549
		} else {
550
			return $this->parent->backorders_require_notification();
551
		}
552
	}
553
554
	/**
555
	 * Is on backorder?
556
	 *
557
	 * @param int $qty_in_cart (default: 0)
558
	 * @return bool
559
	 */
560
	public function is_on_backorder( $qty_in_cart = 0 ) {
561
		if ( true === $this->managing_stock() ) {
562
			return parent::is_on_backorder( $qty_in_cart );
563
		} else {
564
			return $this->parent->is_on_backorder( $qty_in_cart );
565
		}
566
	}
567
568
	/**
569
	 * Returns whether or not the product has enough stock for the order.
570
	 *
571
	 * @param mixed $quantity
572
	 * @return bool
573
	 */
574
	public function has_enough_stock( $quantity ) {
575
		if ( true === $this->managing_stock() ) {
576
			return parent::has_enough_stock( $quantity );
577
		} else {
578
			return $this->parent->has_enough_stock( $quantity );
579
		}
580
	}
581
582
	/**
583
	 * Get the shipping class, and if not set, get the shipping class of the parent.
584
	 *
585
	 * @return string
586
	 */
587 View Code Duplication
	public function get_shipping_class() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
588
		if ( ! $this->variation_shipping_class ) {
589
			$classes = get_the_terms( $this->variation_id, 'product_shipping_class' );
590
591
			if ( $classes && ! is_wp_error( $classes ) ) {
592
				$this->variation_shipping_class = current( $classes )->slug;
593
			} else {
594
				$this->variation_shipping_class = parent::get_shipping_class();
595
			}
596
		}
597
		return $this->variation_shipping_class;
598
	}
599
600
	/**
601
	 * Returns the product shipping class ID.
602
	 *
603
	 * @return int
604
	 */
605 View Code Duplication
	public function get_shipping_class_id() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
606
		if ( ! $this->variation_shipping_class_id ) {
607
			$classes = get_the_terms( $this->variation_id, 'product_shipping_class' );
608
609
			if ( $classes && ! is_wp_error( $classes ) ) {
610
				$this->variation_shipping_class_id = current( $classes )->term_id;
611
			} else {
612
				$this->variation_shipping_class_id = parent::get_shipping_class_id();
613
			}
614
		}
615
		return absint( $this->variation_shipping_class_id );
616
	}
617
618
	/**
619
	 * Get formatted variation data with WC < 2.4 back compat and proper formatting of text-based attribute names.
620
	 *
621
	 * @return string
622
	 */
623
	public function get_formatted_variation_attributes( $flat = false ) {
624
		$variation_data = $this->get_variation_attributes();
625
		$attributes     = $this->parent->get_attributes();
626
		$description    = array();
627
		$return         = '';
628
629
		if ( is_array( $variation_data ) ) {
630
631
			if ( ! $flat ) {
632
				$return = '<dl class="variation">';
633
			}
634
635
			foreach ( $attributes as $attribute ) {
636
637
				// Only deal with attributes that are variations
638
				if ( ! $attribute[ 'is_variation' ] ) {
639
					continue;
640
				}
641
642
				$variation_selected_value = isset( $variation_data[ 'attribute_' . sanitize_title( $attribute[ 'name' ] ) ] ) ? $variation_data[ 'attribute_' . sanitize_title( $attribute[ 'name' ] ) ] : '';
643
				$description_name         = esc_html( wc_attribute_label( $attribute[ 'name' ] ) );
644
				$description_value        = __( 'Any', 'woocommerce' );
645
646
				// Get terms for attribute taxonomy or value if its a custom attribute
647
				if ( $attribute[ 'is_taxonomy' ] ) {
648
649
					$post_terms = wp_get_post_terms( $this->id, $attribute[ 'name' ] );
650
651
					foreach ( $post_terms as $term ) {
652
						if ( $variation_selected_value === $term->slug ) {
653
							$description_value = esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) );
654
						}
655
					}
656
657
				} else {
658
659
					$options = wc_get_text_attributes( $attribute[ 'value' ] );
660
661
					foreach ( $options as $option ) {
662
663
						if ( sanitize_title( $variation_selected_value ) === $variation_selected_value ) {
664
							if ( $variation_selected_value !== sanitize_title( $option ) ) {
665
								continue;
666
							}
667
						} else {
668
							if ( $variation_selected_value !== $option ) {
669
								continue;
670
							}
671
						}
672
673
						$description_value = esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) );
674
					}
675
				}
676
677
				if ( $flat ) {
678
					$description[] = $description_name . ': ' . rawurldecode( $description_value );
679
				} else {
680
					$description[] = '<dt>' . $description_name . ':</dt><dd>' . rawurldecode( $description_value ) . '</dd>';
681
				}
682
			}
683
684
			if ( $flat ) {
685
				$return .= implode( ', ', $description );
686
			} else {
687
				$return .= implode( '', $description );
688
			}
689
690
			if ( ! $flat ) {
691
				$return .= '</dl>';
692
			}
693
		}
694
695
		return $return;
696
	}
697
698
	/**
699
	 * Get product name with extra details such as SKU, price and attributes. Used within admin.
700
	 *
701
	 * @return string Formatted product name, including attributes and price
702
	 */
703
	public function get_formatted_name() {
704
		if ( $this->get_sku() ) {
705
			$identifier = $this->get_sku();
706
		} else {
707
			$identifier = '#' . $this->variation_id;
708
		}
709
710
		$formatted_attributes = $this->get_formatted_variation_attributes( true );
711
		$extra_data           = ' &ndash; ' . $formatted_attributes . ' &ndash; ' . wc_price( $this->get_price() );
712
713
		return sprintf( __( '%s &ndash; %s%s', 'woocommerce' ), $identifier, $this->get_title(), $extra_data );
714
	}
715
716
	/**
717
	 * Get product variation description.
718
	 *
719
	 * @return string
720
	 */
721
	public function get_variation_description() {
722
		return wpautop( do_shortcode( wp_kses_post( get_post_meta( $this->variation_id, '_variation_description', true ) ) ) );
723
	}
724
}
725