Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_text_for_select_based_on_attribute($attribute) {
- // Get an object representing the current product
- global $product;
- // Find the name of the attribute for the slug we passed in to the function
- $attribute_name = $product->get_attribute($attribute);
- // Create a string for our select
- $select_text = 'Select a ' . $attribute_name;
- // Send the $select_text variable back to our calling function
- return $select_text;
- }
- add_filter('variable_product_select_text', 'get_text_for_select_based_on_attribute', 10, 1);
- function wc_dropdown_variation_attribute_options($args = array()) {
- // Uses the same function as above, or optionally a custom filter
- $select_text = get_text_for_select_based_on_attribute($args['attribute']);
- $args = wp_parse_args( $args, array(
- 'options' => false,
- 'attribute' => false,
- 'product' => false,
- 'selected' => false,
- 'name' => '',
- 'id' => '',
- 'class' => '',
- 'show_option_none' => __( $select_text, 'woocommerce' ),
- 'show_option_color' => __( 'Choose a color', 'woocommerce' ),
- 'show_option_size' => __( 'Choose a size', 'woocommerce' )
- ) );
- $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'];
- if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
- $attributes = $product->get_variation_attributes();
- $options = $attributes[ $attribute ];
- }
- echo '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '">';
- if ( $args['show_option_none'] ) {
- echo '<option value="">' . esc_html( $args['show_option_none'] ) . '</option>';
- }
- if ( $args['$id_colors'] ) {
- echo '<option value="">' . esc_html( $args['show_option_color'] ) . '</option>';
- }
- if ( $args['$id_sizes'] ) {
- echo '<option value="">' . esc_html( $args['show_option_size'] ) . '</option>';
- }
- if ( ! empty( $options ) ) {
- if ( $product && taxonomy_exists( $attribute ) ) {
- // Get terms if this is a taxonomy - ordered. We need the names too.
- $terms = wc_get_product_terms( $product->id, $attribute, array( 'fields' => 'all' ) );
- foreach ( $terms as $term ) {
- if ( in_array( $term->slug, $options ) ) {
- echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
- }
- }
- } else {
- foreach ( $options as $option ) {
- // 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 );
- echo '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
- }
- }
- }
- echo '</select>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement