2016-11-08 73 views
2

我想检查任何用户是否有权限下载任何文件。我有产品ID和用户ID,所以我如何检查?检查用户是否有权限下载WooCommerce中的任何文件

我已经在google和woocommerce文档中探索了很多,但是没有找到任何解决方案。

任何帮助?

在此先感谢。

+0

我https://github.com/woocommerce/woocommerce/issues/12275#issuecomment-259122914从woocommerce支持此链接,任何人都可以帮忙吗? –

回答

2

以下是获取此信息的过程,您可以在任何函数中使用该函数或在您的php文件中使用挂钩函数。

下面是代码:

// Get all current customer orders 
$customer_orders = wc_get_orders($args = array(
    'numberposts' => -1, 
    'meta_key' => '_customer_user', 
    'meta_value' => get_current_user_id(),// for current user id 
    'post_status' => array_keys(wc_get_order_statuses()), 
)); 

// The different loops to get the downloadable products bought by this user 
foreach ($customer_orders as $customer_order){ 
    if (!empty($customer_orders)){ 
     foreach ($customer_orders as $customer_order){ 
      if(!$customer_order->has_downloadable_item() && $customer_order->is_paid()){ 
       foreach($customer_order->get_items() as $item){ 
        $item_id = $item['product_id']; // product ID 

        // Just the downloadable items Below 
        if($item->is_download_permitted()) { 
         // do something because the download is permitted 

         // you can use different methods on the $customer_product object: 
         // Get the downloadbles files (array): 
         $downloadable_files_array = get_item_downloads($item); 
         // Display download links for an order item. 
         $display_item_downloads = display_item_downloads($item); 
         // Get the Download URL of a given $download_id 
         // $download_url = get_download_url($item_id, $download_id) 

        } else { 
         // do something because the download is NOT permitted 
        } 
       } 
      } 
     } 
    } 
} 

代码放在您的活动子主题(或主题)的任何PHP文件或也是任何一个插件的PHP文件。

这是测试和功能齐全。

参考:WC_Abstract_Order class - Methods

相关问题