Search code examples
phpwordpressdynamicpaypalcontact-form-7

Prefill CF7 data from the database using a dynamic field


I am trying to create a method through which I can retrieve the data from the database of contacts saved by contact form 7 and pre-fill the fields after the users have previously selected, from a drop-down menu, the PayPal payment method and filled out the form , without then proceeding to the actual payment.

The flow to follow is this: If the user has chosen to pay with PayPal and filled out the entire form, the "paid" item will be 0 and without making any redirects to the PayPal page Then, when the user returns to the event card, we will show all the fields filled in previously and the "pay with paypal" button to complete the paypal payment. So the "paid" item will be 1.

So far I have used the following plugins to save the data of the completed forms Advanced CF7 DB and Contact Form CFDB7. The use of one of these, or someone else, is indifferent to my goal

Here is the code with which so far I am able to populate the dynamic and hidden field [dynamichidden paid ""] when the user selects PayPal but without too much success:

add_action('wpcf7_posted_data', 'course_registration_actions_paypal', 10, 1);
function course_registration_actions_paypal($stato0){
$paypal["paymentmethod"] = '0';
$stato0[“paid”] = '0';
$stato1[“paid”] = '1';

    if (isset($paypal[“paymentmethod]) && $stato0[“paid”] === '0') {
        return $stato0;
    } else {
        return $stato1;
    }
};

Solution

  • Eventually I managed to get it to work as I wanted, but there remains a redirect problem using the "redirection for contact form 7" plugin.

    Always redirect on the first case (paypal) rather than differentiate.

    I think the problem is 'wpcf7_posted_data' as without redirects they work fine.

    This is my code:

    add_action('wpcf7_posted_data','course_registration_actions_persist_payment_status', 10, 1);
    function course_registration_actions_persist_payment_status($record){
    $current_user = wp_get_current_user();
    $userId = get_field('id__pro', 'user_' . $current_user->ID);
    $eventId = get_field('id', false);
    
    $records = WPCF7_ContactForm::find([
        'ID-Course' => $eventId, // 2
        'ID-User' => $userId, // 82994
    ]);
    
    if ($record['paymentmethod'][0] == 0) {
        // Paypal
        $record['paymentmethod'] = 0;
        // No Paid
        $record['paid'] = 0;
    } else {
        // Bank
        $record['paymentmethod'] = 1;
        // Paid
        $record['paid'] = 1;
    }
    
    return $record;
    };