wc_dropdown_variation_attribute_options

Output a list of variation attributes for use in the cart forms.

Description

wc_dropdown_variation_attribute_options( (array) $args = array() ); 

Parameters (1)

0. $args — Optional. (array) => array()
The args.

Usage

  1. if ( !function_exists( 'wc_dropdown_variation_attribute_options' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-template-functions.php'; 
  3.  
  4. // The args. 
  5. $args = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_dropdown_variation_attribute_options($args); 
  9.  

Defined (1)

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

/includes/wc-template-functions.php  
  1. function wc_dropdown_variation_attribute_options( $args = array() ) { 
  2. $args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array( 
  3. 'options' => false,  
  4. 'attribute' => false,  
  5. 'product' => false,  
  6. 'selected' => false,  
  7. 'name' => '',  
  8. 'id' => '',  
  9. 'class' => '',  
  10. 'show_option_none' => __( 'Choose an option', woocommerce ),  
  11. ) ); 
  12.  
  13. $options = $args['options']; 
  14. $product = $args['product']; 
  15. $attribute = $args['attribute']; 
  16. $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute ); 
  17. $id = $args['id'] ? $args['id'] : sanitize_title( $attribute ); 
  18. $class = $args['class']; 
  19. $show_option_none = $args['show_option_none'] ? true : false; 
  20. $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', woocommerce ); // We'll do our best to hide the placeholder, but we'll need to show something when resetting options. 
  21.  
  22. if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) { 
  23. $attributes = $product->get_variation_attributes(); 
  24. $options = $attributes[ $attribute ]; 
  25.  
  26. $html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">'; 
  27. $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>'; 
  28.  
  29. if ( ! empty( $options ) ) { 
  30. if ( $product && taxonomy_exists( $attribute ) ) { 
  31. // Get terms if this is a taxonomy - ordered. We need the names too. 
  32. $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) ); 
  33.  
  34. foreach ( $terms as $term ) { 
  35. if ( in_array( $term->slug, $options ) ) { 
  36. $html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ) . '</option>'; 
  37. } else { 
  38. foreach ( $options as $option ) { 
  39. // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. 
  40. $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false ); 
  41. $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>'; 
  42.  
  43. $html .= '</select>'; 
  44.  
  45. echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args );