[英]Woocommerce Select Orders which match multiple meta values
I'm currently working on a plugin which was built for a bakery to email reports about orders. 我目前正在开发一个为面包店构建的插件,用于通过电子邮件发送有关订单的报告。 They have since opened another store and now the reports need to be split into separate emails depending on store location.
从那以后,他们就开了另一家商店,现在,根据商店的位置,需要将报告分成单独的电子邮件。
I have the following sql query: 我有以下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";
This query returns all orders which are to be collected within a specific date range. 该查询返回在特定日期范围内要收集的所有订单。
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)
I now want to pass an additional criteria that will also limit the return data to a specific store location. 我现在想通过一个附加条件,该条件还将返回数据限制为特定的存储位置。
I have tried adding the following to the query but this returns null. 我尝试将以下内容添加到查询中,但这将返回null。
WHERE oi.meta_key = 'Pickup Location'
AND oi.meta_value LIKE '%Address 1%'
Only records which are true for both criteria must return. 只有两个条件都正确的记录才必须返回。
I do battle to understand why the meta table in wordpress/woocommerce are set up the way they are with so many redundant/duplicate entries. 我为了解为什么wordpress / woocommerce中的元表以如此多的冗余/重复条目的方式设置而作了努力。
I'm not sure whether the developer should have used a raw SQL query or whether there is a better solution for this problem. 我不确定开发人员是否应该使用原始SQL查询,或者是否有针对此问题的更好解决方案。
Any help is much appreciated. 任何帮助深表感谢。
I guess you are looking for this query 我猜你在找这个查询
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
And move the filter to ON
conditions since those belongs to right table 并将过滤器移至
ON
条件,因为它们属于右表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.