gpt4 book ai didi

php - Woocommerce 选择与多个元值匹配的订单

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

我目前正在开发一个插件,该插件是为一家面包店构建的,用于通过电子邮件发送有关订单的报告。此后他们又开设了另一家商店,现在需要根据商店位置将报告分成单独的电子邮件。

我有以下 SQL 查询:

$sql = "SELECT o.order_item_id, o.order_item_name, o.order_id, po.post_date AS order_date_placed
FROM wp_woocommerce_order_items AS o
LEFT JOIN wp_woocommerce_order_itemmeta AS oi ON o.order_item_id = oi.order_item_id
LEFT JOIN wp_posts AS po ON po.ID = o.order_id
WHERE oi.meta_key='Collection Date'
AND oi.meta_value IN ('16/01/2016','17/01/2016','18/01/2016','19/01/2016','20/01/2016','21/01/2016')
ORDER BY po.post_date ASC, o.order_item_id ASC";

此查询返回特定日期范围内要收集的所有订单。

array (size=3)
0 =>
object(stdClass)[593]
public 'order_item_id' => string '19582' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15234' (length=5)
public 'order_date_placed' => string '2016-01-14 08:51:31' (length=19)
1 =>
object(stdClass)[592]
public 'order_item_id' => string '19600' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15248' (length=5)
public 'order_date_placed' => string '2016-01-14 15:43:08' (length=19)
2 =>
object(stdClass)[591]
public 'order_item_id' => string '19603' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15250' (length=5)
public 'order_date_placed' => string '2016-01-14 15:45:25' (length=19)

我现在想传递一个附加条件,该条件也将返回数据限制到特定的商店位置。

我尝试将以下内容添加到查询中,但这返回 null。

WHERE oi.meta_key = 'Pickup Location' 
AND oi.meta_value LIKE '%Address 1%'

只有同时满足两个条件的记录才必须返回。

我确实很难理解为什么 wordpress/woocommerce 中的元表是这样设置的,并且有这么多冗余/重复的条目。

我不确定开发人员是否应该使用原始 SQL 查询,或者是否有更好的解决方案来解决这个问题。

非常感谢任何帮助。

最佳答案

我猜您正在寻找此查询

SELECT o.order_item_id, 
o.order_item_name,
o.order_id,
po.post_date AS order_date_placed
FROM wp_woocommerce_order_items AS o
LEFT JOIN wp_woocommerce_order_itemmeta AS oi
ON ( o.order_item_id = oi.order_item_id
AND oi.meta_key = 'Collection Date'
AND oi.meta_value IN ( '16/01/2016', '17/01/2016','18/01/2016',
'19/01/2016','20/01/2016', '21/01/2016' ) )
OR ( oi.meta_key = 'Pickup Location'
AND oi.meta_value LIKE '%Address 1%' )
LEFT JOIN wp_posts AS po
ON po.id = o.order_id
ORDER BY po.post_date ASC,
o.order_item_id ASC

并将过滤器移至ON条件,因为这些条件属于右表

关于php - Woocommerce 选择与多个元值匹配的订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34805391/

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