Created
May 5, 2019 01:58
-
-
Save felipe-pita/ac4988e6d4c617e7915269c9f301f0c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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> | |
"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ 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