在 WooCommerce 结账中添加一个自定义复选框,其值显示在管理员编辑顺序中 [英] Add a custom checkbox in WooCommerce checkout which value shows in admin edit order

查看:22
本文介绍了在 WooCommerce 结账中添加一个自定义复选框,其值显示在管理员编辑顺序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试添加一个 <input type="checkbox"> 该值也显示在 woocommerce 后端,所以我可以在最后看到客户是否勾选了该框.

I try to add an <input type="checkbox"> which value also shows at the woocommerce backend, so I can see at the end if the costumer ticked the box or not.

复选框应位于付款方式下方.

The checkbox should be below the Payment Methods.

是否可以在 WooCommerce 结帐中添加自定义复选框,该复选框的值显示在管理员编辑顺序中?

Is it possible to add a custom checkbox in WooCommerce checkout which value shows in admin edit order?

推荐答案

您可以分 3 步完成:

You can do it in 3 steps:

  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+ 中测试并有效.选中该复选框后,它会在订单编辑页面的帐单地址下方显示自定义文本...

Tested in WooCommerce 3+ and works. When the checkbox has been checked, it display a custom text below billing address in order edit page…

这篇关于在 WooCommerce 结账中添加一个自定义复选框,其值显示在管理员编辑顺序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆