直接访问WooCommerce 2.0订阅

时间:2015-02-18 11:05:06

标签: php wordpress woocommerce

我一直以这种方式直接从我的Wordpress安装中访问WooCommerce订阅:

SELECT * FROM `wp_usermeta`
    WHERE `meta_key` LIKE '%woocommerce_subscriptions'
    AND   `user_id`=0000

但是使用WooCommerce 2.x,订阅架构已经改变。来自文档:

  

订阅存储&访问

     

强烈建议您通过API函数访问订阅及其关联的元数据,例如WC_Subscriptions_Manager::get_subscription()。计划使用Subscriptions 2.0版更改存储架构。 API函数将保持向后兼容性,但如果直接访问数据,您应该期望您的代码与2.0版本打破。

有没有人知道如何使用WooCommerce 2.x直接从数据库访问订阅数据?

1 个答案:

答案 0 :(得分:1)

尝试使用meta_value,如下所示:

SELECT Count({$wp_prefix}woocommerce_order_itemmeta.meta_value) > 0 AS mVal
FROM   {$wp_prefix}woocommerce_order_itemmeta
       LEFT JOIN {$wp_prefix}woocommerce_order_items
              ON {$wp_prefix}woocommerce_order_itemmeta.order_item_id =
                 {$wp_prefix}woocommerce_order_items.order_item_id
       LEFT JOIN {$wp_prefix}postmeta
              ON {$wp_prefix}postmeta.post_id = {$wp_prefix}woocommerce_order_items.order_id
       LEFT JOIN {$wp_prefix}posts
              ON {$wp_prefix}postmeta.post_id = {$wp_prefix}posts.id
WHERE  ( {$wp_prefix}woocommerce_order_itemmeta.meta_key IN (
                  '_subscription_status-Yikes', '_subscription_end_date',
                  '_subscription_start_date-Yikes',
                  '_subscription_period- Yikes' ) )
       AND post_status != 'wc-pending'
       AND {$wp_prefix}postmeta.meta_value = (SELECT id
                                        FROM   {$wp_prefix}users
                                        WHERE  user_url = 'http://{$subDomain}')
       AND {$wp_prefix}woocommerce_order_itemmeta.meta_value = 0;

{$wp_prefix}应该是您的前缀。最终的代码是:

$subscription = mysqli_fetch_array(mysqli_query($conn, "SELECT COUNT({$wp_prefix}woocommerce_order_itemmeta.meta_value) > 0 AS mVal FROM {$wp_prefix}woocommerce_order_itemmeta LEFT JOIN {$wp_prefix}woocommerce_order_items ON {$wp_prefix}woocommerce_order_itemmeta.order_item_id = {$wp_prefix}woocommerce_order_items.order_item_id LEFT JOIN {$wp_prefix}postmeta ON {$wp_prefix}postmeta.post_id = {$wp_prefix}woocommerce_order_items.order_id LEFT JOIN {$wp_prefix}posts ON {$wp_prefix}postmeta.post_id = {$wp_prefix}posts.ID WHERE ({$wp_prefix}woocommerce_order_itemmeta.meta_key IN ('_subscription_status-Yikes', '_subscription_end_date', '_subscription_start_date-Yikes', '_subscription_period- Yikes') ) AND post_status != 'wc-pending' AND {$wp_prefix}postmeta.meta_value = (SELECT ID FROM {$wp_prefix}users WHERE user_url = 'http://{$subDomain}') AND {$wp_prefix}woocommerce_order_itemmeta.meta_value = 0"));
$subscription = $subscription[0];

测试$subscription1是否为订阅者。