如何在 WooCommerce 中将数据数组从一个挂钩函数传递到另一个函数

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

我正在使用 woocommerce 结帐字段和购物车中包含的数据给合作伙伴 API 打电话。第一个调用是使用结帐过程挂钩进行的,以通过 API 验证数据。如果调用 returns 它是有效的,则该过程继续,否则它停止。

然后我需要等待付款完成才能再次调用以使用完全相同的数据实际创建计划。有没有办法将我在结帐过程挂钩中的第一次调用中创建的数组传递给付款完成挂钩,以便不必重建数组?

代码如下所示:

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
}

您只需在自定义 WC_Session 变量中设置您的数组,之后您就可以像这样使用它:

$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

然后您将能够在 woocommerce_payment_complete 挂钩函数中调用该数组,使用:

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

这应该有效。


现在最好删除(删除)这个自定义会话变量 一旦你完成使用它......你可以使用 __unset() 方法来做到这一点:

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

所以在你的第二个钩子函数中:

//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');
}