如何在 WooCommerce 账单数据中显示新字段的数据?

问题描述 投票:0回答:1

如何在 WooCommerce 账单数据中显示新字段的数据?

我在 WooCommerce 的“我的帐户”页面上的账单地址中创建了两个新字段。 这些仅对某些用户角色(“管理员”)可见,

结帐时显示的字段(在此屏幕截图中 - checkout.jpg)并且是:

1 - 带有文本标签的复选框:

'label' => __('I have Surcharge'
(这是我需要在电子邮件中显示的)

这个字段是用下面的代码创建的

add_filter( 'woocommerce_billing_fields', 'custom_checkbox_below_billing_company' );

function custom_checkbox_below_billing_company( $fields ) {
    //Show the checkbox only to users with the role "administratorr"
    if ( user_can( wp_get_current_user(), 'administrator' ) ) {
        $fields['billing_custom_checkbox'] = array(
            'type'          => 'checkbox',
            'label'         => __('Tengo Recargo', 'woocommerce'),
            'class'         => array('form-row-wide'),
            'priority'      => 30,
            'required'      => false,
            'clear'         => true,
            'default'       => 0,
        );
    }
    return $fields;
}

// Save the value of the checkbox in the database
add_action( 'woocommerce_customer_save_address', 'save_billing_custom_checkbox', 10, 2 );

function save_billing_custom_checkbox( $customer_id, $load_address ) {
    if ( isset( $_POST['billing_custom_checkbox'] ) ) {
        $checkbox_value = sanitize_text_field( $_POST['billing_custom_checkbox'] );
        update_user_meta( $customer_id, 'billing_custom_checkbox', $checkbox_value );
    } else {
        update_user_meta( $customer_id, 'billing_custom_checkbox', '0' );
    }
}

/* SENDING THE FIELD IN THE POST*/

// Show checkbox label in billing address
add_action( 'woocommerce_admin_billing_fields', 'show_billing_custom_checkbox_label' );
add_action( 'woocommerce_billing_fields', 'show_billing_custom_checkbox_label' );

function show_billing_custom_checkbox_label( $fields ) {
    if ( isset( $fields['billing_custom_checkbox'] ) ) {
        $fields['billing_custom_checkbox']['label'] .= ' ' . __('(Tengo Recargo de Ekivalencia)', 'woocommerce');
    }
    return $fields;
}


add_filter( 'woocommerce_email_customer_details_fields', 'add_custom_checkbox_to_emails', 10, 3 );

function add_custom_checkbox_to_emails( $fields, $sent_to_admin, $order ) {
    $checkbox_value = get_user_meta( $order->get_user_id(), 'billing_custom_checkbox', true ); // Get the value of the checkbox
    $fields['billing_custom_checkbox'] = array(
        'label' => __('Tengo Recargo de Ekivalencia', 'woocommerce') . ' ' . ($checkbox_value ? 'SI' : 'NO'), //Add the value of the checkbox in the label
        'value' => $checkbox_value ? 'SI' : 'NO', // Agrega el valor del checkbox en el valor
    );
    return $fields;
}

2 - 输入公司税号的字段,NIF/CIF:

'label' => __('NIF/CIF', 'woocommerce'),

NIF/CIF 字段已创建如下,但在发送给客户和管理员的电子邮件中的显示方式失败

function add_custom_billing_field($fields)
{
  // Get the role of the current user
  $user_id = get_current_user_id();
  $user_role = '';
  if ($user_id) {
    $user = get_userdata($user_id);
    if ($user && isset($user->roles[0])) { //Check if user role exists
      $user_role = $user->roles[0];
    }
  }

  // Show the field only for the desired user role
  if ($user_role == 'administrator') {

    $current_user = wp_get_current_user();
    $saved_license_no = $current_user->license_no;

    // Add the NIF/CIF field to the billing address editing form
    $fields['billing_license_no'] = array(
      'label'       => __('NIF/CIF', 'woocommerce'),
      'placeholder' => __('B12345678', 'woocommerce'),
      'required'    => true,
      'clear'       => false,
      'type'        => 'text',
      'default'     => $saved_license_no,
      'class'       => array('form-row-wide'),
      'priority'    => 25, 
    );
  }

  //Return the updated array of billing fields
  return $fields;
}

/* save the new field and show it in email, checkout, etc... */
add_action('woocommerce_checkout_update_order_meta', 'bbloomer_save_new_checkout_field');
function bbloomer_save_new_checkout_field($order_id)
{
  if (isset($_POST['billing_license_no'])) { //Check if field value was sent
    update_post_meta($order_id, '_license_no', wc_clean($_POST['billing_license_no'])); //Clear value before saving
  }
}


add_action('woocommerce_thankyou', 'bbloomer_show_new_checkout_field_thankyou');
function bbloomer_show_new_checkout_field_thankyou($order_id)
{
  if (get_post_meta($order_id, '_license_no', true)) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta($order_id, '_license_no', true) . '</p>';
}

add_filter('woocommerce_order_details_after_order_table', 'bbloomer_show_new_checkout_field_order', 20, 1);
add_action('woocommerce_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order');
function bbloomer_show_new_checkout_field_order($order)
{
  $order_id = $order->get_id();
  if (get_post_meta($order_id, '_license_no', true)) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta($order_id, '_license_no', true) . '</p>';
}

add_action('woocommerce_email_after_order_table', 'bbloomer_show_new_checkout_field_emails', 20, 4);

function bbloomer_show_new_checkout_field_emails($order, $sent_to_admin, $plain_text, $email)
{
  if (get_post_meta($order->get_id(), '_license_no', true)) echo '<p><strong>NIF/CIF:</strong> ' . get_post_meta($order->get_id(), '_license_no', true) . '</p>';
}

这些字段是从用户的帐户页面编辑的,my-account/edit-address/billing/ 并且在下订单时也会显示在 Checkout 上。

我只需要将这些数据作为 WooCommerce 账单数据的一部分,并与其他数据一起发送:姓名、姓氏、公司名称、地址等。

可以在截图上看到。 //// 管理员邮件

现在它们通过邮件发送但在地址范围之外,如您所见在屏幕截图中(administrator-Mail.png)

但是这个数据不是WooCommerce结构的一部分,所以当我生成发票时没有显示(发票截图-bill.jpg)

我应该怎么做才能使这些数据成为 WooCommerce 帐单地址的一部分?

也许可以通过编辑WooCommerce结构来实现,但我不知道这是否可能,如果可以,将如何完成?

如何以最安全的方式做到这一点?

php wordpress woocommerce checkbox checkout
1个回答
0
投票

要在 WooCommerce 账单数据中显示新字段的数据,您需要将自定义代码添加到:

  1. 将新字段添加到账单表格
  2. 将新字段数据保存到订单元数据
  3. 在订单详情页、邮件等相关地方展示新的字段数据

这里是一个示例代码片段,演示了如何为 Company Name 添加一个新字段到 WooCommerce 账单表单并将数据保存到订单元:

// Step 1: Add new field to billing form
add_filter( 'woocommerce_billing_fields', 'add_company_name_field' );

function add_company_name_field( $fields ) {
    $fields['billing_company_name'] = array(
        'label'       => __( 'Company Name', 'woocommerce' ),
        'required'    => false,
        'class'       => array( 'form-row-wide' ),
        'clear'       => true,
        'priority'    => 25,
    );

    return $fields;
}

// Step 2: Save new field data to order meta
add_action( 'woocommerce_checkout_create_order', 'save_company_name_field' );

function save_company_name_field( $order ) {
    if ( ! empty( $_POST['billing_company_name'] ) ) {
        $order->update_meta_data( '_billing_company_name', sanitize_text_field( $_POST['billing_company_name'] ) );
    }
}

// Step 3: Display new field data in relevant places
add_filter( 'woocommerce_order_formatted_billing_address', 'display_company_name_in_billing_address', 10, 2 );

function display_company_name_in_billing_address( $address, $order ) {
    $company_name = $order->get_meta( '_billing_company_name', true );

    if ( $company_name ) {
        $address .= "\n" . $company_name;
    }

    return $address;
}

在此示例代码中,我们使用

woocommerce_billing_fields
过滤器将“公司名称”的新字段添加到账单表单。然后,我们使用
woocommerce_checkout_create_order
操作将该字段中的数据保存到订单元数据中。

最后,我们使用

woocommerce_order_formatted_billing_address
过滤器在订单详情页面和电子邮件的账单地址部分显示公司名称

您可以根据需要自定义字段名称、标签和元键。此外,您可能需要更新其他使用计费数据的函数,以包含新的字段数据。

© www.soinside.com 2019 - 2024. All rights reserved.