gpt4 book ai didi

php - 在 Woocommerce 中检索产品分类的 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 03:01:37 25 4
gpt4 key购买 nike

在 Woocommerce 中,我有大约 8 个不同类别的许多产品。当用户购买商品时,发送给他们的电子邮件会按照他们将产品添加到购物车的顺序列出产品,这看起来非常困惑。我想按产品类别订购产品,并在每个类别中按字母顺序列出产品。我认为这种“双重排序”功能最好通过修改在发送电子邮件之前调用的 SQL 查询来实现,如下所示:

$line_items = $wpdb->get_results( $wpdb->prepare( "
SELECT order_item_id, order_item_name, order_item_type
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_id = %d
AND order_item_type IN ( '" . implode( "','", $type ) . "' )
ORDER BY order_item_id
//Would like this to be ORDER BY 'product category', 'product name' ASC//
", $this->id ) );

我想不通的是如何在此查询中检索产品类别。我知道它需要一个 JOIN,但对于我来说,我无法追踪数据库表中的关键关系。

更新:woocommerce 产品存储为具有常规 WordPress 分类法的帖子,因此如果有人知道如何在 WordPress 中检索帖子的类别,我认为这会起作用。

最佳答案

您可以使用以下代码,但这仅在您的每个产品仅属于一个类别时才有用

$line_items = $wpdb->get_results( $wpdb->prepare( "
SELECT DISTINCT woi.order_item_id, woi.order_item_name, woi.order_item_type, woim.meta_value AS product_id, t.name AS product_category
FROM {$wpdb->prefix}woocommerce_order_items AS woi
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim
ON ( woi.order_item_id = woim.order_item_id AND woim.meta_key LIKE '_product_id' )
LEFT JOIN {$wpdb->prefix}term_relationships AS tr
ON ( tr.object_id = woim.meta_value )
LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt ON ( tt.term_taxonomy_id = tr.term_taxonomy_id )
LEFT JOIN {$wpdb->prefix}terms AS t ON ( t.term_id = tt.term_id )
WHERE woi.order_id = %d
AND woi.order_item_type IN ( '" . implode( "','", $type ) . "' )
AND tt.taxonomy LIKE 'product_cat'
ORDER BY product_category, woi.order_item_name
", $this->id ) );

希望这会有用。

关于php - 在 Woocommerce 中检索产品分类的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22820884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com