Skip to content

Instantly share code, notes, and snippets.

@WillBrubaker
Created December 6, 2018 14:19
Show Gist options
  • Save WillBrubaker/17d3b0407d1ae3dec77633f3b2c113d8 to your computer and use it in GitHub Desktop.
Save WillBrubaker/17d3b0407d1ae3dec77633f3b2c113d8 to your computer and use it in GitHub Desktop.
Remove add to cart button from products that do not have stock
//not sure what to do with this code snippet? See https://www.thathandsomebeardedguy.com/what-do-i-do-with-these-code-snippets/
add_filter( 'woocommerce_loop_add_to_cart_link', 'handsome_bearded_guy_maybe_remove_add_to_cart_button' );
function handsome_bearded_guy_maybe_remove_add_to_cart_button( $html ) {
global $product;
if ( 'simple' === $product->get_type() && ! $product->get_stock_quantity() ) {
return null;
}
return $html;
}
@rich88812
Copy link

Hi,

I tried your code on a site based on Elementor.

I change the return type to variable as I am using variable products - wanting to remove them if now stock and have a email subscribe notification instead.

//not sure what to do with this code snippet? See https://www.thathandsomebeardedguy.com/what-do-i-do-with-these-code-snippets/
add_filter( 'woocommerce_loop_add_to_cart_link', 'handsome_bearded_guy_maybe_remove_add_to_cart_button' );

function handsome_bearded_guy_maybe_remove_add_to_cart_button( $html ) {
global $product;
if ( 'variable' === $product->get_type() && ! $product->get_stock_quantity() ) {
return null;
}
return $html;
}

It's in functions.php. Any ideas? Site page is: https://www.zonev.com/product/samsung-galaxy-a10-with-zone-v/

Thanks in advance.

@WillBrubaker
Copy link
Author

My only guess would be that the parent product may actually be returning something that evaluates to true from get_stock_quantity while it is the variation that is out of stock.

might want to enable WP_DEBUG and add some logging statements to see what's what.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment