2017-08-27 80 views
0

我嘗試添加<input type="checkbox">,這個值也顯示在woocommerce後端,所以最後我可以看到客戶是否勾選了該框。在WooCommerce結賬中添加一個自定義複選框,其值顯示爲管理編輯訂單

該複選框應位於付款方式下方。

是否可以在WooCommerce結帳中添加一個自定義複選框,該值顯示爲admin編輯順序?

+0

您到目前爲止嘗試過了什麼?堆棧溢出不是一種代碼編寫服務,但如果您告訴我們自己做了什麼,我們可以幫助解決代碼中的特定問題。請參閱[我如何問一個好問題](https://stackoverflow.com/help/how-to-ask) – FluffyKitten

回答

2

你可以做到這一點在3個步驟:

  1. 添加下面的付款方式
  2. 保存自定義複選框字段自定義複選框字段時,它的順序元
  3. 顯示自定義複選框字段的檢查當它的順序編輯頁面

在這裏檢查的是代碼:

// Add custom checkout field: woocommerce_review_order_before_submit 
add_action('woocommerce_review_order_before_submit', 'my_custom_checkout_field'); 
function my_custom_checkout_field() { 
    echo '<div id="my_custom_checkout_field">'; 

    woocommerce_form_field('my_field_name', array(
     'type'  => 'checkbox', 
     'class'  => array('input-checkbox'), 
     'label'  => __('My custom checkbox'), 
    ), WC()->checkout->get_value('my_field_name')); 
    echo '</div>'; 
} 

// Save the custom checkout field in the order meta, when checkbox has been checked 
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1); 
function custom_checkout_field_update_order_meta($order_id) { 

    if (! empty($_POST['my_field_name'])) 
     update_post_meta($order_id, 'my_field_name', $_POST['my_field_name']); 
} 

// Display the custom field result on the order edit page (backend) when checkbox has been checked 
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1); 
function display_custom_field_on_order_edit_pages($order){ 
    $my_field_name = get_post_meta($order->get_id(), 'my_field_name', true); 
    if($my_field_name == 1) 
     echo '<p><strong>My custom field: </strong> <span style="color:red;">Is enabled</span></p>'; 
} 

代碼會出現在您的活動子主題(或主題)的function.php文件中,或者也存在於任何插件文件中。

在WooCommerce 3+中測試並正常工作。當複選框已被選中時,它將在帳單地址下方顯示自定義文本,以便編輯頁面...

+0

真棒一如既往,正是我想要的! – sHamann

+0

您如何設置默認選中的複選框?我試着給woocommerce_form_field的args數組添加'default'=> 1,但這沒有幫助。 –

+0

@YanivWainer我認爲與'),WC() - > checkout-> get_value('my_field_name'));''而不是'),'');' – LoicTheAztec

相關問題