如何在 Woocommerce 中更改 "Choose an option" 中的按钮文本?

How do I change button text from "Choose an option" in Woocommerce?

我将如何更改此 PHP 代码以根据 WordPress Woocommerce 插件中 select 元素的 ID 选择一个选项?我相信我已经在 wc-template-function.php 中找到了正确的 PHP 文件,但是我缺乏 PHP 技能阻碍了我。这是我目前所拥有的:

if ( ! function_exists( 'wc_dropdown_variation_attribute_options' ) ) {

    /**
     * Output a list of variation attributes for use in the cart forms.
     *
     * @param array $args
     * @since 2.4.0
     */
    function wc_dropdown_variation_attribute_options( $args = array() ) {
        $args = wp_parse_args( $args, array(
            'options'          => false,
            'attribute'        => false,
            'product'          => false,
            'selected'         => false,
            'name'             => '',
            'id'               => '',
            'class'            => '',
            'show_option_none' => __( 'Choose an option', '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>';
    }
}

您可以看到我尝试将 show_option_color 和 show_option_size 添加到数组中的位置,然后为它们添加 if 语句,但它似乎不起作用。我不确定如何引用 select 元素的 ID 并根据它是否是正确的 select 元素编写 if 语句。

这是我要定位的 HTML。

<select id="sizes" class="" name="attribute_sizes" data-attribute_name="attribute_sizes">Want this to say Choose a size</select>

<select id="colors" class="" name="attribute_sizes" data-attribute_name="attribute_sizes">Want this to say Choose a color</select>

variable.php 代码行 27 - 38:

<?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;?>

这是自定义过滤器的完美用例!我首先要描述一种方法,它不是最快的方法,但对于可能需要阅读您的代码的任何其他人来说,它可能是最简洁和最容易理解的方法。我还将描述一种 'dirtier' 方法,如果您时间紧迫,应该可以用它来完成这项工作。

快捷方式:

找到这个显示的地方在文件中:

/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/variable.php

在第 27 行附近,根据您的 WooCommerce 版本,您会看到类似以下行的内容:

<option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>&hellip;</option>

__() 函数是 运行 通过使用 'woocommerce' 文本域的 WordPress 翻译系统的第一个参数。最好保留翻译的可能性,因此我们希望在通过翻译功能发送之前更改此文本。

这行代码发生在输出所有产品变体属性的循环中。这使我们可以通过查看 $name 变量轻松查看正在输出哪个属性。

我们需要创建一个函数,接收 $name 变量并根据它输出一个字符串。它看起来像这样:

function get_text_for_select_based_on_attribute($attribute) {

// Find the name of the attribute for the slug we passed in to the function
$attribute_name = wc_attribute_label($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;
}

现在,在variable.php第27行的代码之前,我们可以放这样的东西:

<?php 

  $select_text = get_text_for_select_based_on_attribute($name);

?>

然后,只需将 'Choose an option' 换成您的 $select_text 变量即可:

<option value=""><?php echo __( $select_text, 'woocommerce' ) ?>&hellip;</option>

不要忘记在模板覆盖中完成所有这些操作,否则您的自定义设置将在下次更新时丢失!

http://docs.woothemes.com/document/template-structure/

清洁方式:

一个更好、更可扩展的方法是添加一个自定义过滤器来传递它。这是一些额外的步骤,但如果您想根据您的产品逐个覆盖功能,则可以轻松添加更多自定义逻辑。

首先,使用具有语义意义的名称创建一个自定义过滤器,并将其放在主题的 functions.php 文件中的某个位置:

add_filter('variable_product_select_text', 'get_text_for_select_based_on_attribute', 10, 1);

然后,在 variable.php 文件中,不是直接调用函数,而是通过新过滤器传递它:

$select_text = apply_filters('variable_product_select_text', $name);

为这样的事情设置自定义过滤器确实需要更长的时间,但是您可以获得可维护性的优势,因为您可以在以后堆叠或关闭功能而无需进一步修改现有代码。

WC 2.4 更新

WooCommerce 2.4 版引入了一种获取属性及其关联 select 的不同方式。由于他们仍然没有为此提供过滤器,我建议使用上述方法覆盖 wc_dropdown_variation_attribute_options 函数。因此,将整个函数从声明开始复制并粘贴到主题的 functions.php 文件中,如果不是颜色或大小,则为 select 文本添加一个变量:

//Don't include the if(!function_exists(...) part.

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']);

  wc_dropdown_variation_attribute_options( $args = array() ) {
    $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' )
    ) );
// Put the rest of the function here

这是 WC >2.4 的一个非常简单的解决方案,可以避免重写函数和弄乱您的 functions.php..

variable.php 文件添加到您的主题 (http://docs.woothemes.com/document/template-structure/),并更改此内容(从第 27 行开始):

<?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;?>

对此:

<?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 );
} ?>

这会将 'Choose an option' 替换为 'Choose Size'、'Choose Colour' 等,具体取决于您的属性名称。

实际上有一种更简单的自定义方法。

如果您查看核心 WC 文件 wc-template-functions.php,您会发现 wc_dropdown_variation_attribute_options 函数中的数组实际上是它的参数,具有以下键值对:

'options'          => false,
'attribute'        => false,
'product'          => false,
'selected'         => false,
'name'             => '',
'id'               => '',
'class'            => '',
'show_option_none' => __( 'Choose an option', 'woocommerce' )

现在您需要做的就是在variable.php模板文件中的函数中更改相同的键值对。所以我想显示下拉标签而不是选择选项文本,所以我更改了 fu

wc_dropdown_variation_attribute_options(
    array(
         'options' => $options,
         'attribute' => $attribute_name,
         'product' => $product,
         'selected' => $selected,
         'show_option_none' => wc_attribute_label( $attribute_name )
    )
);

其他对也一样。希望这有帮助。顺便说一句,这只适用于 WC 2.4+ 所以要小心。

我找到了使用 WC 2.5 的方法。将此添加到 functions.php:

function my_dropdown_variation_attribute_options_html($html, $args){
    $html = str_replace('Choose an option', 'Choose', $html);
    return $html;
}
add_filter('woocommerce_dropdown_variation_attribute_options_html', 'my_dropdown_variation_attribute_options_html', 10, 2);

如果您想知道如何用相应的 attribute/variation 名称替换“选择一个选项”,那么您可以按照以下方法进行操作。 编辑 variable.php 并查找如下所示的代码

打开 variable.php(wp-content/plugins/woocommerce/includes/variable.php) 文件到您的 woocommerce 并更改它(从第 41 行到第 43 行):(删除这 3 行代码)

$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>' ) : '';

然后添加这段代码

wc_dropdown_variation_attribute_options
array( 'options' => $options,
'attribute' => $attribute_name,
'product' => $product,
'selected' => $selected,
'show_option_none'=> wc_attribute_label( $attribute_name )
) );

它适用于所有 WordPress 版本 谢谢

更新代码,为仍然感兴趣的人制作

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 
fix_option', 10 ); 
function fix_option( $args ) {
    $attr = get_taxonomy( $args['attribute'] ); 
    $label = $attr->labels->name; 
    $fix = str_replace('Product', '', $label); 
    $fix = strtolower($fix); /
    $args['show_option_none'] = apply_filters( 'the_title', 'Select a '.$fix ); 
}

如果您想在 Woocommerce 中更改“选择一个选项”的下拉文本,请将以下内容添加到主题中的 functions.php 文件:

add_filter('woocommerce_dropdown_variation_attribute_options_args', 'my_change_choose_an_option_text_func', 10, 2);

function my_change_choose_an_option_text_func($args){ 
    $args['show_option_none'] = __( 'New Text', 'your_text_domain' ); 
    return $args; 
}

我结合使用此处的其他答案来完成此操作,对我来说效果很好。

这是使用 WooCoommerce 3.3.5 完成的,并假设过滤器是最近才添加的。

您使用 wc_dropdown_variation_attribute_options() 函数的过滤器。

// define the woocommerce_dropdown_variation_attribute_options_args callback 
function filter_woocommerce_dropdown_variation_attribute_options_args( $array ) { 

    // Find the name of the attribute for the slug we passed in to the function
    $attribute_name = wc_attribute_label($array['attribute']);

    // Create a string for our select
    $select_text = 'Select a ' . $attribute_name;

    $array['show_option_none'] = __( $select_text, 'woocommerce' );
    return $array; 
}; 

// add the filter 
add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 ); 

希望这对某人有所帮助。