2017-03-28 121 views
1

我在用戶插入其信息(姓名,電話,國家,年齡...)的單個產品頁面中有一個表單,我需要做的是在結算中顯示此信息細節 。在結賬自定義字段中檢索產品自定義字段值

所以,我所做的是我加了2場到我的單品的形式,這裏是代碼:

<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

這裏是完整的功能代碼,以獲得產品自定義字段提交值時,添加到購物車將填補我們的結帳自定義字段。

第一個函數僅用於我的測試目的。但是,您的產品自定義字段代碼應該位於「添加到購物車」表單中,以實現此功能。

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 
} 

這裏是缺失的掛鉤函數,將進入購物車你的產品的自定義字段中的值:

// 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_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

@Hanane對於選擇字段中的所有值,這是非常相似:如果您更新問題與你所有的領域,我可以使它的工作,測試它......這取決於你 – LoicTheAztec

+0

感謝Loic你的幫助。我編輯了我的問題,以添加第三個字段「billing_country」。所以我想要的是我在結算明細表中的billing_country從單一產品表單中獲得價值。 – Hanane

相關問題