Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Last active September 15, 2023 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/a94a05d926e185ef1c6c1c9302c92d94 to your computer and use it in GitHub Desktop.
Save nfsarmento/a94a05d926e185ef1c6c1c9302c92d94 to your computer and use it in GitHub Desktop.
Export all ACTIVE subscriptions from WooCommerce Subscriptions using a MySQL query
SELECT
p.ID as 'Subscription ID',
p.post_status as 'Subscription Status',
pm1.meta_value as 'Billing First Name',
pm2.meta_value as 'Billing Last Name',
pm3.meta_value as 'Billing Email',
oitems.order_item_name as 'Product',
pm4.meta_value as 'Order Total',
pm5.meta_value as 'Order Tax'
FROM wp_posts p
INNER JOIN wp_postmeta pm1 ON pm1.post_id = p.ID
INNER JOIN wp_postmeta pm2 ON pm2.post_id = p.ID
INNER JOIN wp_postmeta pm3 ON pm3.post_id = p.ID
INNER JOIN wp_postmeta pm4 ON pm4.post_id = p.ID
INNER JOIN wp_postmeta pm5 ON pm5.post_id = p.ID
INNER JOIN wp_woocommerce_order_items oitems ON oitems.order_id = p.ID
WHERE
post_type = 'shop_subscription'
AND post_status = 'wc-active'
AND pm1.meta_key = '_billing_first_name'
AND pm2.meta_key = '_billing_last_name'
AND pm3.meta_key = '_billing_email'
AND pm4.meta_key = '_order_total'
AND pm5.meta_key = '_order_tax'
AND oitems.order_item_type = 'line_item'
GROUP BY p.ID;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment