基于 Woocommerce 产品属性的 foreach 循环中的 If 语句
If statements in a foreach loop based on Woocommerce product attributes
我使用以下代码为每个属性术语设置图像,只有当 只为每个产品分配一个术语时它才能正常工作:
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 );
但显然,某些产品需要多个术语,即新酒、白葡萄酒、甜酒。
我尝试添加一个 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 );
我做错了什么?感谢任何帮助。
更新二:
这是另一个新版本……对我来说它们都有效。试试这个:
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 文件。 已测试并有效。
我使用以下代码为每个属性术语设置图像,只有当 只为每个产品分配一个术语时它才能正常工作:
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 );
但显然,某些产品需要多个术语,即新酒、白葡萄酒、甜酒。
我尝试添加一个 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 );
我做错了什么?感谢任何帮助。
更新二:
这是另一个新版本……对我来说它们都有效。试试这个:
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 文件。 已测试并有效。