wc_attribute_label

Get a product attributes label.

Description

(string) wc_attribute_label( (string) $name, (string) $product = '' ); 

Returns (string)

Parameters (2)

0. $name (string)
The name.
1. $product — Optional. (string) => ''
Object Optional

Usage

  1. if ( !function_exists( 'wc_attribute_label' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-attribute-functions.php'; 
  3.  
  4. // The name. 
  5. $name = ''; 
  6.  
  7. // object Optional 
  8. $product = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_attribute_label($name, $product); 
  12.  

Defined (1)

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

/includes/wc-attribute-functions.php  
  1. function wc_attribute_label( $name, $product = '' ) { 
  2. global $wpdb; 
  3.  
  4. if ( taxonomy_is_product_attribute( $name ) ) { 
  5. $name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) ); 
  6. $all_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ); 
  7. $label = isset( $all_labels[ $name ] ) ? $all_labels[ $name ] : $name; 
  8. } elseif ( $product ) { 
  9. if ( $product->is_type( 'variation' ) ) { 
  10. $product = wc_get_product( $product->get_parent_id() ); 
  11. // Attempt to get label from product, as entered by the user. 
  12. if ( ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ] ) ) { 
  13. $label = $attributes[ sanitize_title( $name ) ]->get_name(); 
  14. } else { 
  15. $label = $name; 
  16. } else { 
  17. $label = $name; 
  18.  
  19. return apply_filters( 'woocommerce_attribute_label', $label, $name, $product );