Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 638 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hide out of stock related products in WooCommerce

#1
In WooCommerce I would like to hide *Out of Stock* products from **Related products** in single product pages. Is it possible?

Any track is appreciated.

Reply

#2
create a function and hook it to related products hook of woocommerce like:

function dont_show_outofstock( $is_visible, $id ) {
$product = new wC_Product( $id );

if ( ! $product->is_in_stock() && ! $product->backorders_allowed() ) {
$is_visible = false;
}

return $is_visible;
}
add_filter( 'woocommerce_output_related_products_args', 'dont_show_outofstock', 10, 2 );
Reply

#3
Yes it's possible to hide out of stock products from related products.

Add the below to functions.php – this will hide out of stock products from related products.

add_filter( 'woocommerce_output_related_products_args', function( $args )
{
$args = wp_parse_args( array(
'posts_per_page' => 4,
'meta_query' => array (
'key' => '_stock_status',
'value' => 'instock'
)
), $args );
return $args;
});

The posts per page line can be removed, but its useful as a quick of visualising that this has worked on your related products block.
Reply

#4
None of the answers given here worked for me (I believe the `woocommerce_output_related_products_args` filter mentioned does not accept meta_queries), and I wanted a solution that didn't use an SQL query, so I put together the solution below:

add_filter( 'woocommerce_related_products', 'mysite_filter_related_products', 10, 1 );
function mysite_filter_related_products( $related_product_ids ) {

foreach( $related_product_ids as $key => $value ) {
$relatedProduct = wc_get_product( $value );
if( ! $relatedProduct->is_in_stock() ) {
unset( $related_product_ids["$key"] );
}
}

return $related_product_ids;
}

Hope that helps someone looking for a similar solution.
Reply

#5
**UPDATE 2021**

You can use the following:
```
add_filter( 'woocommerce_product_related_posts_query', 'alter_product_related_posts_query', 10, 3 );
function alter_product_related_posts_query( $query, $product_id, $args ){
global $wpdb;

$query['join'] .= " INNER JOIN {$wpdb->postmeta} as pm ON p.ID = pm.post_id ";
$query['where'] .= " AND pm.meta_key = '_stock_status' AND meta_value = 'instock' ";

return $query;
}
```
Code goes in functions.php file of your active child theme (or active theme).

>Now **we need to remove "related products" cached data** deleting the related transients to flush this cache *(thanks to [@Cody Rees][1])*.

**There is 2 ways to do it:**

1). The **easiest** way:

Go to admin Woocommerce > Status > Tools > WooCommerce transients and press on "Clear transcients".

2). The other way targeting specific related transients to be deleted:

Add the following code and save:
```
add_action('init', 'delete_related_products_cached_data');
function delete_related_products_cached_data() {
global $wpdb;

$wpdb->query("DELETE FROM {$wpdb->prefix}options WHERE `option_name` LIKE '_transient_wc_related_%'");
}
```
Code goes in functions.php file of your active child theme (or active theme).

**Run it only once** by browsing any page of your web site and remove it.

[1]:

[To see links please register here]

Reply

#6
This is working code from here. Add this code to your functions.php and you will see that our of stocks products will not be seen in related product block. Code is from here :

[To see links please register here]


add_filter( 'woocommerce_related_products', 'mysite_filter_related_products', 10, 1 );
function mysite_filter_related_products( $related_product_ids ) {

foreach( $related_product_ids as $key => $value ) {
$relatedProduct = wc_get_product( $value );
if( ! $relatedProduct->is_in_stock() ) {
unset( $related_product_ids["$key"] );
}
}

return $related_product_ids;
}
Reply

#7
For those who didn't find solution:
Tested on Woocommerce +6

add_filter( 'woocommerce_related_products', 'vahids_related_products', 10, 3 );
function vahids_related_products( $related_posts, $product_id, $args ){
$in_stock_product_ids = (array) wc_get_products( array(
'status' => 'publish',
'limit' => -1,
'stock_status' => 'instock',
'return' => 'ids',
));

return $in_stock_product_ids;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through