Skip to content

Instantly share code, notes, and snippets.

@NickGreen
Last active December 22, 2017 22:54
Show Gist options
  • Save NickGreen/961a71c6f422d09b58749b198742e211 to your computer and use it in GitHub Desktop.
Save NickGreen/961a71c6f422d09b58749b198742e211 to your computer and use it in GitHub Desktop.
Hide products that have no stock, but still allow backorders.
<?php
/*
* Use case: Product has 'allow backorders' enabled to allow purchase
* of products with stock fewer than 1, but they want them to be purchasable
* if direct linked to or added to the cart in other ways.
*/
function pp_always_hide_zero_stock( $is_visible, $id ) {
$product = wc_get_prodcut( $id );
if ( $product->stock < 1 ) {
$is_visible = false;
}
return $is_visible;
}
add_filter( 'woocommerce_product_is_visible', 'pp_always_hide_zero_stock', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment