在 Woocommerce 中排除相关产品 ID

2023-01-16php开发问题
8

本文介绍了在 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"被添加了,但产品仍然存在.我有正确的身份证.

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17