Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_price_in_attribute_dropdown', 10, 2);
  2. function show_price_in_attribute_dropdown( $html, $args ) {
  3.     // Only if there is a unique variation attribute (one dropdown)
  4.     if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
  5.  
  6.     $options               = $args['options'];
  7.     $product               = $args['product'];
  8.     $attribute             = $args['attribute'];
  9.     $name                  = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
  10.     $id                    = $args['id'] ? $args['id'] : sanitize_title( $attribute );
  11.     $class                 = $args['class'];
  12.     $show_option_none      = $args['show_option_none'] ? true : false;
  13.     $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
  14.  
  15.     if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
  16.         $attributes = $product->get_variation_attributes();
  17.         $options    = $attributes[ $attribute ];
  18.     }
  19.  
  20.     $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' ) . '">';
  21.     $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
  22.            
  23.     if ( ! empty( $options ) ) {
  24.         if ( $product && taxonomy_exists( $attribute ) ) {
  25.             $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
  26.  
  27.             foreach ( $terms as $term ) {
  28.                 if ( in_array( $term->slug, $options ) ) {
  29.                     // Get and inserting the price
  30.                     $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 ) . ' - ' . $add_var ) . '</option>';
  31.                 }
  32.             }
  33.            
  34.         }
  35.     }
  36.     $html .= '</select>';
  37.  
  38.     endif;
  39.  
  40.     return $html;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement