Add session data using add_filter()

Question

I’m a little new to this and learning still.

I have several forms that collect data from a user. This data must then be displayed in an extra field on the shipping page of Woocommerce.

I use add_filter() in the functions.php file of the theme and extra code to display the data on the custom shipping page.

It used to work fine but not any longer and the session information is no longer there.

Can anybody please tell me how do I add the form data to $_SESSION[]

Here is the code for the forms:

add_filter( 'wpcf7_before_send_mail', function( $data ) {
    $submission = WPCF7_Submission::get_instance();
    $data = $submission->get_posted_data();
    /* print '<pre>';
    var_dump($posted_data);
    print '</pre>'; */
     
    $donation_forms = array(700, 717, 724, 726, 9364);
    if (in_array($data['_wpcf7'], $donation_forms)) {
        $form_data = [];

        // global fields for 4 forms
        $form_data['type'] = array('title' => '', 'value' => @$data['type']);
        $form_data['donation-amount'] = array('title' => '', 'value' => @$data['donation-amount'][0]);
        $form_data['amount-other'] = array('title' => '', 'value' => @$data['amount-other']);
        $form_data['name'] = array('title' => 'Name', 'value' => @$data['name']);
        $form_data['address'] = array('title' => 'Address', 'value' => @$data['address']);
        $form_data['city'] = array('title' => 'City', 'value' => @$data['city']);
        $form_data['state'] = array('title' => 'State', 'value' => @$data['state']);
        $form_data['zip'] = array('title' => 'Zip', 'value' => @$data['zip']);
        $form_data['email'] = array('title' => 'Email', 'value' => @$data['email']);
        
        // one time donation
        if($data['_wpcf7'] == 700) {
            $form_data['gift'] = array('title' => 'Is this gift?', 'value' => @$data['gift'][0]);
        }

        // Sustaining Donation
        if($data['_wpcf7'] == 717 || $data['_wpcf7'] == 724) {
            $form_data['donation-type'] = array('title' => 'Donation type', 'value' => @$data['donation-type'][0]);
        }

        // one time donation, Sustaining Donation, Memorial Donations
        if($data['_wpcf7'] == 700 || $data['_wpcf7'] == 717 || $data['_wpcf7'] == 724) {
            $form_data['is-seasonal-address'] = array('title' => 'Do you have a seasonal address?', 'value' => @$data['is-seasonal-address'][0]);

            if (@$data['is-seasonal-address'][0] == "Yes") {
                $form_data['seasonal-address'] = array('title' => 'Seasonal address', 'value' => @$data['seasonal-address']);
                $form_data['seasonal-address-mail'] = array('title' => 'Do you receive mail at your seasonal address?', 'value' => @$data['seasonal-address-mail'][0]);
            }
        }

        // one time donation, Sustaining Donation
        if($data['_wpcf7'] == 700 || $data['_wpcf7'] == 717) {
            $form_data['corporate-gift'] = array('title' => 'Is this a Corporate Gift?', 'value' => @$data['corporate-gift'][0]);
            if (@$data['corporate-gift'][0] == "Yes") {
                $form_data['corporate-business-name'] = array('title' => 'Business name', 'value' => @$data['corporate-business-name']);
                $form_data['corporate-address'] = array('title' => 'Business address', 'value' => @$data['corporate-address']);
                $form_data['corporate-city'] = array('title' => 'Business City', 'value' => @$data['corporate-city']);
                $form_data['corporate-state'] = array('title' => 'Business State', 'value' => @$data['corporate-state']);
                $form_data['corporate-zip'] = array('title' => 'Business Zip', 'value' => @$data['corporate-zip']);
            }
        }

        // Memorial and Honorarium Donations
        if($data['_wpcf7'] == 724) {
            $form_data['acknowledgement'] = array('title' => 'Would you like to send an acknowledgement of this gift to the honoree’s family or friend?', 'value' => @$data['acknowledgement'][0]);
            if (@$data['acknowledgement'][0] == "Yes") {
                $form_data['acknowledgement-name'] = array('title' => 'Name', 'value' => $data['acknowledgement-name']);
                $form_data['acknowledgement-address'] = array('title' => 'Address', 'value' => $data['acknowledgement-address']);
            }

            // $form['name']['title'] = "Donor Name";
            $form_data['honor'] = array('title' => 'In Memory/Honor of', 'value' => @$data['honor']);
        }
    }
});

and here is the code to display the data on the shipping page.

<div class="woocommerce-additional-fields">
    <?php do_action( 'woocommerce_before_order_notes', $checkout ); ?>

    <?php if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' === get_option( 'woocommerce_enable_order_comments', 'yes' ) ) ) : ?>

        <?php if ( ! WC()->cart->needs_shipping() || wc_ship_to_billing_address_only() ) : ?>

            <h3><?php esc_html_e( 'Additional information', 'woocommerce' ); ?></h3>

        <?php endif; ?>

        <div class="woocommerce-additional-fields__field-wrapper">
            <?php foreach ( $checkout->get_checkout_fields( 'order' ) as $key => $field ) : ?>
            <?php

            print '<pre>';
            var_dump($_SESSION);
            print '</pre>'; 

            $data = $checkout->get_value( $key );
            if (isset($_SESSION['donation_data'])) {
                $data = '';
                $type = '';
                foreach ($_SESSION['donation_data'] as $k => $val) {
                    if ($k == 'type') $type = $val['value'].'';
                    if ($k != "donation-amount" && $k != "amount-other" && $k != "type") {
                        $data .= $val['title'] . ': '.$val['value'] .'';
                    }

                    if ($val['value'] == "Donation") {
                        $data .= $_SESSION['donation_data']['donation-amount']['value'].'
                        ';
                    }
                }
            }
            ?>
            <?php woocommerce_form_field( $key, $field, $type.$data); ?>
            <?php endforeach; ?>
        </div>

    <?php endif; ?>

    <?php do_action( 'woocommerce_after_order_notes', $checkout ); ?>
</div>

Any help will be greatly appreciated thank you.

0
calvincani 4 months 0 Answers 15 views 0

Leave an answer