Skip to content

Instantly share code, notes, and snippets.

@dkwiebe
Last active October 30, 2018 18:00
Show Gist options
  • Save dkwiebe/477d5f8af91022357ef13324a06a130e to your computer and use it in GitHub Desktop.
Save dkwiebe/477d5f8af91022357ef13324a06a130e to your computer and use it in GitHub Desktop.
Retrieve Subscription products from WooCommerce
SELECT subscription_line_items.subscription_id,
subscriptions.post_status,
product.id AS product_id,
product.post_title AS product_name,
product.post_status,
mo.product_type,
subscription_line_items.qty,
subscription_line_items.product_total,
subscription_meta.currency,
subscription_meta.billing_period,
subscription_meta.billing_interval,
subscription_meta.customer_id,
subscription_meta.billing_first_name,
subscription_meta.billing_last_name,
subscription_meta.schedule_next_payment
FROM wp_posts AS product
LEFT JOIN (
SELECT tr.object_id AS product_id, t.slug AS product_type
FROM wp_term_relationships AS tr
INNER JOIN wp_term_taxonomy AS X
ON ( x.taxonomy = 'product_type' AND x.term_taxonomy_id = tr.term_taxonomy_id )
INNER JOIN wp_terms AS t
ON t.term_id = x.term_id
) AS mo
ON product.id = mo.product_id
LEFT JOIN (
SELECT wcoitems.order_id AS subscription_id, wcoimeta.meta_value AS product_id,
wcoimeta.order_item_id, wcoimeta2.meta_value AS product_total, wcoimeta3.meta_value AS qty
FROM wp_woocommerce_order_items AS wcoitems
INNER JOIN wp_woocommerce_order_itemmeta AS wcoimeta
ON wcoimeta.order_item_id = wcoitems.order_item_id
INNER JOIN wp_woocommerce_order_itemmeta AS wcoimeta2
ON wcoimeta2.order_item_id = wcoitems.order_item_id
INNER JOIN wp_woocommerce_order_itemmeta AS wcoimeta3
ON wcoimeta3.order_item_id = wcoitems.order_item_id
WHERE wcoitems.order_item_type = 'line_item'
AND wcoimeta.meta_key = '_product_id'
AND wcoimeta2.meta_key = '_line_total'
AND wcoimeta3.meta_key = '_qty'
) AS subscription_line_items ON product.id = subscription_line_items.product_id
LEFT JOIN (
SELECT DISTINCT(wp_postmeta.post_id) AS wppm_id,
wp_postmeta.meta_value AS currency,
wp_postmeta1.meta_value AS billing_period,
wp_postmeta2.meta_value AS billing_interval,
wp_postmeta3.meta_value AS customer_id,
wp_postmeta4.meta_value AS billing_first_name,
wp_postmeta5.meta_value AS billing_last_name,
/*wp_postmeta6.meta_value AS billing_company,
wp_postmeta7.meta_value AS billing_address,*/
wp_postmeta8.meta_value AS schedule_next_payment/*,
wp_postmeta9.meta_value AS schedule_cancelled,
wp_postmeta10.meta_value AS schedule_end,
wp_postmeta11.meta_value AS paid_date,
wp_postmeta12.meta_value as completed_date */
FROM wp_postmeta
INNER JOIN wp_postmeta AS wp_postmeta1
ON wp_postmeta1.post_id = wp_postmeta.post_id
INNER JOIN wp_postmeta AS wp_postmeta2
ON wp_postmeta2.post_id = wp_postmeta.post_id
INNER JOIN wp_postmeta AS wp_postmeta3
ON wp_postmeta3.post_id = wp_postmeta.post_id
INNER JOIN wp_postmeta AS wp_postmeta4
ON wp_postmeta4.post_id = wp_postmeta.post_id
INNER JOIN wp_postmeta AS wp_postmeta5
ON wp_postmeta5.post_id = wp_postmeta.post_id
/*INNER JOIN wp_postmeta AS wp_postmeta6
ON wp_postmeta6.post_id = wp_postmeta.post_id
INNER JOIN wp_postmeta AS wp_postmeta7
ON wp_postmeta7.post_id = wp_postmeta.post_id*/
INNER JOIN wp_postmeta AS wp_postmeta8
ON wp_postmeta8.post_id = wp_postmeta.post_id
/*INNER JOIN wp_postmeta AS wp_postmeta9
ON wp_postmeta9.post_id = wp_postmeta.post_id
INNER JOIN wp_postmeta AS wp_postmeta10
ON wp_postmeta10.post_id = wp_postmeta.post_id
/*INNER JOIN wp_postmeta AS wp_postmeta11
ON wp_postmeta11.post_id = wp_postmeta.post_id
INNER JOIN wp_postmeta AS wp_postmeta12
ON wp_postmeta12.post_id = wp_postmeta.post_id */
WHERE wp_postmeta.meta_key = '_order_currency'
AND wp_postmeta1.meta_key = '_billing_period'
AND wp_postmeta2.meta_key = '_billing_interval'
AND wp_postmeta3.meta_key = '_customer_user'
AND wp_postmeta4.meta_key = '_billing_first_name'
AND wp_postmeta5.meta_key = '_billing_last_name'
/*AND wp_postmeta6.meta_key = '_billing_company'
AND wp_postmeta7.meta_key = '_billing_address_index'*/
AND wp_postmeta8.meta_key = '_schedule_next_payment'
/*AND wp_postmeta9.meta_key = '_schedule_cancelled'
AND wp_postmeta10.meta_key = '_schedule_end'
AND wp_postmeta11.meta_key = '_paid_date'
AND wp_postmeta12.meta_key = '_completed_date'*/
) AS subscription_meta ON subscription_meta.wppm_id = subscription_line_items.subscription_id
LEFT JOIN wp_posts AS subscriptions
ON subscriptions.ID = subscription_line_items.subscription_id
/*WHERE product.post_status = 'publish' */
WHERE product.post_type = 'product'
AND subscriptions.post_type = 'shop_subscription'
AND subscriptions.post_status NOT IN( 'wc-pending', 'trash' )
ORDER BY product_id, billing_period, billing_interval;
@mattpramschufer
Copy link

On line: 19 you need to change
INNER JOIN wp_term_taxonomy AS X
to be
INNER JOIN wp_term_taxonomy AS x

The uppercase X throws errors as you reference lowercase x below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment