diff --git a/includes/wc-account-functions.php b/includes/wc-account-functions.php index f705abe38f6..7ec43bd8874 100644 --- a/includes/wc-account-functions.php +++ b/includes/wc-account-functions.php @@ -86,13 +86,11 @@ function wc_get_account_menu_items() { 'customer-logout' => __( 'Logout', 'woocommerce' ), ); - // Remove empty items. - if ( ! empty( WC()->customer ) ) { - $downloads = WC()->customer->get_downloadable_products(); + // Remove downloads if no downloadable products exist. + $downloads = wc_get_customer_download_permissions( get_current_user_id() ); - if ( ! sizeof( $downloads ) ) { - unset( $items['downloads'] ); - } + if ( ! sizeof( $downloads ) ) { + unset( $items['downloads'] ); } // Check if payment gateways support add new payment methods. diff --git a/includes/wc-user-functions.php b/includes/wc-user-functions.php index 8b668a12127..697b760fde1 100644 --- a/includes/wc-user-functions.php +++ b/includes/wc-user-functions.php @@ -351,6 +351,39 @@ function wc_modify_map_meta_cap( $caps, $cap, $user_id, $args ) { } add_filter( 'map_meta_cap', 'wc_modify_map_meta_cap', 10, 4 ); +/** + * Get customer download permissions from the database. + * + * @param int $customer_id Customer/User ID + * @return array + */ +function wc_get_customer_download_permissions( $customer_id ) { + global $wpdb; + + return apply_filters( 'woocommerce_permission_list', $wpdb->get_results( + $wpdb->prepare( " + SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions + WHERE user_id = %d + AND permissions.order_id > 0 + AND + ( + permissions.downloads_remaining > 0 + OR permissions.downloads_remaining = '' + ) + AND + ( + permissions.access_expires IS NULL + OR permissions.access_expires >= %s + OR permissions.access_expires = '0000-00-00 00:00:00' + ) + ORDER BY permissions.order_id, permissions.product_id, permissions.permission_id; + ", + $customer_id, + date( 'Y-m-d', current_time( 'timestamp' ) ) + ) + ), $customer_id ); +} + /** * Get customer available downloads. * @@ -358,35 +391,13 @@ add_filter( 'map_meta_cap', 'wc_modify_map_meta_cap', 10, 4 ); * @return array */ function wc_get_customer_available_downloads( $customer_id ) { - global $wpdb; - $downloads = array(); $_product = null; $order = null; $file_number = 0; // Get results from valid orders only - $results = apply_filters( 'woocommerce_permission_list', $wpdb->get_results( $wpdb->prepare( " - SELECT permissions.* - FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions - WHERE user_id = %d - AND permissions.order_id > 0 - AND - ( - permissions.downloads_remaining > 0 - OR - permissions.downloads_remaining = '' - ) - AND - ( - permissions.access_expires IS NULL - OR - permissions.access_expires >= %s - OR - permissions.access_expires = '0000-00-00 00:00:00' - ) - ORDER BY permissions.order_id, permissions.product_id, permissions.permission_id; - ", $customer_id, date( 'Y-m-d', current_time( 'timestamp' ) ) ) ), $customer_id ); + $results = wc_get_customer_download_permissions( $customer_id ); if ( $results ) { foreach ( $results as $result ) {