I am trying to create a product loop based on an attribute value from (pa_product-series). many products can have the same value, but I only want to return one from each...avoiding multiple products with same value.
Example (xxxx is the product-series):
product 1 - x400
product 2 - x400
product 3 - x500
product 4 - x600
product 5 - x600
Results should only show product 1, product 3 and product 4.
This is the code I have so far, but its returning an empty array
global $product;
$prod_series = $product->get_attribute( 'pa_product-series' );
$postid = get_the_ID();
$args = array(
'post_type' => 'product',
'orderby' => 'title',
'order' => 'asc',
'posts_per_page' => 10,
'tax_query' => array(
'relation' => '', array(
'taxonomy' => 'pa_product-series',
'field' => 'slug',
'terms' => $prod_series
),
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ):
$term_names = array();
while ( $loop->have_posts() ):
$loop->the_post();
foreach( wc_get_product_terms($loop->post->ID, 'pa_product-series' ) as $attribute_value ):
$term_names[$attribute_value] = $attribute_value;
endforeach;
endwhile;
print_r($attribute_value);
else:
echo '<span>No SKUs matches found</span>';
endif;
wp_reset_postdata();
when I use print_r($attribute_value), it returns the following:
WP_Term Object (
[term_id] => 1687
[name] => CT0201CSF
[slug] => ct0201csf
[term_group] => 0
[term_taxonomy_id] => 1687
[taxonomy] => pa_product-series
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
[term_order] => 0
)
WP_Term Object (
[term_id] => 1687
[name] => CT0201CSF
[slug] => ct0201csf
[term_group] => 0
[term_taxonomy_id] => 1687
[taxonomy] => pa_product-series
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
[term_order] => 0
)
I need to limit to 1 result per term_id or name...
I am trying to create a product loop based on an attribute value from (pa_product-series). many products can have the same value, but I only want to return one from each...avoiding multiple products with same value.
Example (xxxx is the product-series):
product 1 - x400
product 2 - x400
product 3 - x500
product 4 - x600
product 5 - x600
Results should only show product 1, product 3 and product 4.
This is the code I have so far, but its returning an empty array
global $product;
$prod_series = $product->get_attribute( 'pa_product-series' );
$postid = get_the_ID();
$args = array(
'post_type' => 'product',
'orderby' => 'title',
'order' => 'asc',
'posts_per_page' => 10,
'tax_query' => array(
'relation' => '', array(
'taxonomy' => 'pa_product-series',
'field' => 'slug',
'terms' => $prod_series
),
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ):
$term_names = array();
while ( $loop->have_posts() ):
$loop->the_post();
foreach( wc_get_product_terms($loop->post->ID, 'pa_product-series' ) as $attribute_value ):
$term_names[$attribute_value] = $attribute_value;
endforeach;
endwhile;
print_r($attribute_value);
else:
echo '<span>No SKUs matches found</span>';
endif;
wp_reset_postdata();
when I use print_r($attribute_value), it returns the following:
WP_Term Object (
[term_id] => 1687
[name] => CT0201CSF
[slug] => ct0201csf
[term_group] => 0
[term_taxonomy_id] => 1687
[taxonomy] => pa_product-series
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
[term_order] => 0
)
WP_Term Object (
[term_id] => 1687
[name] => CT0201CSF
[slug] => ct0201csf
[term_group] => 0
[term_taxonomy_id] => 1687
[taxonomy] => pa_product-series
[description] =>
[parent] => 0
[count] => 2
[filter] => raw
[term_order] => 0
)
I need to limit to 1 result per term_id or name...
Share Improve this question edited Sep 24, 2019 at 21:20 jasone asked Sep 24, 2019 at 16:24 jasonejasone 11 bronze badge 1 |1 Answer
Reset to default -1Using the developers' reference example, you are referencing $loop redundantly. Drop the $loop->
before the_post()
and have_posts()
:
if ( have_posts() ) {
$term_names = array();
while ( have_posts() ) {
the_post();
$myPostID = get_the_ID();
//Uncomment next 3 lines to debug using your error log
//ob_start();
//var_dump($myPostID);
//error_log('myPostID = ' . ob_get_clean(),0);
$arrProductTerms = wc_get_product_terms( $myPostID, 'pa_product-series' );
//Uncomment next 3 lines to debug using your error log
//ob_start();
//var_dump($arrProductTerms);
//error_log('arrProductTerms = ' . ob_get_clean(),0);
foreach($arrProductTerms as $att){
// prevent later values from overwriting existing...
// Also, I believe $att is an object with properties, so you need to choose which one should be unique. I chose "slug"
if (!array_key_exists($att->slug,$term_names)) $term_names[$att->slug]=$att;
} // loop foreach
} // loop while
// sort by key ($att->slug)
ksort($term_names);
// Used array_keys because we stored $att as objects
echo '<span>' . implode( '</span>, <span>', array_keys($term_names) ) . '</span> ';
} else {
echo '<span>No SKUs matches found</span>';
} // end if
If that doesn't get it, I would use the error_log()
call to closely examine the output of wc_get_product_terms()
to see if you are getting what you expect.
$term_names[$attribute_value] = $attribute_value
with$term_names[$attribute_value->slug] = $attribute_value
– Mike Baxter Commented Sep 24, 2019 at 21:26