gpt4 book ai didi

mysql - 在 WooCommerce 中列出带有订单详细信息的优惠券

转载 作者:搜寻专家 更新时间:2023-10-30 20:23:12 25 4
gpt4 key购买 nike

我有一个有 1000 张优惠券的网站。所有优惠券的使用限额为一张。我正在使用 Raunuk Gupta 提供的代码直接从 SQL 数据库导出优惠券。

How WooCommerce Coupons are stored in Database

是否可以检索使用优惠券的用户的订单元数据?我想在报告中包括用户名、电子邮件地址和可能的其他一些自定义字段。

谢谢。

  • 我们生成 1000 张优惠券并将其提供给一家公司(客户)。
  • 每张优惠券的使用限额为 1。
  • 公司然后将优惠券分发给其成员。
  • 月底,我们需要向公司发送一份报告,告诉他们:
    1. 使用了 1000 张优惠券中的多少张。 (这可以使用上面链接中提供的代码来完成)
    2. 然后从使用过的优惠券中,我们需要告诉他们是谁使用了它,换句话说,就是下订单的用户的姓名和电子邮件地址。

LINK TO SAMPLE EXCEL EXPORT-黄色字段来自优惠券-蓝色字段来自订单

最佳答案

下面的 MySQL 查询将列出您与订单关联的所有优惠券。

SELECT pc.post_title AS coupon_name,
pc.post_excerpt AS coupon_description,
Max(CASE WHEN pmc.meta_key = 'discount_type' AND pc.`ID` = pmc.`post_id` THEN pmc.`meta_value` END) AS discount_type,
Max(CASE WHEN pmc.meta_key = 'coupon_amount' AND pc.`ID` = pmc.`post_id` THEN pmc.`meta_value` END) AS coupon_amount,
Max(CASE WHEN pmc.meta_key = 'product_ids' AND pc.`ID` = pmc.`post_id` THEN pmc.`meta_value` END) AS product_ids,
Max(CASE WHEN pmc.meta_key = 'product_categories' AND pc.`ID` = pmc.`post_id` THEN pmc.`meta_value` END) AS product_categories,
Max(CASE WHEN pmc.meta_key = 'customer_email' AND pc.`ID` = pmc.`post_id` THEN pmc.`meta_value` END) AS customer_email,
Max(CASE WHEN pmc.meta_key = 'usage_limit' AND pc.`ID` = pmc.`post_id` THEN pmc.`meta_value` END) AS usage_limit,
Max(CASE WHEN pmc.meta_key = 'usage_count' AND pc.`ID` = pmc.`post_id` THEN pmc.`meta_value` END) AS total_usaged,
po.ID AS order_id,
MAX(CASE WHEN pmo.meta_key = '_billing_email' AND po.ID = pmo.post_id THEN pmo.meta_value END) AS billing_email,
MAX(CASE WHEN pmo.meta_key = '_billing_first_name' AND po.ID = pmo.post_id THEN pmo.meta_value END) AS billing_first_name,
MAX(CASE WHEN pmo.meta_key = '_billing_last_name' AND po.ID = pmo.post_id THEN pmo.meta_value END) AS billing_last_name,
MAX(CASE WHEN pmo.meta_key = '_order_total' AND po.ID = pmo.post_id THEN pmo.meta_value END) AS order_total
FROM `wp_posts` AS pc
INNER JOIN `wp_postmeta` AS pmc ON pc.`ID` = pmc.`post_id`
INNER JOIN `wp_woocommerce_order_items` AS woi ON pc.post_title = woi.order_item_name
AND woi.order_item_type = 'coupon'
INNER JOIN `wp_posts` AS po ON woi.order_id = po.ID
AND po.post_type = 'shop_order'
AND po.post_status IN ('wc-completed', 'wc-processing', 'wc-refunded') -- Added needed order status over here.
INNER JOIN `wp_postmeta` AS pmo ON po.ID = pmo.post_id
WHERE pc.post_type = 'shop_coupon'
GROUP BY po.ID
ORDER BY pc.ID DESC,
po.ID DESC
LIMIT 0, 10 -- modify it accordingly.

希望这对您有所帮助!

关于mysql - 在 WooCommerce 中列出带有订单详细信息的优惠券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52763254/

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