superboer810
superboer810

Reputation: 41

Change 'choose an option' text on product page

On the product page of my woo commerce site. I want to change the text in the drop down menu 'Choose an option' into another language. Is there a way to do that.

Upvotes: 3

Views: 16890

Answers (5)

    // Меняем текст "выбрать опцию" на странице вариативного товара, на название атрибута
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'my_wc_filter_dropdown_args', 10 );
function my_wc_filter_dropdown_args( $args ) {
    $variation_tax = get_taxonomy( $args['attribute'] );
    $args['show_option_none'] = str_replace('Товар ',  '', apply_filters( 'the_title', $variation_tax->labels->name )); // Вырезаем слово "Товар " из названия атрибута
    return $args;
}

Upvotes: 0

Calara Ionut
Calara Ionut

Reputation: 1

@sobeit answer may not work for you. That is due to the fact that not all attributes are stored as taxonomies. But you don't really need to get the attribute, since inside the args array you already have the label.

Here's the updated code

add_filter( 'woocommerce_dropdown_variation_attribute_options_args','my_wc_filter_dropdown_args') );

function wc_filter_dropdown_args( $args ) {
        global $product;
        $args['show_option_none'] = __( 'Choose', 'your-theme-name' ) . ' ' . strtolower(wc_attribute_label($args['attribute'],$product));

        return $args;
    }

P.S. I would have posted this as a comment but I don't have enough reputation to do that.

Upvotes: 0

Stef
Stef

Reputation: 369

Another way of doing this is to use the html hook: woocommerce_dropdown_variation_attribute_options_html

Start by hooking this filter in functions.php and adding a var_dump() so you know what the existing html is, as follows:

function filter_woocommerce_dropdown_variation_attribute_options_html( $html, $args ) { 

  var_dump($html);

  return $html; 
}; 


add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'filter_woocommerce_dropdown_variation_attribute_options_html', 10, 2 );

After that it's just a case of replacing the html as it stands with what you actually want:

function filter_woocommerce_dropdown_variation_attribute_options_html( $html, $args ) 
  { 
    $html = "//my replacement html with text in foreign language";
    return $html; 
}; 

add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'filter_woocommerce_dropdown_variation_attribute_options_html', 10, 2 );

That should do the trick.

Upvotes: 0

GusRuss89
GusRuss89

Reputation: 1404

AGMG's answer is no longer correct. To change the dropdown's default option you can now hook on to woocommerce_dropdown_variation_attribute_options_args

E.g.

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'my_wc_filter_dropdown_args', 10 );
function my_wc_filter_dropdown_args( $args ) {
    $args['show_option_none'] = 'Select an option';
    return $args;
}

My use case was to change it to the variation group name (E.g. 'Size' or 'Colour').

function my_wc_filter_dropdown_args( $args ) {
    $variation_tax = get_taxonomy( $args['attribute'] );
    $args['show_option_none'] = apply_filters( 'the_title', $variation_tax->labels->name );
    return $args;
}

Upvotes: 5

AGMG
AGMG

Reputation: 3948

I don't quite get the part 'text in the drop down menu 'Choose an option' into another language'. But if you just need to change the text into something like 'Select' OR use relevant variation label as default select option, you can do that by overriding Woocommerce default variable.php file.

  1. Create a child theme (always recommended) if you don't have one and activate it
  2. Download variable.php from /wp-content/plugins/woocommerce/templates/single-product/add-to-cart
  3. Open variable.php and search for <option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>&hellip;</option>
  4. Replace Choose an option with any text you need and save the file
  5. Create a new directory under you child theme named 'woocommerce'
  6. Inside of it create single-product --> add-to-cart sub directories and upload previously edited variable.php file.

If you need to remove label and add it as the default option value, replace;

    <td class="label"><label for="<?php echo sanitize_title($name); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
    <td class="value"><select id="<?php echo esc_attr( sanitize_title( $name ) ); ?>" name="attribute_<?php echo sanitize_title( $name ); ?>">
    <option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>&hellip;</option>

With;

    <td class="label"><label for="<?php echo sanitize_title($name); ?>"></label></td>
    <td class="value"><select id="<?php echo esc_attr( sanitize_title( $name ) ); ?>" name="attribute_<?php echo sanitize_title( $name ); ?>">
    <option value=""><?php echo wc_attribute_label( $name ); ?></option>

That's it!

Upvotes: 2

Related Questions