我使用以下代码为每个属性字词设置图片,只有当只为每个产品分配一个字词时,它才能正常工作:
function tutsplus_list_attributes( $product ) {
global $product;
$style = array_shift( wc_get_product_terms( $product->id, 'pa_style', array( 'fields' => 'names' ) ) );
$new = array_shift( wc_get_product_terms( $product->id, 'pa_other', array( 'fields' => 'names' ) ) );
echo '<div class="att">';
if ($style == 'Red Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/red-wine-icon.png" alt="Red Wine" width="35" height="35"> Red';
}
if ($style == 'White Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/white-wine-icon.png" alt="White Wine" width="35" height="35"> White';
}
if ($style == 'Rosé Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/rose-wine-icon.png" alt="Rosé Wine" width="35" height="35"> Rosé';
}
if ($style == 'Sweet') {
echo '<img src="' . get_template_directory_uri() .'/img/sweet-wine-icon.png" alt="Sweet Wine" width="35" height="35"> Sweet';
}
if ($new == 'New Wines') {
echo '<img src="' . get_template_directory_uri() .'/img/new-wine-icon.png" alt="New Wine" width="35" height="35">';
}
echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'tutsplus_list_attributes', 0 );
但显然,有些产品需要不止一个术语,即New,White Wine,Sweet。
我尝试添加foreach循环以使其适用于多个属性术语,但现在它没有显示任何内容:
function tutsplus_list_attributes( $product ) {
global $product;
$style = array_shift( wc_get_product_terms( $product->id, 'pa_style', array( 'fields' => 'names' ) ) );
$new = array_shift( wc_get_product_terms( $product->id, 'pa_other', array( 'fields' => 'names' ) ) );
echo '<div class="att">';
foreach ( $style as $s) {
if ($style == 'Red Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/red-wine-icon.png" alt="Red Wine" width="35" height="35"> Red';
}
elseif ($style == 'White Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/white-wine-icon.png" alt="White Wine" width="35" height="35"> White';
}
elseif ($style == 'Rosé Wine') {
echo '<img src="' . get_template_directory_uri() .'/img/rose-wine-icon.png" alt="Rosé Wine" width="35" height="35"> Rosé';
}
elseif ($style == 'Sweet') {
echo '<img src="' . get_template_directory_uri() .'/img/sweet-wine-icon.png" alt="Sweet Wine" width="35" height="35"> Sweet';
}
elseif ($new == 'New Wines') {
echo '<img src="' . get_template_directory_uri() .'/img/new-wine-icon.png" alt="New Wine" width="35" height="35">';
}
}
echo '</div>';
}
add_action( 'woocommerce_single_product_summary', 'tutsplus_list_attributes', 0 );
我做错了什么?任何帮助表示赞赏。
答案 0 :(得分:0)
更新2:
这是另一个新版本......对我来说,他们两个都在工作。试试这个:
add_action( 'woocommerce_single_product_summary', 'custom_list_attributes', 2 );
function custom_list_attributes( $product ) {
global $product;
$styles = wc_get_product_terms( $product->id, 'pa_style', array( 'fields' => 'names' ) );
$others = wc_get_product_terms( $product->id, 'pa_other', array( 'fields' => 'names' ) );
$path = get_template_directory_uri() .'/img/';
echo '<div class="att">';
foreach( $styles as $style ){
if ($style == 'Red Wine') {
echo '<img src="' . $path .'red-wine-icon.png" alt="Red Wine" width="35" height="35"> Red';
}
elseif ($style == 'White Wine') {
echo '<img src="' . $path .'white-wine-icon.png" alt="White Wine" width="35" height="35"> White';
}
elseif ($style == 'Rosé Wine') {
echo '<img src="' . $path .'rose-wine-icon.png" alt="Rosé Wine" width="35" height="35"> Rosé';
}
elseif ($style == 'Sweet') {
echo '<img src="' . $path .'sweet-wine-icon.png" alt="Sweet Wine" width="35" height="35"> Sweet';
}
}
foreach( $others as $other ){
if ($other == 'New Wines') {
echo '<img src="' . $path .'new-wine-icon.png" alt="New Wine" width="35" height="35">';
break;
}
}
echo '</div>';
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。
原始答案:您应该尝试以下方法(我已经重新访问并压缩了您的代码):
add_action( 'woocommerce_single_product_summary', 'custom_list_attributes', 2 );
function custom_list_attributes() {
global $product;
$colors_array = array( 'Red Wine', 'White Wine', 'Rosé Wine', 'Sweet' ); // Your colors in an array
$pa_style_terms = wc_get_product_terms( $product->get_id(), 'pa_style', array( 'fields' => 'names' ) );
$pa_other_terms = wc_get_product_terms( $product->get_id(), 'pa_other', array( 'fields' => 'names' ) );
$path = get_template_directory_uri() .'/img/';
echo '<div class="att">';
if( count($pa_style_terms) > 0 ){
foreach ( $pa_style_terms as $key => $style) {
// Check if color match with the colors in the array
if ( in_array( $style, $colors_array ) ) {
$color = $colors_array[$key]; // Get the color name
$color_key = explode( ' ', $color ); // Split each word in color
$ckey_name = reset( $color_key ); // Keep the first word in the color
$ckey_slug = strtolower( $ckey_name ); // Make it lower case
// Displaying each color in the loop
echo '<img src="' . $path . $ckey_slug . '-wine-icon.png" alt="' . $color . '" width="35" height="35">' . $ckey_name;
}
}
}
if( count($pa_other_terms) > 0 ){
if ( reset($pa_other_terms) == 'New Wines') {
// Displaying
echo '<img src="' . $path .'new-wine-icon.png" alt="' . reset($pa_other_terms) . '" width="35" height="35">';
}
}
echo '</div>';
}
代码进入活动子主题(或活动主题)的function.php文件。 经过测试并正常工作。