按类别(术语)过滤 Woocommerce $order 项目 [英] Filtering the Woocommerce $order items By Category (Term)

查看:19
本文介绍了按类别(术语)过滤 Woocommerce $order 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被这个问题绕了几天,这让我有点抓狂.我知道我有所有的部分,但我似乎无法将它们组合在一起.

I've been going round in circles with this problem for a few days now and it's starting to drive me a little mad. I know I have all the pieces but I just can't seem to get them to fit together.

我有一个用于 Woocommerce 订单的打印模板,它以正常方式列出产品,它们存储在数组中的顺序又是它们放在篮子中的顺序等.但是,我们希望它们按类别分组(学期).所以这意味着 term_id 17 下的所有项目首先列出,然后所有 term_id 18

I have a printing template for Woocommerce orders that lists the products in the normal way, the order they are stored in the array which in turn is the order they were placed in the basket etc. However, we want them grouped by Category (Term). So this will mean that all items under term_id 17 are listed first, then all term_id 18, etc.

理想情况下,这将是一个自动过程,在该过程中,代码读取所有使用中的条款,然后一次遍历它们并输出篮子中该条款的任何产品.但我离那个阶段还很远.

Ideally, this would be an automatic process where the code reads all of the Terms in use, then steps through them one at a time and outputs any products for that Term that are in the basket. But I'm nowhere near that stage yet.

到目前为止,我使用这段代码取得了部分成功:

So far I have had partial success with this code:

foreach ($order->get_items() as $item) {

     $product_id = $item['product_id'];

     $meta = $item['item_meta'];
     $meta = array_filter($meta, function ($key) {
     return !in_array($key, Order::getHiddenKeys());
}, ARRAY_FILTER_USE_KEY);

$terms = get_the_terms ( $product_id, 'product_cat' );

foreach ( $terms as $term ) {
    $cat_id = $term->term_id;
    
    if($cat_id === 18) {
        var_dump($item['name']);
    }
}

这将成功转储在 term_id 18 中分类的订单项目.但是,如果我像这样修改 IF 语句:

This will successfully dump the order items that are categorised in term_id 18. However, if I modify the IF statement like this:

if($cat_id === 18) {
    var_dump($item['name']);
} elseif($cat_id === 17) {
    var_dump($item['name']);
}

我原以为它会输出 term_id 18 项,然后是 term_id 17 AFTER 项.不幸的是,它只是按照默认顺序显示数组,即 17,然后是 18,尽管代码是以这种方式布局的.

I would have expected it to output the term_id 18 items, then the term_id 17 ones AFTER them. Unfortunately, it simply shows the array in the default order of 17, then 18, despite the fact the code is laid out in this fashion.

我认为它可能以这种方式输出,因为它没有修改原始数组,只是在不同点屏蔽了它的一部分.所以,我一直在试验 array_filter 函数,但我似乎无法正确理解逻辑.我很不好意思发布这个尝试,因为我知道这很糟糕,但这是我对 array_filter 的尝试:

I thought that maybe it was outputting in this way because it's not modifying the original array, simply masking parts of it at different points. So, I have been experimenting with the array_filter function, but I can't seem the get the logic right. I'm pretty embarrassed to post this attempt as I know it's awful, but this is as far as I've gotten experimenting with array_filter:

function test ($var) {
    foreach ( $terms as $term ) {
        $cat_id = $term->term_id;
    
        if($cat_id === 18) {
            print_r($item['name']);
        }
    }
    return $var;
}
    
foreach ($order->get_items() as $item) {

    $product_id = $item['product_id'];

    $meta = $item['item_meta'];
    $meta = array_filter($meta, function ($key) {
    return !in_array($key, Order::getHiddenKeys());
}, ARRAY_FILTER_USE_KEY);
    
$terms = get_the_terms ( $product_id, 'product_cat' );

print_r(array_filter($terms, "test"));

我知道这是大错特错,但我就是无法理解.我对基本的 PHP 和 Wordpress PHP 使用正常,但 Woocommerce 要复杂得多!我非常感谢您对此的任何帮助.

I know it's significantly wrong, but I just can't get my head around it. I'm fine with basic PHP and Wordpress PHP usage normally, but Woocommerce is so much more complicated! I'd really appreciate any help with this please.

简单回顾一下,我希望产品按类别顺序打印/回显/等(如果有人觉得超级慷慨,则自动执行,哈哈).我真的不介意这是如何实现的,只要它显然是安全可靠的.

Just to recap, I want the products in the order printed / echoed / etc in Category Order (automatically if anyone feels super generous lol). I don't really mind how this is achieved so long as it's safe and secure obviously.

对于长篇文章深表歉意,并提前致谢.

Apologies for the long post and huge thanks in advance.

推荐答案

在我看来,您似乎在循环订单,但您并没有循环您想要对其进行排序的类别.

It looks to me like you're looping the orders, but you're not looping the categories you want to be ordering them.

我没有运行过这段代码,但我认为该模式应该按照您的意愿行事.

I've not run this code, but the pattern should do what you want I think.

// Group items into Categories - If you have a custom order you wish, pre-populate this array, or you can sort after
$categories = [];
foreach ($order->get_items() as $item) {
    $product_id = $item['product_id'];
    $terms = get_the_terms($product_id, 'product_cat');
    $cat_id = $terms[0]->term_id;
    $categories[$cat_id][] = $item;
}

// Loop Categories
foreach($categories as $category => $items){
    echo sprintf("<h1>%s</h1>", $category);
    //Loop Items in Category
    foreach ($items as $item) {
        print_r($item['name']);
    }
}

这篇关于按类别(术语)过滤 Woocommerce $order 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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