Skip to content

Instantly share code, notes, and snippets.

@KoolPal
Forked from felipe-pita/functions.php
Last active February 5, 2020 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KoolPal/04743eac123b60fc4d57aab97af62018 to your computer and use it in GitHub Desktop.
Save KoolPal/04743eac123b60fc4d57aab97af62018 to your computer and use it in GitHub Desktop.
Woocommerce - Remove products from Filter where the variation is out of stock - Replace size with your variation attribute slug
/**
* Remove produtos que a variação não tem em estoque
* Removes products from Filter where the variation is out of stock
* @see https://github.com/woocommerce/woocommerce/issues/20689
*/
add_action( 'woocommerce_before_shop_loop_item_title', 'remove_out_of_stock_products_from_active_filter' );
function remove_out_of_stock_products_from_active_filter(){
if (isset($_GET['filter_size'])) {
global $product;
if ($product->is_type('variable')) {
$variations = $product->get_available_variations();
$is_available = false;
foreach ($variations as $variation) {
if (isset($variation['attributes']['attribute_pa_size'])) {
if ($variation['attributes']['attribute_pa_size'] == $_GET['filter_size'] && $variation['is_in_stock']){
$is_available = true;
}
}
}
if (!$is_available) {
global $product;
$id = $product->get_id();
echo "
<style>
.woocommerce-result-count { visibility: hidden }
.post-$id { display: none !important }
.woocommerce-pagination { display: none !important }
</style>
";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment