Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_price_in_attribute_dropdown', 10, 2);
- function show_price_in_attribute_dropdown( $html, $args ) {
- // Only if there is a unique variation attribute (one dropdown)
- if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
- $options = $args['options'];
- $product = $args['product'];
- $attribute = $args['attribute'];
- $name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
- $id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
- $class = $args['class'];
- $show_option_none = $args['show_option_none'] ? true : false;
- $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
- if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
- $attributes = $product->get_variation_attributes();
- $options = $attributes[ $attribute ];
- }
- $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' ) . '">';
- $html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
- if ( ! empty( $options ) ) {
- if ( $product && taxonomy_exists( $attribute ) ) {
- $terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
- foreach ( $terms as $term ) {
- if ( in_array( $term->slug, $options ) ) {
- // Get and inserting the price
- $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>';
- }
- }
- }
- }
- $html .= '</select>';
- endif;
- return $html;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement