【问题标题】:How to add extra field to Woo Commerce checkout and display in edit order page [duplicate]如何在 Woocommerce 结帐中添加额外字段并在编辑订单页面中显示 [重复]
【发布时间】:2021-07-10 07:01:34
【问题描述】:

我在 WooCommerce 结帐中额外添加了三个字段(CVR 编号、参考编号和 ønsket 杠杆数据)。它们在结账时正确显示,但一旦下订单,信息就不会发送到 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;

}

【问题讨论】:

    标签: php wordpress woocommerce checkout orders


    【解决方案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 );
    

    【讨论】:

    • 你如何让它也包含在发送给我们的 WooCommerce 电子邮件中,以便我们知道要打包什么等?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 2020-08-03
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多