phpwordpresspdfgravityforms

Showing gravityforms generated gravity PDF-s under woocommerce downloads tab


If an user is logged in and has filled in the form, then a PDF is generated (Im using gravity forms and gravityPDF). Im trying to show the logged in users PDF link(s) under woocommerce my account downloads tab. So every PDF(s) is different for the user, based on how many times they've registered.

So far I know I need to do a lookup for the Entry ID using the GFAPI, through that I can use the [gravitypdf] shortcode with the entry attribute to display the appropriate PDF Download link. So far it seems I can only present shortcodes in this fashion - [gravitypdf id="560f2ef799945" entry="250"]

Do I have to get the right entry id with wp_get_current_user();? Code wise I have something like this so far:

function user_id_gf (){
    $search_criteria = array();
    $form_id = 1;
    $sorting = array(
      'key' => get_current_user($user_id),
    );
    $result = GFAPI::get_entries($form_id, $search_criteria, $sorting);

    return $result;
}

Can I somehow create a dynamic shortcode, based on $result variable? So that the link the shortcode generates is based on logged in user? Another question is, how can I show multiple PDF links if an user has registered multiple times?


Solution

  • A couple of things. You need to use the search_criteria array to find the needed entries, not the sort. Then you need to search a key and value like in the example below. I added a few more lines to the search to only include active entries (as opposed to trashed entries). Once you find the matching entries, you can loop through them in a foreach loop. In each loop, you can add the pdf shortcode with that entry id. I also added a '
    ' break return in case there are multiple pdfs for easier viewing.

    Try adding the following to your functions.php Then use the following shortcode: [get_user_pdfs]

    //get user pdfs
    add_shortcode( 'get_user_pdfs', 'user_pdfs' );
    function user_pdfs($atts = array()){
        $form_id = 1;
        $search_criteria = array(
            'status'        => 'active',
            'field_filters' => array(
                array(
                    'key' => 'created_by', 
                    'operator' => 'is',
                    'value' => wp_get_current_user()->ID
                )
            )
        );
        $entries = GFAPI::get_entries( $form_id, $search_criteria );
        foreach($entries as $entry){
             $pdfs.=do_shortcode('[gravitypdf id="560f2ef799945" entry="'.$entry['id'].'"]').'<br>';
        }
        return $pdfs;
    }