2

Pretty simple concept... I want to hide all products that have the tag "Wholesale" from everywhere in WooCommerce if the user is not logged in... I have gotten close, but no dice just yet.

What I Have Currently

$product_tags = wp_get_post_terms( $product->id, 'product_tag' );

if ( ! empty( $product_tags ) ) {
    foreach( $product_tags as $tag ) {
        if ( $tag->slug === 'wholesale' && ! is_user_logged_in() ) {
            return;
        }
    }
}

See Gist: https://gist.github.com/DerekFoulk/d94646da9f22d5dddff6

The results of my efforts can be seen on this page: http://gigacord.com/shop/

As you can see from the results, there are holes in the product grid because the row is supposed to have the class .first on product 1/3 (on each row) and .last on item 3/3. Where I am currently "removing" the product is apparently after the logic that counts the items per row and then assigns said classes.

This snippet does not do everything I would like. In a nutshell, I would like to remove the product from the products array as soon as possible (before my theme can start constructing its elements). I would also like to hide all product information when the product page is accessed directly (probably a different question though).

So, is there a WooCommerce hook that runs everywhere products are displayed, and if so, how can I hide the products that have the tag of "Wholesale" using that hook?

7
  • It would be helpful to see your loop...Also, using :first-child and :last-child CSS selectors may allow you to completely get rid of the .first and .last classes
    – rnevius
    Jan 8, 2016 at 19:58
  • @rnevius - Here is a link to my repo: github.com/DerekFoulk/gigacord-wp Jan 8, 2016 at 20:44
  • @rnevius - Agreed, this particular symptom could be remedied by using those selectors. This theme is not the most semantic, and there are definitely some less-than-perfect styles. I will see about updating those selectors as they will most likely solve that particular issue :) Jan 8, 2016 at 20:47
  • 1
    Check this answer I suspect it can be modified to fit your needs. Jan 8, 2016 at 20:53
  • 1
    Ah, so those .first and .last selectors are use to clear the floats for each row. So, the proposed pseudo-selectors would not suffice, however :nth-child(3n) may do the trick :) Jan 8, 2016 at 20:55

2 Answers 2

4

it's better you use pre_get_posts

here...

function rei_exclude_by_product_tag( $query ) {
    if ( $query->is_main_query() && is_woocommerce() && !is_user_logged_in() ) {
        $taxquery = array(
            array(
                    'taxonomy' => 'product_tag',
                    'field' => 'id',
                    'terms' => array( 6 ), // the ID of the product tag
                    'operator'=> 'NOT IN' // exclude
                )
            );

        $query->set('tax_query', $taxquery);
    }
}
add_action( 'pre_get_posts', 'rei_exclude_by_product_tag' );
2
  • 1
    Nicely done. I wasn't sure if you could tell is_user_logged_in() on pre_get_posts, but great! Jan 9, 2016 at 12:37
  • Thank you very much, I will try this tomorrow and report back! Jan 12, 2016 at 1:33
0

Thanks Reigel!! Was looking for this.

Side Note on "Reigel": (for those looking into the Category)

You can swap "product_tag" with "product_cat" too.

Worked for me when targeting my public category.

1
  • I did, but thank you for replying... Jan 6, 2022 at 13:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.