替换默认的“选择选项" Woocommerce变体属性下拉列表中的文本由不同的自定义变量 [英] Replace default "Select Option" text in Woocommerce variation attribute dropdowns by different custom ones

查看:494
本文介绍了替换默认的“选择选项" Woocommerce变体属性下拉列表中的文本由不同的自定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Woocommerce,并且对可变产品的下拉选择字段有疑问.在woocommerce中,在此下拉菜单中未选择任何内容时,将显示选择选项"文本.

I am just starting out with Woocommerce and have a question about drop down select field for variable products. In woocommerce when nothing is selected on this dropdowns, "Select Option" text is displayed.

我需要每个下拉选择字段选项,以在未选择任何内容时显示不同的文本.

I need each dropdown select field option to display a different text when nothing has been selected yet.

我发现此代码用我想要的内容替换了标准的选择选项"文本,但是对所有下拉列表选择字段都起作用.

I found this code which replaces the standard "Select Option" text with what ever I want, but it does to all dropdowns select fields.

  function custom_woocommerce_product_add_to_cart_text($args){
 $args['show_option_none'] = __( 'Select Framed or Unframed Sizes', 'woocommerce' ); 
  return $args;
}

我注意到第一个下拉选择字段的ID为"type",第二个下拉字段的ID为"size".

I noticed that the first drop down select field has an ID which is "type", the 2nd one has an ID of "size".

我们可以更改上面的代码并添加以下内容吗?

Can we change the above code and have something along the lines of:

  • 如果ID =类型,则使用文本A,
  • 如果ID =大小,则使用文本B?

我还可以强制第一个下拉列表始终显示所有选项吗?

Also can I force the first drop down list to always show all options?

假设我的第一个下拉列表具有选项A,B,C和D:

Lets say my first drop down list has options A,B,C and D:

  • 如果我选择A,则第二个下拉列表将为我提供1、2、3作为选项.我选择2.
  • 如果现在我尝试在第一个字段中进行另一个选择,则仅显示A和C,而将B和D排除在外,因为B和D在1和3中不可用.

听起来令人困惑,但我希望这是有道理的.

Sounds confusing, but I hope it makes sense.

感谢您的帮助.

推荐答案

在woocommerce中,属性ID(或分类)以" p 开头strong>产生 a 敬意" ,所以:

In woocommerce the attributes IDs (or taxonomy) begin all by pa_ as "product attribute", so:

  • 类型" <select> html标记应具有:id="pa_type"(但不是id="type"),
  • 用于尺寸" <select> html标记应具有:id="pa_size"(而不是id="size").
  • The <select> html tag for "Type" should have: id="pa_type" (but not id="type"),
  • The <select> html tag for "Size" should have: id="pa_size" (but not id="size").

现在,您可以按以下方式通过分类标准来定位每个属性:

Now you can target each attribute by its taxonomy slug this way:

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'dropdown_variation_attribute_options', 10, 1 );
function dropdown_variation_attribute_options( $args ){

    // For attribute "Type"
    if( 'pa_type' == $args['attribute'] )
        $args['show_option_none'] = __( 'Select Framed or Unframed Types', 'woocommerce' );

    // For attribute "Sizes"
    if( 'pa_size' == $args['attribute'] )
        $args['show_option_none'] = __( 'Select Framed or Unframed Sizes', 'woocommerce' );

    return $args;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.

经过测试,可以正常工作.

Tested and works.

您在问许多不同的问题.在这里,我只回答一个.这就是StackOverFlow上的方式.

You are asking many different questions. Here I answer just one. It's the way on StackOverFlow.

在WooCommerce中,一切皆有可能,这取决于您的知识,技能和花费时间.

Everything is possible in WooCommerce, depending on your knowledge, skills and time to spend.

您的第二个问题是一个高级,复杂且过于宽泛的问题.

Your 2nd question is something advanced, complicated and a bit too broad.

这篇关于替换默认的“选择选项" Woocommerce变体属性下拉列表中的文本由不同的自定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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