function woocommerce_output_related_products() {
$args = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'rand', // @codingStandardsIgnoreLine.
'post__not_in' => array(502,281)
);
woocommerce_related_products( apply_filters( 'woocommerce_output_related_products_args', $args ) );
}
我将此函数从 includes/wc-template-functions.php 复制到我的主题的functions.php
I copied this function from includes/wc-template-functions.phpinto my theme's functions.php
为了验证我的更改是否有效,我将 posts_per_page 更改为 3,它只查询 3 而不是 4.
To verify that my changes would work I changed the posts_per_page to 3 and it queried only 3 instead of 4.
我需要排除一些产品,但 post__not_in 不起作用.
I need to exclude a few products, but post__not_in is not working.
我做错了吗?我还能如何排除使用此功能的产品?
Am I doing something wrong? How else can I exclude products using this function?
我用这个函数输出产品:woocommerce_output_related_products();
I'm outputting the products with this function: woocommerce_output_related_products();
好讨厌的问题.我根本无法从这里排除产品.有人可以帮忙吗?
such an obnoxious problem. I simply cannot exclude products from here. can anyone help?
我也试过这个:
add_filter( 'woocommerce_output_related_products_args', function( $args ) {
$args = wp_parse_args( array( "post__not_in" => array('502','281') ), $args );
return $args;
});
我做了 print_r($args) 并且它显示我的post__not_in"被添加了,但产品仍然存在.我有正确的身份证.
i did print_r($args) and it showed that my "post__not_in" was being added, but the products are still there. I have the right ID.
改用 woocommerce_related_products 过滤器钩子,这样:
Use the woocommerce_related_products filter hook instead, this way:
add_filter( 'woocommerce_related_products', 'exclude_related_products', 10, 3 );
function exclude_related_products( $related_posts, $product_id, $args ){
// HERE set your product IDs to exclude
$exclude_ids = array('502','281');
return array_diff( $related_posts, $exclude_ids );
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
这篇关于在 Woocommerce 中排除相关产品 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
以编程方式将可下载文件添加到 Woocommerce 产品Add programmatically a downloadable file to Woocommerce products(以编程方式将可下载文件添加到 Woocommerce 产品)
获取今天 Woocommerce 中每种产品的总订单数Get today#39;s total orders count for each product in Woocommerce(获取今天 Woocommerce 中每种产品的总订单数)
在 WooCommerce 和电话字段验证问题中添加自定义注Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和电话字段验证问题中添加自定义注册字段
在 Woocommerce 简单产品中添加一个将更改价格的选Add a select field that will change price in Woocommerce simple products(在 Woocommerce 简单产品中添加一个将更改价格的选择字段)
在 WooCommerce 3 中将自定义列添加到管理产品列表Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中将自定义列添加到管理产品列表)
自定义结帐“下订单"按钮输出htmlCustomizing checkout quot;Place Orderquot; button output html(自定义结帐“下订单按钮输出html)