2016-11-08 115 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

相關問題