Change "Choose an option" with attribute name
-
Hi there,
first of all many compliments for your work, this plugin is amazing!I’ve just a little problem, I need to show the attribute name where now is showing “choose an option” as default in a variable product page. I need this because I’m using woocommerce for a eshop of contact lenses and is very important to indicate the title of each variation (you know!).
Can you help me?Thanks in advance
Fabrizio
– woocommerce 2.4.7 –
-
Hello,
I believe by default the attribute names do show as the title: https://cld.wthms.co/1kYXL/5gmaKyqo
Your theme may be changing this though. As you can see here, the default label is the attribute name: https://github.com/woothemes/woocommerce/blob/master/templates/single-product/add-to-cart/variable.php#L37
I believe fabrizioe is talking about the actual text inside the topmost option inside the select tag rather then the label for the dropdown.
This solution works well and is found on Stack Overflow.
Copy the file /plugins/woocommerce/templates/single-product/add-to-cart/variable.php to /themes/{your_theme_folder}/woocommerce/single-product/add-to-cart.php and then edit lines 27 – 28 from this (original):
<?php foreach ( $attributes as $attribute_name => $options ) : ?> <tr> <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td> <td class="value"> <?php $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) : $product->get_variation_default_attribute( $attribute_name ); wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) ); echo end( $attribute_keys ) === $attribute_name ? '<a class="reset_variations" href="#">' . __( 'Clear selection', 'woocommerce' ) . '</a>' : ''; ?> </td> </tr> <?php endforeach;?>
to these lines this here:
<?php $variations_arr = array(); foreach ( $attributes as $attribute_name => $options ) : ob_start(); ?> <tr> <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td> <td class="value"> <?php $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) : $product->get_variation_default_attribute( $attribute_name ); wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) ); echo end( $attribute_keys ) === $attribute_name ? '<a class="reset_variations" href="#">' . __( 'Clear selection', 'woocommerce' ) . '</a>' : ''; ?> </td> </tr> <?php $variations_ob = ob_get_clean(); $variations_arr[wc_attribute_label($attribute_name)] = $variations_ob; endforeach; foreach ($variations_arr as $name => $ob) { echo str_ireplace('choose an option', 'Choose '.$name, $ob ); } ?>
This is the safest option for your site as the other solutions could be overwritten through an update. Editing other peoples plugins is a bad which is probably the number 1 reason woocommerce allows us to override their templates from our themes rather then inside their plugin files.
Hi there,
Is it possible that this solution doesn’t work anymore in WooCommerce 2.5. I have the same files with the same content, and changing
<?php foreach ( $attributes as $attribute_name => $options ) : ?> <tr> <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td> <td class="value"> <?php $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) : $product->get_variation_default_attribute( $attribute_name ); wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) ); echo end( $attribute_keys ) === $attribute_name ? '<a class="reset_variations" href="#">' . __( 'Clear selection', 'woocommerce' ) . '</a>' : ''; ?> </td> </tr> <?php endforeach;?>
to what @mrosata posted 4 months ago doesn’t seem to work for me.
I’ve also tried to follow the StockOverFlow post with the same results.
Thanks for your help,
Romeo.It is still actually working. Make sure you are copying the template to the correct path
/themes/{your_theme_folder}/woocommerce/single-product/add-to-cart/The instruction above has a typo error saying to copy it inside /themes/{your_theme_folder}/woocommerce/single-product/add-to-cart.php
Hi there again,
I’ve double checked and the path was correct. I was /wp-content/themes/divi-child/woocommerce/single-product/add-to-cart/variable.php
My exact code is:
<table class="variations" cellspacing="0"> <tbody> <?php $variations_arr = array(); foreach ( $attributes as $attribute_name => $options ) : ob_start(); ?> <tr> <td class="label"><label for="<?php echo sanitize_title( $attribute_name ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td> <td class="value"> <?php $selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) : $product->get_variation_default_attribute( $attribute_name ); wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) ); echo end( $attribute_keys ) === $attribute_name ? '<a class="reset_variations" style="display: block;" href="#">' . __( 'Clear selection', 'woocommerce' ) . '</a>' : ''; ?> </td> </tr> <?php $variations_ob = ob_get_clean(); $variations_arr[wc_attribute_label($attribute_name)] = $variations_ob; endforeach; foreach ($variations_arr as $name => $ob) { echo str_ireplace('choose an option', 'Choose '.$name, $ob ); } ?> </tbody> </table>
As you can see here, https://www.coolmaison.com/producto/fillin-lamp/ is still saying “Escoge una opción” which means Choose an option in Spanish.
It is strange, because changes like
<a class="reset_variations" <strong>style="display: block;"</strong> href="#">' . __( 'Clear selection', 'woocommerce' ) . '</a>
do apply.Thanks for your time!
Thanks to @mrosata that pointed to the right direction.
In the same Stack Overflow question, there is an update.
@savan Dholu suggests to replace$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( urldecode( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ) : $product->get_variation_default_attribute( $attribute_name ); wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected ) ); echo end( $attribute_keys ) === $attribute_name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
With
wc_dropdown_variation_attribute_options( array( 'options' => $options, 'attribute' => $attribute_name, 'product' => $product, 'selected' => $selected, 'show_option_none'=> wc_attribute_label( $attribute_name ) ) );
It works like a charm!
Thanks guys
- The topic ‘Change "Choose an option" with attribute name’ is closed to new replies.