wc_get_product_variation_attributes

Get attibutes/data for an individual variation from the database and maintain it's integrity.

Description

(array) wc_get_product_variation_attributes( (revisit?) $variation_id ); 

Returns (array)

Parameters (1)

0. $variation_id (revisit?)
The variation id.

Usage

  1. if ( !function_exists( 'wc_get_product_variation_attributes' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
  3.  
  4. // The variation id. 
  5. $variation_id = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_get_product_variation_attributes($variation_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-product-functions.php  
  1. function wc_get_product_variation_attributes( $variation_id ) { 
  2. // Build variation data from meta 
  3. $all_meta = get_post_meta( $variation_id ); 
  4. $parent_id = wp_get_post_parent_id( $variation_id ); 
  5. $parent_attributes = array_filter( (array) get_post_meta( $parent_id, '_product_attributes', true ) ); 
  6. $found_parent_attributes = array(); 
  7. $variation_attributes = array(); 
  8.  
  9. // Compare to parent variable product attributes and ensure they match 
  10. foreach ( $parent_attributes as $attribute_name => $options ) { 
  11. if ( ! empty( $options['is_variation'] ) ) { 
  12. $attribute = 'attribute_' . sanitize_title( $attribute_name ); 
  13. $found_parent_attributes[] = $attribute; 
  14. if ( ! array_key_exists( $attribute, $variation_attributes ) ) { 
  15. $variation_attributes[ $attribute ] = ''; // Add it - 'any' will be asumed 
  16.  
  17. // Get the variation attributes from meta 
  18. foreach ( $all_meta as $name => $value ) { 
  19. // Only look at valid attribute meta, and also compare variation level attributes and remove any which do not exist at parent level 
  20. if ( 0 !== strpos( $name, 'attribute_' ) || ! in_array( $name, $found_parent_attributes ) ) { 
  21. unset( $variation_attributes[ $name ] ); 
  22. continue; 
  23. /** 
  24. * Pre 2.4 handling where 'slugs' were saved instead of the full text attribute. 
  25. * Attempt to get full version of the text attribute from the parent. 
  26. */ 
  27. if ( sanitize_title( $value[0] ) === $value[0] && version_compare( get_post_meta( $parent_id, '_product_version', true ), '2.4.0', '<' ) ) { 
  28. foreach ( $parent_attributes as $attribute ) { 
  29. if ( 'attribute_' . sanitize_title( $attribute['name'] ) !== $name ) { 
  30. continue; 
  31. $text_attributes = wc_get_text_attributes( $attribute['value'] ); 
  32.  
  33. foreach ( $text_attributes as $text_attribute ) { 
  34. if ( sanitize_title( $text_attribute ) === $value[0] ) { 
  35. $value[0] = $text_attribute; 
  36. break; 
  37.  
  38. $variation_attributes[ $name ] = $value[0]; 
  39.  
  40. return $variation_attributes;