Seb G
Seb G

Reputation: 671

Get product visibility value - WooCommerce

I need to check the visibility status of a product by it's id. NOT just if it is visible or not, but if it's search, hidden, or visible.

I have tried get_post_meta($id, '_visibility', true);, however only my older products have that post meta in the database so I'm assuming it is no longer used.

The overall reason for this is because I'm trying to display products with the product shortcode, but there isn't and option to "show all" within visibility. for example,

echo do_shortcode('[products ids="' . $id . '" visibility="all"]');

So now I have have to check the visibility of the product and echo out the appropriate shortcode to show it. Unless I am mistaken and there is a way to show products with the shortcode no matter what their visibility is...

Upvotes: 1

Views: 6218

Answers (2)

LovingGarlic
LovingGarlic

Reputation: 185

In the other answer, get_product() is deprecated as of Version 3. You should use wc_get_product() if using a version >= 3.0.0.

Upvotes: 5

Seb G
Seb G

Reputation: 671

SOLUTION

Whoops, totally missed this: get_catalog_visibility();

I just had to get the product using its id and then use the function above.

Example:

$product = get_product($id);
$product_visibility = $product->get_catalog_visibility();

Upvotes: 5

Related Questions