Skip to content

Instantly share code, notes, and snippets.

@felipe-pita
Created May 5, 2019 01:58
Show Gist options
  • Save felipe-pita/ac4988e6d4c617e7915269c9f301f0c4 to your computer and use it in GitHub Desktop.
Save felipe-pita/ac4988e6d4c617e7915269c9f301f0c4 to your computer and use it in GitHub Desktop.
/**
* Remove produtos que a variação não tem em estoque
* @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_tamanho'])) {
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_tamanho'])) {
if ($variation['attributes']['attribute_pa_tamanho'] == $_GET['filter_tamanho'] && $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>
";
}
}
}
}
@KoolPal
Copy link

KoolPal commented Feb 5, 2020

@ felipe-pita Excellent code. Works amazing,
One issue found is that if I select the Query Type as OR and I select more than 1 attribute, the page goes blank.

OR – If a user selects two attributes, products which match either attribute should be returned

Reference: https://docs.woocommerce.com/document/woocommerce-widgets/#section-6

Can you please review and help with some tweaks to your code above?

Thanks

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