Last active
July 7, 2021 18:37
-
-
Save jorpdesigns/0aea2da5ca43c22d243ae535c4ced683 to your computer and use it in GitHub Desktop.
Snippet to customize variation dropdown option labels on WooCommerce product page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // THIS SNIPPET ADDS CUSTOM FIELD 'price_per_unit' TO THE OPTION LABEL. | |
| add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'custom_dropdown_args_label', 10, 2); | |
| function custom_dropdown_args_label( $html, $args ) { | |
| $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' ); | |
| $variations = $product->get_available_variations(); | |
| 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 ) ) { | |
| $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>'; | |
| } | |
| } | |
| } else { | |
| foreach ( $options as $option ) { | |
| foreach ($variations as $variation) { | |
| if ($variation['attributes'][$name] == $option) { | |
| $unit_price = $variation['price_per_unit']; // or replace with your custom field value | |
| } | |
| } | |
| // This handles < 2.4.0 bw compatibility where text attributes were not sanitized. | |
| $selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false ); | |
| $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $option . ' - Unit Price: ' . get_woocommerce_currency_symbol() . $unit_price ) . '</option>'; | |
| } | |
| } | |
| } | |
| $html .= '</select>'; | |
| return $html; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment