2017-03-28 2 views
1

사용자가 정보 (이름, 전화 번호, 국가, 나이 ...)를 삽입하는 단일 제품 페이지에 양식이 있습니다.이 정보를 청구서에 표시하는 것입니다. 세부 사항. 그런 다음체크 아웃 사용자 정의 필드에서 제품 사용자 정의 필드 값 검색

<div class="col-md-6"> 
       <div class="form-group"> 
        <input class="form-control alt" name="billing_vol" type="text" 
          placeholder="<?php esc_html_e('N° Vol : *', 'rentcar'); ?>" > 
       </div> 
      </div> 
      <div class="form-group"> 
       <input class="form-control alt" type="text" name="billing_cli_age" 
         placeholder="<?php esc_html_e('Age *', 'rentcar'); ?>" 
        " 
       > 
      </div><div class="col-md-6"> 
       <div class="form-group"> 
        <?php 
         global $woocommerce; 
         $countries_obj = new WC_Countries(); 
         $countries = $countries_obj->__get('countries'); 
        ?> 

        <?php 
         woocommerce_form_field('billing_country', array(
         'type'  => 'select', 
         'class'  => array('chzn-drop'), 
         'placeholder' => __('Country'), 
         'options' => $countries 
         ) 
         ); 
        ?> 

       </div> 
      </div> 

결제 관련 세부 양식에서 체크 아웃 페이지에서 같은 일이, 내가 추가 한 않았다

그래서,이 무슨 짓을했는지 나는 하나의 제품에서 내 양식에 두 필드를 추가, 여기에 코드입니다 이 2 개의 필드. 여기에 코드입니다 :

add_filter('woocommerce_checkout_fields' , 'custom_override_checkout_fields'); 

function custom_override_checkout_fields($fields) { 

    $fields['billing']['billing_cli_age'] = array(
     'label'   => __('Age ', 'woocommerce'), 
     'placeholder' => _x('Age ', 'placeholder', 'woocommerce'), 
     'required'  => false, 
     'class'   => array('form-row-wide'), 
     'clear'   => true 
    ); 

    $fields['billing']['billing_vol'] = array(
     'label'   => __('N° vol', 'woocommerce'), 
     'placeholder' => _x('N° vol', 'placeholder', 'woocommerce'), 
     'required'  => false, 
     'class'   => array('form-row-wide'), 
     'clear'   => true 
    ); 
    return $fields; 
} 

는 지금은 붙어, 나는 체크 아웃 페이지에서 형성 결제 세부에 (나는 하나의 제품에서 가지고) 형태로 사용자가 추가 한 정보를 전송하는 방법을 모르겠어요.

어떻게하면됩니까?

감사합니다.

답변

1

다음은 장바구니에 장바구니가 Google Checkout 맞춤 입력란을 채울 때 값을 제출 한 제품 맞춤 입력란을 가져 오는 전체 기능 코드입니다.

첫 번째 기능은 테스트 목적으로 만 사용됩니다. 그러나 제품 맞춤 필드 코드는이 작업을 수행하기 위해 장바구니 추가 양식 안에 있어야합니다. 당신이 얻을, 루프를 중단해야합니다, 당신은 여러 카트 항목이 수 있듯이

// Store the data fields to cart 
add_action('woocommerce_add_cart_item_data', 'action_save_my_custom_product_fields', 10, 2); 
function action_save_my_custom_product_fields($cart_item_data, $product_id) { 
    $bool = false; 
    $data = array(); 
    if(isset($_REQUEST['billing_vol'])) { 
     $cart_item_data['custom_data']['billing_vol'] = $_REQUEST['billing_vol']; 
     $data['billing_vol'] = $_REQUEST['billing_vol']; 
     $bool = true; 
    } 
    if(isset($_REQUEST['billing_cli_age'])) { 
     $cart_item_data['custom_data']['billing_cli_age'] = $_REQUEST['billing_cli_age']; 
     $data['billing_cli_age'] = $_REQUEST['billing_cli_age']; 
     $bool = true; 
    } 
    if(isset($_REQUEST['billing_country'])) { 
     $cart_item_data['custom_data']['billing_country'] = $_REQUEST['billing_country']; 
     $data['billing_country'] = $_REQUEST['billing_country']; 
     $bool = true; 
    } 
    if($bool) { 
     // below statement make sure every add to cart action as unique line item 
     $unique_key = md5(microtime().rand()); 
     $cart_item_data['custom_data']['unique_key'] = $unique_key; 
     $data['unique_key'] = $unique_key; 
     WC()->session->set('custom_data', $data); 
    } 
    return $cart_item_data; 
} 

: 여기

add_action('woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button',20); 
function action_before_add_to_cart_button() { 
    ?> 
     <div class="col-md-6"> 
      <div> 
       <input class="form-control alt" name="billing_vol" type="text" 
         placeholder="<?php esc_html_e('N° Vol : *', 'rentcar'); ?>"> 
      </div> 
      <div> 
       <input class="form-control alt" type="text" name="billing_cli_age" 
         placeholder="<?php esc_html_e('Age *', 'rentcar'); ?>"> 
      </div> 
      <div class="form-group"> 
      <?php 
       // Getting the countries (simplified) 
       $countries = WC()->countries->get_countries(); 

       woocommerce_form_field('billing_country', array(
        'type'  => 'select', 
        'class'  => array('chzn-drop'), 
        'placeholder' => __('Country'), 
        'options' => $countries 
       )); 
      ?> 
      </div> 
     </div> 
    <?php 
} 

는 제품 사용자 정의 필드 값 카트에 전달합니다 누락 된 후크 기능입니다 첫 번째 항목의 사용자 정의 값 :

add_filter('woocommerce_checkout_fields' , 'custom_override_checkout_fields'); 
function custom_override_checkout_fields($fields) { 

    // We break the loop to get the custom data from 1 cart item 
    foreach(WC()->cart->get_cart() as $cart_item) break; 

    $data = $cart_item['custom_data']; 

    // COUNTRIES: Getting the countries (simplified) 
    $countries = WC()->countries->get_countries(); 
    $billing_country_key = $data['billing_country']; 
    $billing_country_value = $countries[$billing_country_key]; 

    $fields['billing']['billing_cli_age'] = array(
     'type'   => 'text', 
     'label'   => __('Age ', 'woocommerce'), 
     'placeholder' => _x('Age ', 'placeholder', 'woocommerce'), 
     'required'  => false, 
     'class'   => array('form-row-wide'), 
     'clear'   => true, 
     'default'  => $data['billing_cli_age'], 
    ); 

    $fields['billing']['billing_vol'] = array(
     'type'   => 'text', 
     'label'   => __('N° vol', 'woocommerce'), 
     'placeholder' => _x('N° vol', 'placeholder', 'woocommerce'), 
     'required'  => false, 
     'class'   => array('form-row-wide'), 
     'clear'   => true, 
     'default'  => $data['billing_vol'], 
    ); 

    $fields['billing']['billing_country'] = array(
     'type' => 'select', 
     'default' => $billing_country_key, 
     'options' => array($billing_country_key => $billing_country_value), 
    ); 

    return $fields; 
} 

코드가 활성 자식 테마 (또는 테마)의 function.php 파일에 간다 나 또한 어떤 플러그인 파일입니다.

이 코드는 테스트되었으며 작동합니다.

국가 필드에 대해서는 Javascript/Ajax도 관련되어 있기 때문에 복잡하지 않습니다. 고유 한 "선택" "옵션"으로 작동하지만 여러 선택 옵션이있는 것은 아닙니다.

+0

당신이 천재인지,이 코드와 함께 작동합니까? 내가 선택한 필드를 가지고 있기 때문에 나는 하나의 제품에서 가지고있는 모든 값을 얻습니다. – Hanane

+0

@ 하난 선택한 필드에 대해 매우 유사합니다 : 모든 분야에 관한 질문, 나는 그것을 시험해 볼 수 있습니다. ... 당신에게 달렸습니다. – LoicTheAztec

+0

덕분에 Loic에게 도움을 청했습니다. 제 3 필드 "billing_country"를 추가하기 위해 제 질문을 편집했습니다. 그래서 내가 원하는 것은 결제 내역 양식에있는 billing_country가 단일 제품 양식에서 가치를 얻는다는 것입니다. – Hanane

관련 문제