sql 获取WooCommerce按产品/邮政ID或产品SKU获取订单ID

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 获取WooCommerce按产品/邮政ID或产品SKU获取订单ID相关的知识,希望对你有一定的参考价值。

SELECT p.ID AS order_id,
       p.post_date AS order_date,
       woi.order_item_name AS product_title,
       woim.meta_key
FROM wp_posts AS p
INNER JOIN wp_woocommerce_order_items AS woi ON p.ID = woi.order_id
INNER JOIN wp_woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_type = 'shop_order'
  AND p.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
  AND woim.meta_key = '_product_id'
  AND woim.meta_value = '37'  -- Replace it with your Product/Post ID
  AND woi.order_item_type = 'line_item'
  AND DATE(p.post_date) BETWEEN '2016-09-17' AND '2017-01-15';
  
  --   ----------------------------------------------------
  --   ----------------------------------------------------
  
  
  SELECT p.ID AS order_id,
       p.post_date AS order_date,
       woi.order_item_name AS product_title,
       woim.meta_key
FROM wp_posts AS p
INNER JOIN wp_woocommerce_order_items AS woi ON p.ID = woi.order_id
INNER JOIN wp_woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_type = 'shop_order'
  AND p.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold')
  AND woim.meta_key = '_product_id'
  AND woim.meta_value =
    (SELECT pro.ID
     FROM wp_posts AS pro
     INNER JOIN wp_postmeta AS prom ON pro.ID = prom.post_id
     WHERE pro.post_type = 'product'
       AND pro.post_status = 'publish'
       AND prom.meta_key = '_sku'
       AND prom.meta_value = 'some_SKU' -- Replace it with your SKU
     LIMIT 1)
  AND woi.order_item_type = 'line_item'
  AND DATE(p.post_date) BETWEEN '2016-09-17' AND '2017-01-15';

以上是关于sql 获取WooCommerce按产品/邮政ID或产品SKU获取订单ID的主要内容,如果未能解决你的问题,请参考以下文章

如何按ID排序不随机化Woocommerce的相关产品

使用 WooCommerce 在简码中按产品类别获取产品

Woocommerce 使用产品 SKU 获取产品 ID

在 Woocommerce REST API 中按类别获取产品

woocommerce 中的地理围栏产品

sql 获取WooCommerce产品类型