I have a WP Query to list matching products, like this:
function sku_ean_sizes () {
global $product;
$current_stijlcode = $product->get_attribute( 'pa_stijlcode' );
$current_ean = $product->get_attribute( 'pa_ean' );
$postid = get_the_ID();
$args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc',
'posts_per_page' => 100,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'pa_stijlcode',
'field' => 'slug',
'terms' => $current_stijlcode,
),
array(
'taxonomy' => 'pa_ean',
'field' => 'slug',
'terms' => $current_ean,
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
foreach( wc_get_product_terms( $product->id, 'pa_maat' ) as $attribute_value ){
echo '<span>' . $attribute_value . '</span>';
}
endwhile;
} else {
// no sku matches found
}
wp_reset_postdata();
}
I would like to use the foreach loop to list the found product attributes attached to the products found with the WP Query:
global $product;
foreach( wc_get_product_terms( $product->id, 'pa_maat' ) as $attribute_value ){
echo '<span>' . $attribute_value . '</span>';
}
This code works. However, the $attribute_value
variable outputs duplicate records. This makes sense, as multiple products could have the same output.
What adjustment can i make so it excludes duplicate values?
In context; This is to display all available sizes for a specific product.