php – 如何在Woocommerce中的“选择一个选项”中更改按钮文本?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何在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','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并根据if语句是否是正确的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;?>
这是自定义过滤器的完美用例!我首先要描述的方法并不是最快捷的方法,但对于可能需要阅读代码的其他人来说,这可能是最干净,最容易理解的方法.如果你处于紧张状态,我还会描述一种“更脏”的方式.

快速方式:

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

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

第27行,根据您的WooCommerce版本,您会看到类似这样的内容

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

__()@L_406_28@使用’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;
}

现在,在变量.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,'show_option_none' => __( $select_text,'woocommerce' )
    ) );
// Put the rest of the function here

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何在Woocommerce中的“选择一个选项”中更改按钮文本?全部内容,希望文章能够帮你解决php – 如何在Woocommerce中的“选择一个选项”中更改按钮文本?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。