如何向 Woo Commerce 结帐添加额外字段并在编辑订单页面中显示

时间:2021-04-15 08:42:23

标签: php wordpress woocommerce checkout orders

我在 WooCommerce 结账时额外添加了三个字段(CVR 编号、参考编号和 ønsket leveringsdato)。它们在结账时正确显示,但一旦下订单,信息就不会发送到 WooCommerce 后端/订单概览。我如何发送信息以便我们接收?

    function add_woocommerce_billing_fields($billing_fields){

    //reorder woo my billing address form fields
    $billing_fields2['billing_first_name'] = $billing_fields['billing_first_name'];
    $billing_fields2['billing_last_name']  = $billing_fields['billing_last_name'];

    $billing_fields2['billing_vat'] = array(
        'type' => 'text',
        'label' =>  __('CVR nummer',  'keyelp-shop-customization' ),
        'class' => array('form-row-wide'),
        'required' => true,
        'clear' => true
    );

    $billing_fields2['billing_ref'] = array(
        'type' => 'text',
        'label' =>  __('Reference nummer',  'keyelp-shop-customization' ),
        'class' => array('form-row-wide'),
        'required' => false,
        'clear' => true
    );

    $billing_fields2['billing_date'] = array(
        'type' => 'text',
        'label' =>  __('Ønsket leveringsdato',  'keyelp-shop-customization' ),
        'class' => array('form-row-wide'),
        'required' => false,
        'clear' => true
    );
        
    $merged_billing_fields = $billing_fields2 + $billing_fields;

    return $merged_billing_fields;

}

1 个答案:

答案 0 :(得分:1)

您可以使用 woocommerce_checkout_update_order_metawoocommerce_admin_order_data_after_billing_address 动作挂钩。代码位于您的本机主题 functions.php 文件中。

<块引用>

使用 woocommerce_checkout_update_order_meta 钩子可以 保存价值元。

function custom_checkout_field_update_order_meta( $order_id ) {
    if ( $_POST['billing_vat'] ){ 
        update_post_meta( $order_id, 'billing_vat', esc_attr($_POST['billing_vat']) );
    }
    if ( $_POST['billing_ref'] ){ 
        update_post_meta( $order_id, 'billing_ref', esc_attr($_POST['billing_ref']) );
    }
    if ( $_POST['billing_date'] ){ 
        update_post_meta( $order_id, 'billing_date', esc_attr($_POST['billing_date']) );
    }
}
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
<块引用>

使用 woocommerce_admin_order_data_after_billing_address 钩子 您可以在帐单邮寄地址后显示元值。

function custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('CVR nummer').':</strong> ' . get_post_meta( $order->get_id(), 'billing_vat', true ) . '</p>';
    echo '<p><strong>'.__('Reference nummer').':</strong> ' . get_post_meta( $order->get_id(), 'billing_ref', true ) . '</p>';
    echo '<p><strong>'.__('Ønsket leveringsdato').':</strong> ' . get_post_meta( $order->get_id(), 'billing_date', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );