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;
}
1条答案
按热度按时间k10s72fa1#
这不需要使用JavaScript或jQuery。通过以下内容,您将能够用属性术语描述替换变化描述:
代码放在子主题的functions.php文件中(或插件中)。测试和作品。
如果要保留变体说明,并在其后显示属性术语说明,请替换:
使用: