Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force products number on recommended products extension #69

Open
rubengc opened this issue Jun 15, 2016 · 0 comments
Open

Force products number on recommended products extension #69

rubengc opened this issue Jun 15, 2016 · 0 comments
Assignees

Comments

@rubengc
Copy link

rubengc commented Jun 15, 2016

Hi

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

Here is my code:

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants