如何从“选择一个选项"更改按钮文本?在 Woocommerce 中? [英] How do I change button text from "Choose an option" in Woocommerce?

查看:15
本文介绍了如何从“选择一个选项"更改按钮文本?在 Woocommerce 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would I go about changing this PHP code to to change Choose an option based on the id of the select element in the Woocommerce plugin for WordPress? I believe I have found the correct PHP file in wc-template-function.php but my lack of PHP skills is holding me back. Here is what I have so far:

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 语句.

You can see where I tried to add show_option_color and show_option_size in to the array and then add if statements for them, but it doesn't seem to be working. I'm not sure how to reference the id of the select element and write the if statement based on if its is the correct select element.

这是我要定位的 HTML.

Here is the HTML I'm trying to target.

<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 行:

variable.php code lines 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;?>

推荐答案

这是自定义过滤器的完美用例!我首先要描述一种方法,它不是最快的方法,但对于可能需要阅读您的代码的任何其他人来说,它可能是最清晰和最容易理解的方法.我还将描述一种更肮脏"的方式,如果您处于时间紧迫的情况下应该可以完成这项工作.

This is a perfect use case for a custom filter! I am first going to describe a method that is not the quickest way, but it is probably the cleanest and easiest to understand for anyone else who might have to read your code. I will also describe a 'dirtier' way that should do the job if you are in a time crunch.

快速方法:

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

The place to find where this is displayed is in the file:

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

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

Around line 27, depending on your version of WooCommerce, you will see something like this line:

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

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

The __() function is running the first parameter through WordPress's translation system using the 'woocommerce' text domain. It is best to preserve the possibility for translation, so we will want to change this text before we send it through the translation function.

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

This line of code happens during a loop that outputs all of the product variation attributes. This allows us to easily see which attribute is being output by looking at the $name variable.

我们需要创建一个函数,它接受 $name 变量并基于它输出一个字符串.它看起来像这样:

We will need to make a function that takes in the $name variable and outputs a string based on it. It would look something like this:

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 行代码之前,我们可以这样写:

Now, before the code on line 27 of variable.php, we can put something like this:

<?php 

  $select_text = get_text_for_select_based_on_attribute($name);

?>

然后,只需将选择一个选项"替换为您的 $select_text 变量:

Then, simply swap out 'Choose an option' with your $select_text variable:

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

不要忘记在模板覆盖中完成这一切,否则您的自定义将在下次更新时丢失!

Don't forget to do this all in a template override or your customization will be lost on the next update!

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

更清洁的方式:

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

A better and more extensible way of doing this is to add a custom filter to pass this through. It's a few extra steps, but allows you to easily add further custom logic if you want to override the functionality on a case-by-case basis depending on your product.

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

First, make a custom filter with a semantically-meaningful name, and put it somewhere in your functions.php file for the theme:

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

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

Then, in the variable.php file, instead of just calling the function directly, pass it through your new filter:

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

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

Setting up custom filters for things like this does take a little bit longer, but you get the advantage of maintainability, since you can stack or turn off functions down the road without needing to further modify your existing code.

WC 2.4 的更新

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

Version 2.4 of WooCommerce introduces a different way of getting attributes and their associated selects. Since they still have not provided a filter for this, I would recommend overriding the wc_dropdown_variation_attribute_options function using the methods described above. So copy and paste the entire function into your theme's functions.php file starting at the declaration, and add a variable for the select text if it's not a color or size:

//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 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆