检查用户是否有权在 WooCommerce 中下载任何文件
Check if user have permission or not to download any file in WooCommerce
我想检查是否有任何用户有权下载任何文件。我有产品 ID 和用户 ID,如何查看?
我在 google 和 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 文件。已测试并有效。
参考:
我想检查是否有任何用户有权下载任何文件。我有产品 ID 和用户 ID,如何查看?
我在 google 和 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 文件。已测试并有效。
参考: