2017-05-04 24 views
0

我正在使用WooCommerce功能的WordPress網站上工作。如何在WooCommerce產品頁面上創建自定義文本框?

我已經建立2個自定義字段,對於產品頁的後端內的產品數據中,使用以下代碼:

<?php 
function theme_name_woocommerce_custom_fields() { 
    // Price Per Character 
    woocommerce_wp_text_input( 
     array(
      'id'   => '_character_price', 
      'label'  => 'Price Per Character', 
      'description' => 'This is the amount a customer pays per letter entered.', 
      'desc_tip' => 'true', 
      'placeholder' => 'Enter Amount per Letter (Exclude the £)' 
     ) 
    ); 
    // Custom Text Box 
    woocommerce_wp_checkbox(
     array(
      'id'   => '_custom_text_box', 
      'label'  => 'Show Custom Text Box', 
      'description' => 'Select this box, if you would like a Custom Text Box to appear on the Product's page.', 
      'desc_tip' => 'true', 
     ) 
    ); 
} 
add_action('woocommerce_product_options_general_product_data', 'theme_name_woocommerce_custom_fields'); 

function save_theme_name_woocommerce_custom_field($post_id) { 
    if (! empty($_POST['_custom_text_field'])) { 
     update_post_meta($post_id, '_custom_text_field', esc_attr($_POST['_custom_text_field'])); 
    } 
} 
add_action('woocommerce_process_product_meta', 'save_theme_name_woocommerce_custom_fields'); 
?> 

在參考woocommerce_wp_checkbox,我想創建一個功能,由此當此複選框被選中時,它會在相關產品的頁面上創建一個自定義文本框。這個自定義文本框然後可以被潛在客戶用來輸入他們想要打印到頁面的產品中的一段文本。

有人知道我需要輸入什麼額外的編碼,以達到上述目標嗎?

回答

1

您可以覆蓋簡單/變量產品的模板文件並在其中添加自定義字段。只有在複選框被選中的情況下,保持顯示/隱藏文本框的邏輯。

添加到購物車完成後,您將在POST中獲取所有發佈的數據。從那裏抓起來,放入woocommerce的對象。

============================================== ========= 編輯:

您可以按照此步驟:

  1. 複製woocommerce /模板/單品/添加到購物車/ variable.php此模板您的孩子主題並進行自定義。你會在那裏看到一個表單標籤。在那裏你可以把你自定義的創建複選框(你從admin添加)。此外,添加一個自定義文本框(用戶將輸入文本) - 並隱藏它。

  2. 從functions.php中添加自定義js。 - 在這個JS你可以寫邏輯,如果複選框被選中,那麼你將顯示文本框,否則不是。

  3. 現在,當用戶添加到購物車時,將此自定義文本框數據添加到woocommerce對象。如何將自定義數據輸入到woocommerce對象 - 您可以在此處獲得一步一步的詳細信息:https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/

+0

感謝您的回覆。這將如何在產品頁面上創建一個文本框,以便潛在客戶輸入他們自己的文本? – Craig

+0

如果你看到單品/ add-to-cart/variable.php,你會在那裏找到表單標籤 - 同樣適用於simple.php。只需添加一個文本框 - 一個正常的html輸入控件。並添加您的自定義js以顯示/隱藏它 –

+0

我怎樣才能設置它,以便當選擇複選框時,文本框出現? – Craig

相關問題