EDD Recommended Products actually tries to search suggestion based on already in cart products
Sometimes it can not find the expected results and the number of given results could be random (sometimes nothing, sometimes two or sometimes four)
this presents a problem for new products or expensive products which are generally sold alone
I fix it on my site forcing the number of recommendations always to the configured adding random products
add_filter('edd_rp_multi_recommendation_results', 'force_recommendation_results');
function force_recommendation_results( $suggestions ) {
if(count($suggestions) < edd_get_option( 'edd_rp_suggestion_count', 3 )) {
$max_results = edd_get_option( 'edd_rp_suggestion_count', 3 ) - count($suggestions);
// Excludes cart items and already suggested downloads
$cart_items = edd_get_cart_contents();
$cart_post_ids = array_map('intval', wp_list_pluck( $cart_items, 'id' ));
$excluded_downloads = array_merge( $cart_post_ids, array_keys( $suggestions ) );
// Excludes already purchased downloads
$user_purchases = edd_get_users_purchases( get_current_user_id(), -1, false, 'any' );
if($user_purchases) {
foreach($user_purchases as $user_purchase) {
$purchase_downloads = edd_get_payment_meta_cart_details( $user_purchase->ID, true );
foreach($purchase_downloads as $purchase_download) {
$excluded_downloads[] = $purchase_download['id'];
}
}
}
$random_suggestions = get_posts( array(
'post_type' => 'download',
'post_status' => 'publish',
'posts_per_page' => $max_results,
'post__not_in' => $excluded_downloads,
'orderby' => 'rand',
) );
// Adds new suggestions to suggestions array with structure array( download_id => purchases )
foreach($random_suggestions as $random_suggestion) {
$suggestions[$random_suggestion->ID] = 1;
}
}
return $suggestions;
}
And why I write this here? Because I do not know if this could be a new feature for the extension or a new snippet for this library and, of course, I want you suggestions to improve the code :)