php-检查用户是否有权在WooCommerce中下载任何文件

我想检查是否有任何用户有权下载任何文件.我有产品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-在唯一类别存档页面上隐藏主要类别标题


下一篇:php-一次将多个项目添加到WooCommerce购物车