从选定的变体中向WooCommerce变量产品标题添加一些属性值 [英] Add some attribute values to WooCommerce variable product title from chosen variation
问题描述
我正在寻找一些帮助,以使WooCommerce可变产品标题根据版本而更改.在这种特定情况下,我希望在选择颜色时更改标题,例如产品名黑色".
有没有简单的代码片段可以使其正常工作?
更新 04-2021-已在WooCommerce 5.1+ (处理自定义产品属性) 上成功测试>
以下代码将根据特定的已定义产品属性(或全部可选,也可以全部选择)添加选定变量的值到可变产品标题中:
代码:
//定义要在可变产品标题上显示的产品属性术语名称add_filter('woocommerce_available_variation','filter_available_variation_attributes',10,3);函数filter_available_variation_attributes($ data,$ product,$ variation){//在此定义产品属性段,并将其值添加到产品标题中//或将数组替换为'all'字符串以显示所有属性值$ attribute_names = array('Custom','Color');foreach($ data ['attributes'] as $ attribute => $ value){$ attribute = str_replace('attribute_','',$ attribute);$ attribute_name = wc_attribute_label($ attribute,$ variation);if((is_array($ attribute_names)&& in_array($ attribute_name,$ attribute_names))|| $ attribute_names ==='all'){$ value = taxonomy_exists($ attribute)吗?get_term_by('slug',$ value,$ attribute)->名称:$ value;$ data ['for_title'] [$ attribute_name] = $ value;}}返回$ data;}//显示为可变的产品标题,已定义的产品属性术语名称add_action('woocommerce_after_variations_form','add_variation_attribute_on_product_title');函数add_variation_attribute_on_product_title(){//这里定义分隔符字符串$ separator ='-';?>< script type =文本/javascript">(函数($){var name ='<?php global $ product;echo $ product-> get_name();?>';$('form.cart').on('show_variation',function(event,data){var text ='';$ .each(data.for_title,function(key,value){文字+ ='<?php echo $ separator;?>'+值;});$('.product_title').text(name + text);}).on('hide_variation',function(event,data){$('.product_title').text(name);});})(jQuery);</script><?php}
显示所有属性
通过将变量 $ attribute_names
定义为"all"
,您可以显示所选变体的所有变体属性值,如下所示:
$ attribute_names ="all";
代码会出现在您活动的子主题(或主题)的functions.php文件中,也可能会出现在任何插件文件中.
经过测试,可以正常工作……您会得到类似的东西:
I'm looking for some help getting the WooCommerce variable product title to change based on variations. In this specific case I would like the title to change when a color is selected, like "Productname Black".
Is there any easy snippet to get this to work?
UPDATE 04-2021 - Successfully tested on WooCommerce 5.1+ (handle custom product attributes)
The following code, will add to variable product title the value(s) of the chosen variation from specific defined product attribute(s) (or all of them optionally too):
The code:
// Defining product Attributes term names to be displayed on variable product title
add_filter( 'woocommerce_available_variation', 'filter_available_variation_attributes', 10, 3 );
function filter_available_variation_attributes( $data, $product, $variation ){
// Here define the product attribute(s) slug(s) which values will be added to the product title
// Or replace the array with 'all' string to display all attribute values
$attribute_names = array('Custom', 'Color');
foreach( $data['attributes'] as $attribute => $value ) {
$attribute = str_replace('attribute_', '', $attribute);
$attribute_name = wc_attribute_label($attribute, $variation);
if ( ( is_array($attribute_names) && in_array($attribute_name, $attribute_names) ) || $attribute_names === 'all' ) {
$value = taxonomy_exists($attribute) ? get_term_by( 'slug', $value, $attribute )->name : $value;
$data['for_title'][$attribute_name] = $value;
}
}
return $data;
}
// Display to variable product title, defined product Attributes term names
add_action( 'woocommerce_after_variations_form', 'add_variation_attribute_on_product_title' );
function add_variation_attribute_on_product_title(){
// Here define the separator string
$separator = ' - ';
?>
<script type="text/javascript">
(function($){
var name = '<?php global $product; echo $product->get_name(); ?>';
$('form.cart').on('show_variation', function(event, data) {
var text = '';
$.each( data.for_title, function( key, value ) {
text += '<?php echo $separator; ?>' + value;
});
$('.product_title').text( name + text );
}).on('hide_variation', function(event, data) {
$('.product_title').text( name );
});
})(jQuery);
</script>
<?php
}
Displaying all attributes
You can display all variations attributes values for the chosen variation by defining the variable $attribute_names
to "all"
so like:
$attribute_names = "all";
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
Tested and works… you will get something like:
这篇关于从选定的变体中向WooCommerce变量产品标题添加一些属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!