In one of our apps about Staging WordPress Sites - qSandbox we needed to have a Maintenance plan.
This plan would allow people to keep their subscription active so their sites won't get deleted.
We might have to use that same idea for the other products as well.

We didn't want that extra plan to always show up because people might choose it and get confused by it.
Also the pricing section would become too large if we have yet another plan.

WooCommerce has a great number of filters that can be used to hide a specific variation.
We are checking for a specific SKU because each product runs on live & staging servers.
For this reason if we were to check for a specific ID that would make things confusing.
Because the variations will most likely be different on each server.
Another issue might be an that let's say an variation with id 100 should be hidden.
On the live system that's the correct variation id, but on the staging site(s) a variation with id 100 maybe one of the important ones that should always be displayed.

That's the reason behind using a SKU number to check if it's the right variation to hide.

We made it so when the product page is opened and has a given parameter
then the hidden plan will show up.

?app_pricing_show_hidden

The idea is for the plan to show only when necessary.

We are using 'woocommerce_variation_is_visible' filter to show or hide a specific variation.
WooCommerce passes 4 parameters to this filter which is great.

The 1st parameter is if WooCommerce "thinks" the variation should be visible or hidden.
this involves checking if it has a price set and a few more.

2nd parameter is its id
3nd parameter is its parent id
4th parameter is the most important one. It's an instance of WC_Product_Variation.

By using the WC_Product_Variation instance we can ask for the sku number.

Installation

You need to add this block of code to your functions.php file or even better into a custom plugin that you store important snippets.
Having the code in the functions.php file would work for the moment but when you switch to a different theme, you have to remember to copy all the custom code you put into it.

One our case the sku contain the keyword: maintenance.
We're doing case insensitive check because our SKU numbers are usually uppercase.
Do make sure you change the keyword in the stripos() check.

add_filter( 'woocommerce_variation_is_visible', 'orbisius_blog6648_maybe_toggle_variation_visibility', 10, 4 );

/**
 * We'll normally skip the maintenance plan unless: it's admin, app_pricing_maintenance=1 is passed.
 * if app_pricing_maintenance is we'll skip the other skus and leave the maintenance plan?
 * @param bool $visible
 * @param int $id
 * @param int $parent_id
 * @param WC_Product_Variation $obj
 * @see https://orbisius.com/6648
 * @author <a href='https://orbisius.com/?utm_source=tutorial_snippet&utm_medium=post6648' target='_blank'>https://orbisius.com</a>
 */
function orbisius_blog6648_maybe_toggle_variation_visibility($visible, $id, $parent_id, $obj) {
	$sku = $obj->get_sku();

	if (!isset($_REQUEST['app_pricing_show_hidden']) && (stripos($sku, 'maintenance') !== false)) {
		return false;
	}

	return $visible;
}

Disclaimer: The content in this post is for educational purposes only. Always remember to take a backup before doing any of the suggested steps just to be on the safe side.
Do you need any of the code in this post customized or packaged into a plugin? Get a free quote
Referral Note: When you purchase through an referral link (if any) on our site, we earn a commission.