wordpress 在woocommerce中显示所选变体的属性术语描述

pprl5pva  于 12个月前  发布在  WordPress
关注(0)|答案(1)|浏览(110)

在WooCommerce下产品>属性> [属性名称] >添加新的[属性变体]有一个标题为“描述”的部分,如何显示描述属性,而不是variation.php文件中的描述**{{ data.variation.variation_description }}**?
我试过如何在WooCommerce回答代码中获取当前选定的变量产品特定数据,但不知道如何显示属性描述。

k10s72fa

k10s72fa1#

这不需要使用JavaScript或jQuery。通过以下内容,您将能够用属性术语描述替换变化描述:

add_filter( 'woocommerce_available_variation', 'display_available_attributes_term_description', 10, 3 );
function display_available_attributes_term_description( $data, $product, $variation ) {
    $terms_descriptions = array(); // Initialize variable

    // loop through attributes variation
    foreach( $data['attributes'] as $attr_taxonomy => $term_slug ) {
        $taxonomy = str_replace('attribute_', '', $attr_taxonomy);
        // Check that is not a custom attribute
        if( ! taxonomy_exists($taxonomy) ) {
            continue;
        }
        $term_id = get_term_by('slug', $term_slug, $taxonomy)->term_id; // Get the term ID
        $term_description = term_description( $term_id ); // Get the term description
        // Check that term description is not empty
        if( ! empty($term_description) ) {
            $terms_descriptions[] = $term_description; // Add the term description to the array
        }
    }
    // Replace variation description with attribute(s) term description(s)
    if( count($terms_descriptions) > 0 ) {
        $data['variation_description'] = implode(' <br>', $terms_descriptions); // stringify the array
    }

    return $data;
}

代码放在子主题的functions.php文件中(或插件中)。测试和作品。
如果要保留变体说明,并在其后显示属性术语说明,请替换:

$data['variation_description'] = implode(' <br>', $terms_descriptions);

使用:

$data['variation_description'] .= ' <br>' . implode(' <br>', $terms_descriptions);

相关问题