In an online store on WooCommerce, I use code that displays certain product attributes on archive/category pages.
<code>add_action( 'woocommerce_before_shop_loop_item_title', 'new_template_loop_product_meta', 20 );
function new_template_loop_product_meta() {
global $product;
$attrs_by_cats = [
20 => [ 'pa_size' ],
];
$attr_list = [
'Size' => 'pa_size',
];
if ( ! is_object( $product ) ) {
$product = wc_get_product( get_the_id() );
}
$cats = $product->get_category_ids();
if ( ! is_array( $cats ) ) {
return;
}
$attrs = [];
foreach ( $cats as $cat ) {
if ( isset( $attrs_by_cats[ $cat ] ) ) {
$attrs[] = $attrs_by_cats[ $cat ];
}
}
$allowed_attrs = array_unique( array_merge( [], ...$attrs ) );
echo '<div class="custom-attributes">';
foreach ( $attr_list as $attr_title => $attr_name ) {
if ( in_array( $attr_name, $allowed_attrs, true ) ) {
show_attribute( $product, $attr_title, $attr_name );
}
}
echo '</div>';
}
function show_attribute( $product, $attr_title, $attr_name ) {
if ( 'sku' === $attr_name ) {
$attr = (string) $product->get_sku();
} else {
$attr = $product->get_attribute( $attr_name );
}
if ( '' === $attr ) {
return;
}
echo '<span class="custom-attributes-text">Size: ' . esc_html( $attr ) . '</span>';
}
</code>
<code>add_action( 'woocommerce_before_shop_loop_item_title', 'new_template_loop_product_meta', 20 );
function new_template_loop_product_meta() {
global $product;
$attrs_by_cats = [
20 => [ 'pa_size' ],
];
$attr_list = [
'Size' => 'pa_size',
];
if ( ! is_object( $product ) ) {
$product = wc_get_product( get_the_id() );
}
$cats = $product->get_category_ids();
if ( ! is_array( $cats ) ) {
return;
}
$attrs = [];
foreach ( $cats as $cat ) {
if ( isset( $attrs_by_cats[ $cat ] ) ) {
$attrs[] = $attrs_by_cats[ $cat ];
}
}
$allowed_attrs = array_unique( array_merge( [], ...$attrs ) );
echo '<div class="custom-attributes">';
foreach ( $attr_list as $attr_title => $attr_name ) {
if ( in_array( $attr_name, $allowed_attrs, true ) ) {
show_attribute( $product, $attr_title, $attr_name );
}
}
echo '</div>';
}
function show_attribute( $product, $attr_title, $attr_name ) {
if ( 'sku' === $attr_name ) {
$attr = (string) $product->get_sku();
} else {
$attr = $product->get_attribute( $attr_name );
}
if ( '' === $attr ) {
return;
}
echo '<span class="custom-attributes-text">Size: ' . esc_html( $attr ) . '</span>';
}
</code>
add_action( 'woocommerce_before_shop_loop_item_title', 'new_template_loop_product_meta', 20 );
function new_template_loop_product_meta() {
global $product;
$attrs_by_cats = [
20 => [ 'pa_size' ],
];
$attr_list = [
'Size' => 'pa_size',
];
if ( ! is_object( $product ) ) {
$product = wc_get_product( get_the_id() );
}
$cats = $product->get_category_ids();
if ( ! is_array( $cats ) ) {
return;
}
$attrs = [];
foreach ( $cats as $cat ) {
if ( isset( $attrs_by_cats[ $cat ] ) ) {
$attrs[] = $attrs_by_cats[ $cat ];
}
}
$allowed_attrs = array_unique( array_merge( [], ...$attrs ) );
echo '<div class="custom-attributes">';
foreach ( $attr_list as $attr_title => $attr_name ) {
if ( in_array( $attr_name, $allowed_attrs, true ) ) {
show_attribute( $product, $attr_title, $attr_name );
}
}
echo '</div>';
}
function show_attribute( $product, $attr_title, $attr_name ) {
if ( 'sku' === $attr_name ) {
$attr = (string) $product->get_sku();
} else {
$attr = $product->get_attribute( $attr_name );
}
if ( '' === $attr ) {
return;
}
echo '<span class="custom-attributes-text">Size: ' . esc_html( $attr ) . '</span>';
}
Right now the code displays the “Size” attribute in a single list/array, comma separated. I need to show the sizes individually and without comma, i.e. wrap each of the sizes in a span and make a border for each one.
I’ve looked at different options on this site for placing attributes on the archive/categories pages, but nothing fits.
How can this be implemented? I will be glad to help with the code!