在WooCommerce结账中添加一个自定义复选框,其值显示为管理编辑订单

问题描述:

我尝试添加<input type="checkbox">,这个值也显示在woocommerce后端,所以最后我可以看到客户是否勾选了该框。在WooCommerce结账中添加一个自定义复选框,其值显示为管理编辑订单

该复选框应位于付款方式下方。

是否可以在WooCommerce结帐中添加一个自定义复选框,该值显示为admin编辑顺序?

+0

您到目前为止尝试过了什么?堆栈溢出不是一种代码编写服务,但如果您告诉我们自己做了什么,我们可以帮助解决代码中的特定问题。请参阅[我如何问一个好问题](https://*.com/help/how-to-ask) – FluffyKitten

你可以做到这一点在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