2016-11-01 64 views
7

因此,我有一個使用woocommerce的電子商務,我使用自定義運費來支付運費。 而且我已經添加了新的輸入數據(選擇)。就像你可以看到下面的圖片: enter image description hereWordPress的Woocommerce從定製運輸領域(AJAX)獲得價值

// Hook in 
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields'); 

// Our hooked in function - $fields is passed via the filter! 
function custom_override_checkout_fields($fields) { 

    $fields['billing']['billing_city'] = array(
     'type' => 'select', 
     'label' => __('Kota/Kabupaten', 'woocommerce'), 
     'required' => true, 
     'class' => array('form-row-wide', 'address-field'), 
     'clear' => true, 
     'options' => array(
      '' => 'Pilih Kota/Kabupaten' 
     ) 
    ); 
    $fields['shipping']['shipping_city'] = array(
     'type' => 'select', 
     'label' => __('Kota/Kabupaten', 'woocommerce'), 
     'required' => true, 
     'class' => array('form-row-wide', 'address-field'), 
     'clear' => true, 
     'options' => array(
      '' => 'Pilih Kota/Kabupaten' 
     ) 
    ); 


    $fields['billing']['billing_subdistrict'] = array(
     'type' => 'select', 
     'label' => __('Kecamatan', 'woocommerce'), 
     'required' => true, 
     'class' => array('form-row-wide', 'address-field'), 
     'clear' => true, 
     'options' => array(
      '' => 'Pilih Kecamatan' 
     ) 
    ); 

    $fields['shipping']['shipping_subdistrict'] = array(
     'type' => 'select', 
     'label' => __('Kecamatan', 'woocommerce'), 
     'required' => true, 
     'class' => array('form-row-wide', 'address-field'), 
     'clear' => true, 
     'options' => array(
      '' => 'Pilih Kecamatan' 
     ) 
    ); 


    return $fields; 
} 

Woocommerce默認數據有ADDRESS_1,address_2,國家,州,城市,但我需要一個名爲街道一個更多的數據。我不需要保存這些數據(subdistrict)。但我需要使用該值作爲跟蹤運費的參數。

我已經創建了新的class-custom-shipping-delivery.php。 和我已經確保該功能完美工作,因爲我已經嘗試手動設置$子區域數據。

//custom-shipping.php 
$province = $package['destination']['state']; 
$city = $package['destination']['city']; 

$subdistrict= 'something'; 
//How to get the data from custom field (ajax) 
//because I need to see the shipping fee result before Checkout (and update it to add rate) 


$destination_code = $this->getDestinationCode($province,$city,$subdistrict); 

$ongkir = $this->cek_ongkir($origin,$destination_code,$weight); 

//print_r(); 

// send the final rate to the user. 
$this->add_rate(array(
    'id' => $this->id, 
    'label' => $this->title, 
    'cost' => $ongkir 
)); 

摘要:

如何從街辦輸入類型選擇(結賬頁)值?

對不起,我只是從另一個人的工作編輯,所以我不明白,代碼。但我認爲他們忘了獲得這個價值,因爲他們只是硬編碼,我是一個新手,所以我不知道如何通過結算表單傳遞數據。

+0

後代碼,而不是屏幕截圖 – Blueblazer172

回答

2

尋找答案了一段時間後,然後我得到了答案。

所以爲了總結它: 以上答案使用會話從航運自定義字段(ajax)獲取數據。 所以當AJAX運行。我發送'subdistrict'字段的值來運行並保存到會話中。

function ajax_process($subdistrict){ 
    //some code 
    session_start(); 
    $_SESSION['subdistrict'] = $subdistrict; 
} 

然後從其他功能的會話數據運行此代碼:

@session_start(); 
$subdistrict = $_SESSION['subdistrict']; 

更多信息,我得到的回答是:

http://phpwpinfo.com/how-to-update-shipping-cost-in-cart-dynamically-based-on-a-custom-field-in-woocommerce/