2015-10-16 150 views
1

我一直在開發一個WooCommerce網站,我有一個任務,我卡住了。我需要在購物車物品表中添加額外的自定義輸入字段。在Woocommerce購物車頁面上添加自定義輸入字段

像例如,如果一個人的訂單「2000 YouTube的觀看次數」包,然後用鼠標右鍵下方的項目名稱,我希望用戶輸入他YouTube視頻網址

我知道我可以在產品頁面添加自定義輸入字段和可以簡單地讓他們在車頁面顯示。但我想將用戶輸入數據帶到購物車頁面。每個購物車項目都會有一個自定義輸入字段。到目前爲止,我已經研究了很多,但對我的解決方案沒有任何成功。所有我發現要將自定義輸入字段數據打印到產品頁面上添加的購物車頁面上。

任何幫助,將理解使用Google我發現這個教程的

+0

在購物車頁面上添加它似乎是故意讓它比應該更難。只需在各個產品頁面上使用[Product Add ons](http://www.woothemes.com/products/product-add-ons)。 – helgatheviking

回答

-3

30秒。

http://www.themelocation.com/how-to-add-custom-field-to-woocommerce-checkout-page/

這裏是片段。

/** 
* Add the field to the checkout page 
*/ 

add_action('woocommerce_after_order_notes', 'some_custom_checkout_field'); 

functionsome_custom_checkout_field($checkout) { 

    echo '<div id="some_custom_checkout_field"><h2>' . __('Heading') . '</h2>'; 

    woocommerce_form_field('some_field_name', array(
     'type'   => 'text', 
     'class'   => array('my-field-class form-row-wide'), 
     'label'   => __('Additional Field'), 
     'placeholder' => __('Some hint'), 
     'required'  => true, 
    ), $checkout->get_value('some_field_name')); 

    echo '</div>'; 
} 

/** 
* Process the checkout 
*/ 
add_action(‘woocommerce_checkout_process’, ‘some_custom_checkout_field_process’); 
functionsome_custom_checkout_field_process() { 
    // if the field is set, if not then show an error message. 
    if (! $_POST[‘some_field_name’]){ 
     wc_add_notice(__(‘Please enter value.’), ‘error’); 
    } 
} 

/** 
* Update the order meta with field value 
*/ 
add_action(‘woocommerce_checkout_update_order_meta’, ‘some_custom_checkout_field_update_order_meta’); 
function some_custom_checkout_field_update_order_meta($order_id) { 
    if (! empty($_POST[‘some_field_name’])) { 
     update_post_meta($order_id, ‘Some Field’, sanitize_text_field($_POST[‘some_field_name’])); 
    } 
} 

我已經實現了這樣的事情,它應該工作,我還沒有測試過上面的代碼。

希望它有幫助。

+1

你浪費了你30秒的時間,你沒有正確地閱讀我的問題。我提到我想在購物車頁面上實現該功能,而不是在Checkout頁面上。我知道如何在結帳頁面上做到這一點,但我要求購物車頁面。 –

0

我也有這個問題。我已經用它解決了它。像購物車頁面一樣添加字段,但使用jQuery接收這些值並通過GET請求動態地轉移它們(將它們添加到提交按鈕),$ _GET以隱藏輸入類型將它們填充到結帳頁面中。

相關問題