WooCommerce 优惠券如何存储在数据库中?

Posted

技术标签:

【中文标题】WooCommerce 优惠券如何存储在数据库中?【英文标题】:How WooCommerce Coupons are stored in Database? 【发布时间】:2017-08-16 04:33:32 【问题描述】:

每次我有新的收银机时,我都需要运行一个脚本来检查我的优惠券,但我找不到优惠券生成器 (WooCommerce) 的优惠券的存储位置。

【问题讨论】:

【参考方案1】:

WooCommerce 优惠券与post_type => 'shop_coupon' 存储在wp_posts 表中。并且所有关联的数据都存储在wp_postmeta表中。

您可以使用这个 mysql 查询来获取所有的优惠券:

SELECT * FROM `wp_posts` WHERE `post_type` = 'shop_coupon' ORDER BY `ID` DESC

这会得到所有的优惠券

SELECT p.`ID`, 
       p.`post_title`   AS coupon_code, 
       p.`post_excerpt` AS coupon_description, 
       Max(CASE WHEN pm.meta_key = 'discount_type'      AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS discount_type,          -- Discount type 
       Max(CASE WHEN pm.meta_key = 'coupon_amount'      AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS coupon_amount,          -- Coupon amount 
       Max(CASE WHEN pm.meta_key = 'free_shipping'      AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS free_shipping,          -- Allow free shipping 
       Max(CASE WHEN pm.meta_key = 'expiry_date'        AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS expiry_date,                -- Coupon expiry date 
       Max(CASE WHEN pm.meta_key = 'minimum_amount'     AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS minimum_amount,         -- Minimum spend 
       Max(CASE WHEN pm.meta_key = 'maximum_amount'     AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS maximum_amount,         -- Maximum spend 
       Max(CASE WHEN pm.meta_key = 'individual_use'     AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS individual_use,         -- Individual use only 
       Max(CASE WHEN pm.meta_key = 'exclude_sale_items' AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS exclude_sale_items,         -- Exclude sale items 
       Max(CASE WHEN pm.meta_key = 'product_ids'    AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_ids,                -- Products 
       Max(CASE WHEN pm.meta_key = 'exclude_product_ids'AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS exclude_product_ids,        -- Exclude products 
       Max(CASE WHEN pm.meta_key = 'product_categories' AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS product_categories,             -- Product categories 
       Max(CASE WHEN pm.meta_key = 'exclude_product_categories' AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS exclude_product_categories,-- Exclude Product categories 
       Max(CASE WHEN pm.meta_key = 'customer_email'     AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS customer_email,         -- Email restrictions 
       Max(CASE WHEN pm.meta_key = 'usage_limit'    AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS usage_limit,                -- Usage limit per coupon 
       Max(CASE WHEN pm.meta_key = 'usage_limit_per_user'   AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS usage_limit_per_user,   -- Usage limit per user 
       Max(CASE WHEN pm.meta_key = 'usage_count'    AND  p.`ID` = pm.`post_id` THEN pm.`meta_value` END) AS total_usaged                   -- Usage count 
FROM   `wp_posts` AS p 
       INNER JOIN `wp_postmeta` AS pm ON  p.`ID` = pm.`post_id` 
WHERE  p.`post_type` = 'shop_coupon' 
       AND p.`post_status` = 'publish' 
GROUP  BY p.`ID` 
ORDER  BY p.`ID` ASC;

参考:MySQL Query to Get All Coupon

【讨论】:

知道有效期可能在哪里吗? @DeepakShenoy:到期日期存储在wp_postmeta 表中,对应post_idmeta_key = expiry_date。请检查答案随附的参考链接。 您好,很抱歉重复评论,在您的网站上留下了一条。是否可以检索使用优惠券的用户的订单元数据?对于所有优惠券,我的优惠券使用限制设置为 1。我希望在报告中包含用户名、电子邮件地址以及可能的其他一些自定义字段。谢谢。 嗨@user1664798:由于这个问题与您的问题没有直接关系,所以我无法更新我的答案,如果您可以发布一个新问题并ping我我会尝试的链接,那就太好了找出解决方案,同时您可以查看this ans。 @RaunakGupta 我已经在这里发布了我的问题。希望这就是我想ping你的方式?非常感谢。 ***.com/questions/52763254/…

以上是关于WooCommerce 优惠券如何存储在数据库中?的主要内容,如果未能解决你的问题,请参考以下文章

检查是不是在 woocommerce 中应用了任何优惠券代码

Woocommerce 如何将优惠券表格移动到结帐页面的底部

如何以编程方式删除 Woocommerce 中应用的折扣券?

从 WooCommerce 订单中获取优惠券数据

如何使用 woocommerce_order_item_meta_end 在电子邮件确认中显示项目元数据

如何在 Woocommerce 购物车页面中添加订单备注字段?