Documentation

Programatically assign variation visibility

If you’re looking to assign the variaton visibility in your store and don’t want to do it on a per-product basis, you can do so with the code on this page.

WooCommerce stores the visibility in a hidden taxonomy named product_visibility. The possible terms of this taxonomy are:

  • exclude-from-catalog
  • exclude-from-filtered
  • exclude-from-search
  • featured
  • outofstock
  • rated-1
  • rated-2
  • rated-3
  • rated-4
  • rated-5

Show Single Variations makes use of only the first 4: exclude-from-catalog, exclude-from-filtered, exclude-from-search, and featured.

You can programatically assign any of these terms with a code snippet like this:

$variation_id = 4053;
$append       = false;
$terms        = array( 'exclude-from-catalog' );
wp_set_object_terms( $variation_id, $terms, 'product_visibility', $append );

You would also need to change post meta _visibility. This is used to set the value of these checkboxes:

$visibility = array( 'catalog', 'filtered', 'search' );
update_post_meta( $variation_id, '_visibility', $visibility );

Example

To show a variation in catalog, search and filtered results, use this code:

$variation_id = 4053;
$append       = false; // Replace terms, don't append.
$terms        = array();
wp_set_object_terms( $variation_id, $terms, 'product_visibility', $append );

$visibility = array( 'catalog', 'filtered', 'search' );
update_post_meta( $variation_id, '_visibility', $visibility );

To hide a variation from catalog, search and filtered results, use this code:

$variation_id = 4053;
$append       = false; // Replace terms, don't append.
$terms        = array( 'exclude-from-catalog', 'exclude-from-filtered', 'exclude-from-search' );
wp_set_object_terms( $variation_id, $terms, 'product_visibility', $append );

$visibility = array();
update_post_meta( $variation_id, '_visibility', $visibility );

Are you using WooCommerce Show Single Variations yet?

Start your 14-day free trial right now and get instant access to the features described in this article.

or learn more

Was this helpful?

Please let us know if this article was useful. It is the best way to ensure our documentation is as helpful as possible.

WooCommerce Show Single Variations

WooCommerce Show Single Variations

Not got the plugin yet? Get instant access right now, free for 14 days.

or learn more

Still need help?

If you haven't found what you're looking for in our documentation, please contact support.

Get Support