检查用户是否有权限在WooCommerce中下载文件


Check if user have permission or not to download any file in WooCommerce

我想检查是否有用户有权限下载任何文件。我有产品id和用户id,所以我怎么检查?

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

帮忙吗?

2020 - WooCommerce 3+代码更新

下面是获取可下载的订单项信息的过程,您可以在php文件中的任何函数或钩子函数中使用这些信息:

// Get all current customer orders
$customer_orders = wc_get_orders( $args = array(
    'limit'       => -1,
    'customer_id' => get_current_user_id(), // The current user id
    'status'      => array_keys(wc_get_order_statuses()),
) );
// The different loops to get the downloadable products bought by this user
if ( ! empty($customer_orders) ){
    // Loop through customer orders
    foreach ( $customer_orders as $order){
        // Check if current order has available downloadable items (permitted)
        if( $order->has_downloadable_item() && $order->is_paid() && $order->is_download_permitted() ){
            // Loop through order items with downloadable items
            foreach( $order->get_items() as $item ){
                $product_id = $item->get_product_id(); // product ID
                // Get the downloadbles files (array):
                $downloads = $item->get_item_downloads();
                if( ! empty( $downloads ) ) {
                    // Loop through downloads
                    foreach( $downloads as $download_id => $download ) {
                        // Output formatted download name and link
                        echo '<p>' . $download['name'] . ' <a href="' . $download['download_url'] . '">' . __("Download link") . '</a></p>';
                    }
                }
            }
        }
    }
}

代码放在您的活动子主题(或活动主题)的任何php文件中。

参考:

  • WC_Abstract_Order类-方法
  • WC_Order类-方法
  • WC_Order_Item_Product类-方法