WordPress Woocommerce订阅-按产品获取总活跃订阅

时间:2019-01-15 15:12:03

标签: mysql wordpress woocommerce woocommerce-subscriptions

我们正在构建一个简单的仪表板状态插件,该插件将显示特定产品的总“有效”订阅。

我正在使用以下功能来获取活动的订阅总数,但由于订阅已切换,所以人数并未排队。我该如何考虑交换机订阅?

function get_active_subscriptions_count( $product_id) {
  global $wpdb;

  // return the active subscriptions for a defined product ID
  return $wpdb->get_var("
      SELECT COUNT(p.ID)
      FROM {$wpdb->prefix}posts as p
      LEFT JOIN {$wpdb->prefix}posts AS p2 ON p.post_parent = p2.ID
      LEFT JOIN {$wpdb->prefix}postmeta AS pm ON p2.ID = pm.post_id
      LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON pm.post_id = woi.order_id
      LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
      WHERE p.post_type LIKE 'shop_subscription' 
      AND p2.post_type LIKE 'shop_order' AND woi.order_item_type LIKE 'line_item'
      AND pm.meta_key LIKE '_customer_user'
      AND woim.meta_key = '_product_id'
      AND woim.meta_value = '$product_id'
      AND p.post_status LIKE 'wc-active'
  ");
}

我该如何处理交换机订阅?查询中我缺少什么?

0 个答案:

没有答案