Reputation: 41
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
Reputation: 1
// Меняем текст "выбрать опцию" на странице вариативного товара, на название атрибута
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
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
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
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
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.
variable.php
from /wp-content/plugins/woocommerce/templates/single-product/add-to-cartvariable.php
and search for <option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>…</option>
Choose an option
with any text you need and save the filevariable.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' ) ?>…</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