[英]Hide out of stock related products in WooCommerce
In WooCommerce I would like to hide Out of Stock products from Related products in single product pages.在 WooCommerce 中,我想在单个产品页面中隐藏相关产品中的缺货产品。 Is it possible?
可能吗?
Any track is appreciated.任何曲目表示赞赏。
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:这里给出的答案都不适合我(我相信提到的
woocommerce_output_related_products_args
过滤器不接受 meta_queries),我想要一个不使用 SQL 查询的解决方案,所以我将解决方案放在下面:
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.希望对寻找类似解决方案的人有所帮助。
UPDATE 2021 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).代码在您的活动子主题(或活动主题)的functions.php 文件中。
Now we need to remove "related products" cached data deleting the related transients to flush this cache (thanks to @Cody Rees ) .
现在我们需要删除“相关产品”缓存数据,删除相关瞬态以刷新此缓存(感谢@Cody Rees ) 。
There is 2 ways to do it:有两种方法可以做到:
1). 1)。 The easiest way:
最简单的方法:
Go to admin Woocommerce > Status > Tools > WooCommerce transients and press on "Clear transcients".转到管理员 Woocommerce > 状态 > 工具 > WooCommerce 瞬态,然后按“清除 transcients”。
2). 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).代码在您的活动子主题(或活动主题)的functions.php 文件中。
Run it only once by browsing any page of your web site and remove it.通过浏览您网站的任何页面只运行一次并将其删除。
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.将以下内容添加到functions.php - 这将从相关产品中隐藏缺货产品。
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.可以删除每页行的帖子,但它有助于快速可视化这对您的相关产品块有效。
For those who didn't find solution: Tested on Woocommerce +6对于那些没有找到解决方案的人:在 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;
}
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.
将此代码添加到您的functions.php,您将看到我们的库存产品不会出现在相关产品块中。 Code is from here : https://stackoverflow.com/a/60978253/15213069
代码来自这里: https ://stackoverflow.com/a/60978253/15213069
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;
}
create a function and hook it to related products hook of woocommerce like:创建一个函数并将其挂钩到 woocommerce 的相关产品挂钩,例如:
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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.