簡體   English   中英

如何將數據數組從一個鈎子 function 傳遞到 WooCommerce 中的另一個

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

我正在使用 woocommerce 結帳字段和購物車中包含的數據給合作伙伴 API 打電話。 第一次調用是使用結帳過程掛鈎進行的,以通過 API 驗證數據。 如果調用返回它是有效的,則該過程繼續,否則它停止。

然后,我需要等待付款完成才能再次調用以使用完全相同的數據實際創建計划。 有沒有辦法將我在結帳過程掛鈎的第一次調用中創建的數組傳遞給支付完成掛鈎,以便不必重建數組?

代碼如下所示:

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

這應該有效。


現在最好刪除(刪除)這個自定義 session 變量,一旦你完成使用它......你可以使用__unset()方法來做到這一點,例如:

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

所以在你的第二個鈎子 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');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM