【问题标题】:How do I change button text from "Choose an option" in Woocommerce?如何从 Woocommerce 中的“选择一个选项”更改按钮文本?
【发布时间】:2015-08-23 19:21:41
【问题描述】:

我将如何更改此 PHP 代码以更改基于 WordPress 的 Woocommerce 插件中选择元素的 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;?>

【问题讨论】:

  • 你能澄清这部分吗? '根据产品的 id 选择一个选项' 你的意思是根据产品的 id 吗?
  • 当然。每个产品都没有可供选择的选项、选择尺寸选项、选择颜色选项或两者都有。但是,他们没有说选择尺寸或选择颜色,而是说“选择一个选项”。我只想让每个人说选择颜色或根据您实际选择的尺寸选择尺寸。选择颜色按钮的 id 为 #colors,选择尺寸按钮的 id 为 #sizes,但两者的显示文本都显示“选择一个选项。希望这可以澄清。
  • 是的,这有帮助。尽快寻找答案!

标签: php wordpress woocommerce


【解决方案1】:

我在这里使用了其他答案的组合来完成此操作,它对我来说效果很好。

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

希望这对某人有所帮助。

【讨论】:

  • 它做到了!谢谢!!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    这会将“选择一个选项”替换为“选择尺寸”、“选择颜色”等,具体取决于您的属性名称。

    【讨论】:

    • 我将 variable.php 添加到我的主题的根目录(使用 style.css 和 functions.php)并将代码更改为您上面的代码,我没有从“选择一个选项”中得到任何改变”。 pastebin.com/FBTmmCKq 是我的 variable.php 的样子
    • @jfoutch 您需要遵循主题覆盖文件夹(http://docs.woothemes.com/document/template-structure/)中的 woocommerce 模板目录结构,因此请将其放在 YOUR_THEME_ROOT/woocommerce/single-product/add-to-cart/variable.php
    • @jfoutch 您还需要删除第 46 行多余的 &lt;?php endforeach;?&gt;。堆栈溢出编辑器决定删除该代码块。基本上只是用上面的替换原来的foreach循环。
    【解决方案4】:

    其实还有更简单的自定义方法。

    如果您查看核心 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+,所以要小心。

    【讨论】:

      【解决方案5】:

      这是自定义过滤器的完美用例!我首先要描述一种方法,它不是最快的方法,但对于可能必须阅读您的代码的其他人来说,它可能是最简洁和最容易理解的方法。我还将描述一种“更肮脏”的方式,如果您处于时间紧缩状态,它应该可以完成这项工作。

      快捷方式:

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

      /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 变量替换“选择一个选项”:

      <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 版引入了一种获取属性及其关联选择的不同方式。由于他们仍然没有为此提供过滤器,我建议使用上述方法覆盖 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 版本中,该特定行不在 variable.php 中。然而,我确实在 woocommerce/includes/wc-template-functions.php 中找到了它。我将variable.php代码添加到上面的OP中。
      • 啊,我看到他们最近改变了很多。它仍然在 variable.php 中处理,除了现在您要查找第 33 行。我将更新答案以反映新方法。
      • 实际上,只是再次查看了您的粘贴 - 有几个语法问题。您在顶部有一个杂散的正斜杠,并且似乎您试图在其内部调用该函数,而大括号不匹配。尝试用这个覆盖你所拥有的,并特别注意语法和附件:pastebin.com/fvkbf7Lp
      • 我想我明白了 - 将 get_text_for_select_based_on_attribute() 函数更新为:pastebin.com/JLpbZnSH
      • 太棒了!谢谢!您绝对应该密切关注该功能在未来的变化。覆盖一个函数很容易导致更新损坏,但是 Woo 团队通常不会在每个版本中一遍又一遍地更改相同的内容。您可能至少在下一个主要版本之前有一个宽限期,但将其放在列表中以在未来更新期间进行检查。不幸的是,系统不会告诉你是否有函数覆盖更新,就像它告诉你模板覆盖一样。
      【解决方案6】:

      如果您希望在 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; 
      }
      

      【讨论】:

        【解决方案7】:

        更新了代码,供任何仍然感兴趣的人使用

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

        【讨论】:

          【解决方案8】:

          如果您想知道如何将“选择一个选项”替换为相应的属性/变体名称,那么您可以这样做。 编辑 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 版本 谢谢

          【讨论】:

          • 对我不起作用。 wc_dropdown_variation_attribute_options 和数组之间似乎缺少“(”。添加缺少的“(”后完美工作。谢谢!
          猜你喜欢
          • 2017-10-22
          • 2023-01-09
          • 2013-07-10
          • 2013-10-15
          • 2016-04-24
          • 2018-12-11
          • 2023-03-10
          • 2014-07-20
          相关资源
          最近更新 更多