php-检查用户是否有权在WooCommerce中下载任何文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-检查用户是否有权在WooCommerce中下载任何文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1588字,纯文字阅读大概需要3分钟。
内容图文
我想检查是否有任何用户有权下载任何文件.我有产品ID和用户ID,如何检查?
我在google和woocommerce文档中进行了很多探索,但未找到任何解决方案.
有什么帮助吗?
提前致谢.
解决方法:
这是获取此信息的过程,您可以在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
内容总结
以上是互联网集市为您收集整理的php-检查用户是否有权在WooCommerce中下载任何文件全部内容,希望文章能够帮你解决php-检查用户是否有权在WooCommerce中下载任何文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。