我正在尝试过滤woocommerce中“相关产品”中显示的产品,以便它只输出库存中的相关产品。
我试过编辑"related.php“模板,如下所示--但它不起作用!
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => $related,
'post__not_in' => array( $product->id ),
'meta-key' => '_stock_status',
'meta-value' => 'outofstock',
'compare' => '!='
) );如果能帮上忙,我将不胜感激
非常感谢
发布于 2020-04-02 02:51:43
这里给出的答案对我都不起作用(我相信提到的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;
}希望这对正在寻找类似解决方案的人有所帮助。
发布于 2015-11-06 17:03:13
我想你已经完成了这个任务,但是如果任何人有这个问题,那么他们可以使用这个代码在相关产品上只显示“现货”产品,更改为related.php
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => $related,
'post__not_in' => array( $product->id ),
'meta_key' => '_stock_status',
'meta_value' => 'instock',
'compare' => '!='
)
);https://stackoverflow.com/questions/29352698
复制相似问题