2016-11-01 2 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를 만듭니다. 그리고 이미 $ 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 
)); 

요약 :

하는 방법 (체크 아웃 페이지)를 선택 가도 입력 형식에서 값을 얻는 방법?

죄송합니다. 저는 다른 사람의 작업을 편집하여 코드를 전혀 이해하지 못합니다. 하지만 그들은 단지 하드 코드를하기 때문에 그 가치를 잊어 버린 것 같아요. 그리고 저는 Wordpress의 초보자이기 때문에 계산 양식에 데이터를 전달하는 법을 모르겠습니다.

+0

우편 번호 대신을 screenshots – Blueblazer172

답변

2

는 내가 답변을 얻을.

따라서 요약 : 위 답변은 세션을 사용하여 사용자 정의 필드 (아약스)를 출하 할 때 데이터를 가져옵니다. 그래서 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 용 Checkout Field Editor라는 플러그인이 있습니다. 이 플러그인은 하나의 사이트 라이센스를 위해 $ 49 비용 같은

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

보인다.

다른 옵션은 직접 코드를 작성하는 것입니다. 자습서가 있습니다. 잠시 동안 대답을 검색 한 후

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

+0

이미 사용자 정의 필드를 추가하고 있습니다. 그러나 나는 아약스로부터 그 가치를 얻는 방법을 모른다. – GandhyOnly

관련 문제