I'll be direct : by default, you can't. It's not related to a certain theme or plugin, but to Woocommerce itself. This is a problem that exists in woocommerce for a very long time. It's not possible by default in woocommerce to handle variable products stock visibility (stock status) based on it's variations visibility as it's something necessary and needed by Woocommerce1.
A way of doing this is to add this action function :
add_action('woocommerce_before_shop_loop_item', 'out_of_stock_variations_loop');
function out_of_stock_variations_loop()
{
global $product;
$filter = 'size';
if ($product->product_type === 'variable') {
$available = $product->get_available_variations();
if ($available) {
foreach ($available as $instockvar) {
if (isset($instockvar[ 'attributes' ][ 'attribute_pa_' . $filter ])) {
if ($_GET[ 'filter_' . $filter ]) {
if ( !in_array( $instockvar[ 'attributes' ][ 'attribute_pa_' . $filter ], explode(',', $_GET[ 'filter_' . $filter ]) , true ) || ($instockvar[ 'max_qty' ] <= 0) ) {
echo "<style>.post-" . $product->get_id() . " {display: none}</style>";
} else {
echo "<style>.post-" . $product->get_id() . " {display: list-item !important}</style>";
}
}
}
}
}
}
}
?>
Which will only show the list of products in stock. The problem with this is that it will leave blank spaces where the not-in-stock products are deleted by this query, and it can be difficult to circumvent this problem only with css because, on each row, first and last products havefirst
andlast
classes that determines their layout in CSS. To overcome this, add this jQuery to your child-theme js script:
function openLayeredNavFilterIfSelected() {
if (jQuery('.wc-layered-nav-term').hasClass('woocommerce-widget-layered-nav-list__item--chosen')) {
/*keep layered nav filter open, if at least an attribute is selected*/
jQuery('.woocommerce-widget-layered-nav-list__item--chosen').parent().parent().show();
if (!jQuery('.filter-icon').hasClass('arrow_up')) {
jQuery('.filter-icon').addClass('arrow_up');
}
/*redistribute products rows to avoid missing spaces*/
jQuery('.site-main ul.products.columns-3 li.product').removeClass('first').removeClass('last');
jQuery('.site-main ul.products.columns-3 li.product:visible').each(function(i) {
if (i % 3 == 0) jQuery(this).addClass('first'); //add class first to firsts products of rows
if (i % 3 == 2) jQuery(this).addClass('last'); //add class last to lasts products of rows
});
}
}
openLayeredNavFilterIfSelected();
jQuery(window).resize(openLayeredNavFilterIfSelected);
And you should be good to go.
1 WOOF Products Filter plugin and out of stock variations issue in Woocommerce
Related :
- filter products by attribute and hide out of stock items of variable products
- https://wordpress.org/support/topic/hide-out-of-stock-variations-when-filtering/#post-7718128
- https://xtemos.com/forums/topic/filter-attributes-dont-show-out-of-stock-variations/
- https://wordpress.org/support/topic/exclude-out-of-stock-products-from-filter/
Not a really good answer here.
As @Islam Elshobokshy said, it's a bit complex to edit the product loop query to remove out of stock variations.
Still, you can trick using CSS to "hide" out of stock variations from the list. But it may "break" your product list/grid: if there is enough product for 2 pages (10 items/page), but one product is hidden on page 1 by this filter, page 1 will only have 9 products. We are after the product query.
I quickly tested and wrote something inspired from this:
size
attribute (it could be adapted to handle all attributes from Woocommerce, or URL parameters "filter_*", or hardcoded for every attribute used)Code:
add_action('woocommerce_before_shop_loop_item', 'out_of_stock_variations_loop');
function out_of_stock_variations_loop()
{
global $product;
$filter = 'size'; //to edit
if ($product->product_type === 'variable') {
$available = $product->get_available_variations();
if ($available) {
foreach ($available as $instockvar) {
if (isset($instockvar[ 'attributes' ][ 'attribute_pa_' . $filter ])) {
if (($instockvar[ 'attributes' ][ 'attribute_pa_' . $filter ] === $_GET[ 'filter_' . $filter ]) && ($instockvar[ 'max_qty' ] <= 0)) {
//sadly echo some inline CSS :(
echo "<style>.post-" . $product->get_id() . " {display: none}</style>";
//some tests using is_visible() filters, but we are too late here
//add_filter( 'woocommerce_product_is_visible', function($visible, $id) use ($the_id) {return $id === $the_id ? false : $visible;}, 50, 2);
}
}
}
}
}
}
To achieve the same without inline CSS, you can:
/woocommerce/template/content-product.php
toyour-theme/woocommerce/content-product.php
method($product)
, edit it a bit to return true/false only for required productsif ( empty( $product ) || ! $product->is_visible() || !$method($product) )
In the end, updating the loop query to match your need may be complex, and negatively impact performance. But, to explore more, I would start something like:
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.