2016-11-01 8 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と呼ばれる1以上のデータを必要としています。私はそのデータを保存する必要はありません(下位)。しかし、私はトラックの運賃のためのパラメータとしてその値を使用する必要があります。

私はすでに新しいclass-custom-shipping-delivery.phpを作成しています。 と私は既に$ subdistrictデータを手動で設定しようとしているので、関数が完全に機能することを確認しています。

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

概要:(チェックアウトページ上)を選択Subdistrict入力タイプから値を取得する方法

申し訳ありません私は他の人の仕事から編集するだけで、私はそのコードを全く理解できません。しかし、私は彼らがそれをハードコードし、私はワードプレスの初心者ですので、チェックアウトフォームにデータを渡す方法を知らないので、彼らはその価値を得るのを忘れたと思います。

+0

ポストコードの代わりに、 screenshots – 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/

0

ここには、Woocommerce用のチェックアウトフィールドエディタというプラグインがあります。

https://docs.woocommerce.com/document/checkout-field-editor/

そのプラグインは、単一のサイトライセンスは$ 49かかりますように見えます。

もう1つの方法は、自分でコードを作成することです。ここにチュートリアルがあります。しばらくのための答えを検索した後

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

+0

私はすでにカスタムフィールドを追加しています。しかし私はajaxからその価値を得る方法を知らない。 – GandhyOnly

関連する問題