如何从“选择一个选项”更改按钮文本?在Woocommerce


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

我该如何去改变这个PHP代码来改变选择一个基于选择元素的id在Woocommerce插件WordPress的选项?我相信我已经在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语句,但它似乎不工作。我不确定如何引用选择元素的id,并根据它是否是正确的选择元素来编写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;?>

这是一个自定义过滤器的完美用例!首先,我将描述一种方法,它不是最快的方法,但对于其他可能必须阅读您的代码的人来说,它可能是最干净、最容易理解的方法。如果你时间紧迫,我还会描述一种"更肮脏"的方式来完成这项工作。

快捷方式:

可以在文件中找到它的显示位置:

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

然后,用$select_text变量替换掉'Choose an option':

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

为这样的事情设置自定义过滤器确实需要花费更长的时间,但是你获得了可维护性的优势,因为你可以在以后的过程中堆栈或关闭函数,而不需要进一步修改现有的代码。

更新wc2.4

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

//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

我已经使用了这里的其他答案的组合,它对我很有效。

这是使用woocommerce 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 ); 

希望这能帮助到别人。

我找到了使用wc2.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);

实际上有一种更简单的方法来定制它。

如果你看一下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 )
    )
);

其他对也一样。希望这能帮到你。顺便说一下,这只适用于wc2.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 );
} ?>

这将替换'选择一个选项'为'选择大小','选择颜色'等,这取决于你的属性名称。

如果你想改变Woocommerce中" Choose an option "的下拉文本,在你的主题中添加下面的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; 
}

如果您想知道如何将" Choose an option "替换为相应的属性/变体名称,那么您可以这样做。编辑variable.php并查找如下所示的代码

打开变量。php(wp-content/plugins/woocommerce/includes/variable.php)文件到您的woocommerce,并更改以下内容(从第41行改为第43行):

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