0

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...

1
  • Try replacing $term_names[$attribute_value] = $attribute_value with $term_names[$attribute_value->slug] = $attribute_value Sep 24, 2019 at 21:26

1 Answer 1

-1

Using 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.

7
  • Thanks for the response. I tried your changes but page just tries to load.... @MIke-Baxter Any thoughts?
    – jasone
    Sep 24, 2019 at 21:04
  • Did you try uncommenting the debug lines? If so, did you check your error log for potential issues? If you've done both, with no error log output, you will want to wrap the whole thing in a try{}catch(){} statement to figure out what has gone wrong. Let me know if you need help with try ... catch. Sep 24, 2019 at 21:10
  • Yes, I did umcomment.. but page won't load. I've added extra info to my original question.. I am able to return an object with the code I had, but not able to limit to 1 result per name or term_id.
    – jasone
    Sep 24, 2019 at 21:21
  • I'm pretty sure it is because you are not using $attribute_value->slug or $attribute_value->name as your array key. Sep 24, 2019 at 21:26
  • using $attribute_value->name does return name, but it is still listing all results with same value. How do I now limit it to just one of each?
    – jasone
    Sep 24, 2019 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.