Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2015
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.47 KB | None | 0 0
  1. function get_text_for_select_based_on_attribute($attribute) {
  2. // Get an object representing the current product
  3.     global $product;
  4.  
  5. // Find the name of the attribute for the slug we passed in to the function
  6.     $attribute_name = $product->get_attribute($attribute);
  7.  
  8. // Create a string for our select
  9.     $select_text = 'Select a ' . $attribute_name;
  10.  
  11. // Send the $select_text variable back to our calling function
  12.     return $select_text;
  13. }
  14.  
  15.  
  16. add_filter('variable_product_select_text', 'get_text_for_select_based_on_attribute', 10, 1);
  17.  
  18.  
  19.  
  20. function wc_dropdown_variation_attribute_options($args = array()) {
  21.   // Uses the same function as above, or optionally a custom filter
  22.   $select_text = get_text_for_select_based_on_attribute($args['attribute']);
  23.    
  24.   $args = wp_parse_args( $args, array(
  25.       'options'          => false,
  26.       'attribute'        => false,
  27.       'product'          => false,
  28.       'selected'         => false,
  29.       'name'             => '',
  30.       'id'               => '',
  31.       'class'            => '',
  32.       'show_option_none' => __( $select_text, 'woocommerce' ),
  33.       'show_option_color' => __( 'Choose a color', 'woocommerce' ),
  34.       'show_option_size' => __( 'Choose a size', 'woocommerce' )
  35.     ) );
  36.  
  37.     $options   = $args['options'];
  38.     $product   = $args['product'];
  39.     $attribute = $args['attribute'];
  40.     $name      = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
  41.     $id        = $args['id'] ? $args['id'] : sanitize_title( $attribute );
  42.     $class     = $args['class'];
  43.  
  44.     if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
  45.         $attributes = $product->get_variation_attributes();
  46.         $options    = $attributes[ $attribute ];
  47.     }
  48.  
  49.     echo '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '">';
  50.  
  51.     if ( $args['show_option_none'] ) {
  52.         echo '<option value="">' . esc_html( $args['show_option_none'] ) . '</option>';
  53.     }
  54.     if ( $args['$id_colors'] ) {
  55.         echo '<option value="">' . esc_html( $args['show_option_color'] ) . '</option>';
  56.     }
  57.     if ( $args['$id_sizes'] ) {
  58.         echo '<option value="">' . esc_html( $args['show_option_size'] ) . '</option>';
  59.     }
  60.  
  61.     if ( ! empty( $options ) ) {
  62.         if ( $product && taxonomy_exists( $attribute ) ) {
  63.             // Get terms if this is a taxonomy - ordered. We need the names too.
  64.             $terms = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'all' ) );
  65.  
  66.             foreach ( $terms as $term ) {
  67.                 if ( in_array( $term->slug, $options ) ) {
  68.                     echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
  69.                 }
  70.             }
  71.         } else {
  72.             foreach ( $options as $option ) {
  73.                 // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
  74.                 $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
  75.                 echo '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
  76.             }
  77.         }
  78.     }
  79.  
  80.     echo '</select>';
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement