Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce: How to hide content if product is not visible? WordPress

  • SOLVED

In older versions of WooCommerce I was able to use:

---------------------------------------------------------------------------------

<?php $visibility = get_post_meta($post->ID, '_visibility', true); if ($visibility == visible) { ?>

Content displayed if product is visible

<?php } else { ?><!-- else if product is not visible -->

Content displayed if product is not visible

<?php } ?>

---------------------------------------------------------------------------------

But now it should be something else since this is no longer working with new products (https://createandcode.com/fix-broken-featured-products-woocommerce-3-0/), and uses taxonomy terms (and tax_query) instead of custom fields.

What type of conditional statement would I use to achieve the same effect that I used to use?

Answers (3)

2017-08-31

Farid answers:

Hi,

Now, WooCommerce using products visibility as taxonomy. So if you want to list only visible products then you can use the products query something like this:


$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
);

$loop = new WP_Query( $args );


Or if you want to decide for each product individually then you can do it like this:

$product->is_visible(); // in your case $post->is_visible();

So your condition would be something like this:


<?php $visibility = $post->is_visible();

if ( $visibility ) {

<!--Content displayed if product is visible -->

<?php } else { ?>

<!--Content displayed if product is not visible -->

<?php } ?>


Let me know if you still face any issue.

Thanks


Rempty comments:

Hello drinkguesser
Woocommerce changed the visibility from meta to a custom taxonomy, but added new functions to access it easily

If you are using a woocommerce template you can use

$visibility= $product->is_visible();
if($visibility){
//Here if is visible
}
else{
//Here if not visible
}

2017-08-31

Arnav Joy answers:

can you please let me know how you have changed it in new version ??

2017-08-31

Rempty answers:

Hello drinkguesser
Woocommerce changed the visibility from meta to a custom taxonomy, but added new functions to access it easily

If you are using a woocommerce template you can use

$visibility= $product->is_visible();
if($visibility){
//Here if is visible
}
else{
//Here if not visible
}