I want to check whether any user have permission or not to download any file. I have product id and user id so How I can check?
I have explored a lot on google and in woocommerce documentation but didn’t find any solution.
Any help?
Thanks in advance.
Advertisement
Answer
2020 – Code update for WooCommerce 3+
Here is the process to get downloadable order items information that you can use in any function or hooked function in your php files:
JavaScript
x
// 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>';
}
}
}
}
}
}
Code goes in any php file of your active child theme (or active theme). Tested and works.
Reference: