Eliott
Eliott

Reputation: 89

How to pass a data array from one hooked function to another in WooCommerce

I am doing a call to a partner API using the data contained in woocommerce checkout fields as well as the cart. This first call is made using the checkout process hook to validate the data through the API. if the call returns that it is valid the process goes on, otherwise it stops.

I then need to wait for the payment to be complete to do another call to actually create a plan using the exact same data. Is there a way to pass the array i create in the first call in the checkout process hook to the payment complete hook in order to not have to rebuild the array?

The code would look as follows:

add_action('woocommerce_checkout_process', 'apicall_verif');  
function apicall_verif() {
    $_ids      = array(...);

    $billing_fields = [
      'billing_first_name'  => '',
      'billing_last_name'   => '',
      'billing_email'       => '',
      'billing_phone'       => '',
      'insurance-birthdate' => '',
      'gender-selection'    => '',
        'billing_address_1' => '',
        'billing_address_2' => '',
        'billing_postcode'  => '',
        'billing_city'      => ''  
    ];
    
    foreach( $fields as $field_name => $value ) {
        if( !empty( $_POST[$field_name] ) ) {
            $fields[$field_name] = sanitize_text_field( $_POST[$field_name] );
        } 
    }
     
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( in_array( $cart_item['product_id'],  $_ids ) ) {
            $_product_id = $cart_item['product_id'];
        }
    }
    
    $_product       =  wc_get_product( $_product_id );
    $billingcountry = WC()->customer->get_billing_country();
    $cur_lang       = pll_current_language();
    
    $data_verif = array(
        "refs"         =>  array(
            "country"  => $billingcountry,
        ),
        "settings"     =>  array(
            "language" => $cur_lang
        ),
        "policyholder" =>  array(
            "firstName"  => $fields['billing_first_name'] ,
            "lastName"   => $fields['billing_last_name'],
            "email"      => $fields['billing_email'],
            "phone"      => $fields['billing_phone'],
            "birthdate"  => $fields['insurance-birthdate'],
            "gender"     => $fields['gender-selection' ],
            "address"    =>  array(
                "country"  => $billingcountry,
                "zip"      => $fields['billing_postcode'],
                "city"     => $fields['billing_city'],
                "street"   => $fields['billing_address_1'],
                "number"   => $fields['billing_address_2'],
                "box"      => "box"
            ),
            "entityType" => "ENTITY_TYPE_PERSON"
        ), 
        "risk"         => array(
             "model"            => $_product -> get_title(),
             "originalValue"    =>  $_product -> get_price() * 100,
             "antiTheftMeasure" => "ANTI_THEFT_MEASURE_NONE",
        ),
        "terms"        => array(
            "depreciation" => false
        ),
    );
    
    // Set the array to a custom WC_Session variable (to be used afterwards)
    WC()->session->set('data_verif', $data_verif);
    
    // API CALL HERE AND CHECK WHETHER VALID OR NOT
}


//After payment is completed
add_action( 'woocommerce_payment_complete', 'apicall_create' );
function apicall_create() {
    //Somehow get $data_verif here and do another API call
}

Upvotes: 1

Views: 272

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

You can simply set your array in a custom WC_Session variable, that you will be able to use afterwards like:

$data_verif = array(
    "refs"         =>  array(
        "country"  => $billingcountry,
    ),
    "settings"     =>  array(
        "language" => $cur_lang
    ),
    "policyholder" =>  array(
        "firstName"  => $fields['billing_first_name'] ,
        "lastName"   => $fields['billing_last_name'],
        "email"      => $fields['billing_email'],
        "phone"      => $fields['billing_phone'],
        "birthdate"  => $fields['insurance-birthdate'],
        "gender"     => $fields['gender-selection' ],
        "address"    =>  array(
            "country"  => $billingcountry,
            "zip"      => $fields['billing_postcode'],
            "city"     => $fields['billing_city'],
            "street"   => $fields['billing_address_1'],
            "number"   => $fields['billing_address_2'],
            "box"      => "box"
        ),
        "entityType" => "ENTITY_TYPE_PERSON"
    ), 
    "risk"         => array(
         "model"            => $_product -> get_title(),
         "originalValue"    =>  $_product -> get_price() * 100,
         "antiTheftMeasure" => "ANTI_THEFT_MEASURE_NONE",
    ),
    "terms"        => array(
        "depreciation" => false
    ),
);

// Set the array to a custom WC_Session variable (to be used afterwards)
WC()->session->set('data_verif', $data_verif);

// API CALL HERE AND CHECK WHETHER VALID OR NOT

Then you will be able to call that array in woocommerce_payment_complete hooked function using:

$data_verif = WC()->session->get('data_verif');

This should work.


Now is better to remove (delete) this custom session variable once you have finished to use it… You can do it using the __unset() method like:

WC()->session->__unset('data_verif');

So in your 2nd hooked function:

//After payment is completed
add_action( 'woocommerce_payment_complete', 'apicall_create' );
function apicall_create() {
    // Get the data array
    WC()->session->get('data_verif');

    // Do the API CALL HERE
    
    // Remove the session variable
    WC()->session->__unset('data_verif');
}

Upvotes: 2

Related Questions