Home > Mobile >  CF7 forms breaks when adding WC()->session->set()
CF7 forms breaks when adding WC()->session->set()

Time:08-16

I have a form from which I want to save data and use it during woocommerce checkout. If some conditions are met, the forms redirects to the checkout page.

I want to use the form data but when I add WC()->session->set('cf7_posted_data', $posted_data); the form breaks, ie. emails are sent but then it spins forever and I don't get the validation message.

Here is my code I use and the debug log, any idea what's causing the issue?


function wc_wpcf7_mail_sent_function($contact_form) {
    $submission = WPCF7_Submission::get_instance();
    if($submission) {
        $posted_data = $submission->get_posted_data();
        // send data to WooCommerce session
        if (!empty($posted_data['try'][0])) {
            WC()->session->set('cf7_posted_data', $posted_data);
        }
    }
} 


[12-Aug-2022 08:43:29 UTC] PHP Fatal error:  Uncaught Error: Call to a member function set() on null in /home/romainbe/public_html/thelittlegymfranchise.co.za/wp-content/themes/saratov-child/functions.php:1271
Stack trace:
#0 /home/romainbe/public_html/thelittlegymfranchise.co.za/wp-includes/class-wp-hook.php(307): wc_wpcf7_mail_sent_function(Object(WPCF7_ContactForm))
#1 /home/romainbe/public_html/thelittlegymfranchise.co.za/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters('', Array)
#2 /home/romainbe/public_html/thelittlegymfranchise.co.za/wp-includes/plugin.php(476): WP_Hook->do_action(Array)
#3 /home/romainbe/public_html/thelittlegymfranchise.co.za/wp-content/plugins/contact-form-7/includes/submission.php(102): do_action('wpcf7_mail_sent', Object(WPCF7_ContactForm))
#4 /home/romainbe/public_html/thelittlegymfranchise.co.za/wp-content/plugins/contact-form-7/includes/submission.php(27): WPCF7_Submission->proceed()
#5 /home/romainbe/public_html/thelittlegymfranchise.co.za/wp-content/plugins/contact-form-7/includes/contact-form.php(1047): WPCF in /home/romainbe/public_html/thelittlegymfranchise.co.za/wp-content/themes/saratov-child/functions.php on line 1271

CodePudding user response:

Thanks a lot, indeed WC session didn't exist yet so I've added "WC()->session = new WC_Session_Handler();" and it solved my issue, code below:

        if (!empty($posted_data['try'][0])) {
            // Set data to WooCommerce session
            WC()->session = new WC_Session_Handler();
            WC()->session->set('cf7_posted_data', $posted_data);
        }
  • Related