to build off of LoicTheAztec's answer:
His code only works if you have predefined the attributes in the WP backend under Products -> Attributes. If you work with individual (custom) product attributes that you set up on the product page, wc_get_product_terms() won't return anything. You can recognize predefined attributes by the "pa_" prefix, they are stored in the "woocommerce_attribute_taxonomies" table.
In order to show these individual attributes in the same way as LoicTheAztec suggested, use this code:
add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20);
function custom_attributes_display()
{
// Just for product category archive pages
if(is_product_category())
{
global $product;
// get all product attributes
$attributes = $product->get_attributes();
// the array of attributes you want to display (shown in same order)
$show_attributes = array('My Attribute A', 'Another Attribute B');
foreach($show_attributes as $key => $show_attribute)
{
foreach($attributes as $attribute)
{
// check if current attribute is among the ones to be shown
if ($attribute->get_name() == $show_attribute)
{
echo $attribute->get_options()[0];
// seperate attributes by "/"
if (count($show_attributes) > 1)
echo '/';
unset($show_attributes[$key]);
break;
}
}
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…