这不需要使用JavaScript或jQuery.使用以下内容,您将能够使用属性术语描述(S)替换变体描述:
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;
}
代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.
如果要保留变体说明,并在其后面显示属性术语说明,请替换:
$data['variation_description'] = implode(' <br>', $terms_descriptions);
有:
$data['variation_description'] .= ' <br>' . implode(' <br>', $terms_descriptions);