从所选变体向 WooCommerce 变量产品标题添加一些属性值 [英] Add some attribute values to WooCommerce variable product title from chosen variation

查看:30
本文介绍了从所选变体向 WooCommerce 变量产品标题添加一些属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求帮助,让 WooCommerce 变量产品标题根据变体进行更改.在这种特定情况下,我希望在选择颜色时更改标题,例如产品名称黑色".

是否有任何简单的代码片段可以让它发挥作用?

解决方案

UPDATE 04-2021 - 在 WooCommerce 5.1+ 上成功测试(处理自定义产品属性)

以下代码将把来自特定定义的产品属性的所选变体的值添加到变量产品标题中(或可选的所有属性):

代码:

//定义要显示在变量产品标题上的产品属性术语名称add_filter( 'woocommerce_available_variation', 'filter_available_variation_attributes', 10, 3 );函数 filter_available_variation_attributes( $data, $product, $variation ){//这里定义产品属性(s)slug(s),哪些值将被添加到产品标题//或者用'all'字符串替换数组以显示所有属性值$attribute_names = array('自定义', '颜色');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;}}返回 $data;}//显示到可变产品标题,定义的产品属性术语名称add_action('woocommerce_after_variations_form', 'add_variation_attribute_on_product_title');函数 add_variation_attribute_on_product_title(){//这里定义分隔符字符串$separator = ' - ';?><script type="text/javascript">(功能($){var name = '<?php global $product;echo $product->get_name();?>';$('form.cart').on('show_variation', function(event, data) {var 文本 = '';$.each( data.for_title, function( key, value ) {text += '<?php echo $separator;?>'+ 价值;});$('.product_title').text(name + text);}).on('hide_variation', function(event, data) {$('.product_title').text(name);});})(jQuery);<?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屋!

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