Been trying to figure out how I can get the product attributes of the current product and store it in a variable and put it in a class.

I’ve managed to get the product attributes, unfortunately it seems to be displaying all the products attributes to all the products I have set. Here is the code I have been working.

    <div id="Container" class="nine columns mixitup-container bevtools-liquor">

        <?php

        $liquor = new WP_Query( array( 
            'post_type'   => 'product',
            'product_cat' => 'liquors',
            'meta_query'  => array(
                array(
                    'key'   => '_stock_status',
                    'value' => 'instock'
                )
            )
        ) );

        if ( $liquor->have_posts() ) : while ( $liquor->have_posts() ) : $liquor->the_post();
        ?>


        //In this foreach loop, I'm trying to get all the terms for liquor-brands attributes

        <?php 
        $brand_terms = get_the_terms( $post, 'pa_liquor-brands' );
        foreach ( $brand_terms as $term ) :
        ?>

        <?php $brand_string = ''; ?>
        <?php $brand_string .= $term->slug . ' '; ?>

        <?php endforeach; ?>



        <div id="post-<?php the_ID(); ?>" class="three columns mix product-post <?php echo $brand_string  ?>" >
        </div>


        <?php wp_reset_postdata(); ?>

        <?php endwhile; else: ?>

        <?php //error message ?>

        <?php endif; ?>


        <?php wp_reset_query(); ?>

After running the code here is what the output looks like.

<div id="post-2190" class="34th-pursuit-joes-brew absolut aviation-gin bacardi botanist citadelle-gin don-papa gvine grey-goose jack-daniel johnnie-walker makers-mark monkey-shoulder pale-ale-katipunan" style="display: inline-block;" data-bound="">
</div>

<div id="post-2192" class="34th-pursuit-joes-brew absolut aviation-gin bacardi botanist citadelle-gin don-papa gvine grey-goose jack-daniel johnnie-walker makers-mark monkey-shoulder pale-ale-katipunan" style="display: inline-block;" data-bound="">
</div>

As you can see both products are showing all the product attributes instead of showing what is assigned to them instead.

1 Answer
1

get_terms() Retrieves the terms in a given taxonomy or list of
taxonomies.

What you need is

get_the_terms() Retrieves the terms of the taxonomy that are attached
to the post
.

So you can simply replace

$brand_terms = get_terms( 'pa_liquor-brands' );

with

$brand_terms = get_the_terms( $post, 'pa_liquor-brands' );

And that should to the trick.

You can read more about these two functions here:

https://developer.wordpress.org/reference/functions/get_terms/
https://developer.wordpress.org/reference/functions/get_the_terms/

Edit:
And you will also need to reset your $brand_string otherwise it will add terms from another posts and output them

$brand_terms = get_the_terms($post, 'pa_liquor-brands');
$brand_string = ''; // Reset string
foreach ($brand_terms as $term) :
    $brand_string .= $term->slug . ' ';
endforeach;

// echo $brand_string down here somewhere

Leave a Reply

Your email address will not be published. Required fields are marked *