在Woocommerce中排除相关产品ID [英] Exclude related products ids in Woocommerce

查看:164
本文介绍了在Woocommerce中排除相关产品ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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",但产品仍然存在.我有正确的ID.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆