在Woocommerce编辑订单页面中显示可编辑的自定义字段值

问题描述 投票:3回答:2

我不是开发人员,但不知何故设法添加Woocommerce自定义字段来结帐和订购编辑页面。有类似的问题,但我找不到正确的解决方案。

自定义字段在管理订单编辑页面中可见,但它们不显示值,也不会添加到订单电子邮件中。

我错过了什么?

请在最后看截图。

以下是放在一起的所有代码:

// Woocommerce - Add user custom billing fields
// =============================================================================

function add_woocommerce_admin_billing_fields($billing_fields) {
    $billing_fields['billing_birthday'] = array(
        'label' => __('Datum rojstva', 'woocommerce')
    );
    $billing_fields['billing_socialno'] = array(
        'label' => __('Davčna številka', 'woocommerce')
    );

    return $billing_fields;
}
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');

function add_woocommerce_found_customer_details($customer_data, $user_id, $type_to_load) {
    if ($type_to_load == 'billing') {
        $customer_data[$type_to_load . 'billing_birthday'] = get_user_meta($user_id, $type_to_load . 'billing_birthday', true);
        $customer_data[$type_to_load . 'billing_socialno'] = get_user_meta($user_id, $type_to_load . 'billing_socialno', true);

    }
    return $customer_data;
}
add_filter('woocommerce_found_customer_details', 'add_woocommerce_found_customer_details', 10, 3);

function add_woocommerce_billing_fields($billing_fields) {
    $billing_fields['billing_birthday'] = array(
        'type' => 'tel',
        'label' => __('Datum rojstva'),
        'value' => get_post_meta( $order->id, 'billing_birthday', true ),
        'placeholder' => __('dd/mm/yyyy', 'placeholder'),
        'pattern' => __('\d{1,2}/\d{1,2}/\d{4}', 'pattern' ),
        'class' => array('form-row-first'),
        'required' => true,
        'clear' => true

    );
    $billing_fields['billing_socialno'] = array(
        'type' => 'tel',
        'label' => __('Davčna številka'),
        'value' => get_post_meta( $order->id, 'billing_socialno', true ),
        'placeholder' => _x('8-mestna številka', 'placeholder'),
        'class' => array('form-row-last'),
        'required' => false,
        'clear' => true
    );

    return $billing_fields;
}
add_filter('woocommerce_billing_fields', 'add_woocommerce_billing_fields');

//Doda user meta v backend profil
function add_woocommerce_customer_meta_fields($billing_fields) {
    if (isset($billing_fields['billing']['fields'])) {
        $billing_fields['billing']['fields']['billing_birthday'] = array(
            'label' => __('Datum rojstva', 'woocommerce'),
            'description' => 'Pa kaj bo končno ratalo memo milo?'
        );
        $billing_fields['billing']['fields']['billing_socialno'] = array(
            'label' => __('Davčna številka', 'woocommerce'),
            'description' => ''
        );

    }
    return $billing_fields;
}
add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');

function add_woocommerce_order_fields($address, $order) {
    $address['billing_birthday'] = $order->billing_birthday . get_post_meta($order->id, '_billing_birthday', true) ;
    $address['billing_socialno'] = $order->billing_socialno;
    return $address;
}
add_filter('woocommerce_order_formatted_billing_address', 'add_woocommerce_order_fields', 10, 2);

function add_woocommerce_formatted_address_replacements($replace, $args) {
    $replace['{billing_birthday}'] = !empty($args['billing_birthday']) ? 'Datum rojstva' . $args['billing_birthday'] : '';
    $replace['{billing_socialno}'] = !empty($args['billing_socialno']) ? 'Davčna številka' . $args['billing_socialno'] : '';
    return $replace;
}
add_filter('woocommerce_formatted_address_replacements', 'add_woocommerce_formatted_address_replacements', 10, 2);

function add_woocommerce_localisation_address_formats($formats) {
    $formats['default'] = $formats['default'] . "\n{billing_birthday}\n{billing_socialno}";
    return $formats;
}
add_filter('woocommerce_localisation_address_formats', 'add_woocommerce_localisation_address_formats', 10, 1);

// Change field type to tel woocommerce checkout

function bbloomer_change_checkout_field_input_type() {
echo "<script>document.getElementById('billing_postcode').type = 'tel';</script>";
echo "<script>document.getElementById('billing_birthday').type = 'tel';</script>";

}

add_action( 'woocommerce_after_checkout_form', 'bbloomer_change_checkout_field_input_type'); 

订单编辑页面的屏幕截图

enter image description here

php wordpress woocommerce checkout orders
2个回答
1
投票

我测试了你的代码,只有一些小错误。你很接近让它按预期工作。所以你需要在以下代码中进行一些更改:

1)删除'values',因为这个hooked函数适用于checkout并且不需要值(当你试图从非现有订单中获取它们时更少)。

这样可以避免隐藏的错误,并且当客户已经在以前的追踪中填充了字段时,将显示正确的值...

// Add custom Checkout billing fields
add_filter('woocommerce_billing_fields', 'add_woocommerce_billing_fields', 20, 1);
function add_woocommerce_billing_fields( $billing_fields ) {

    $billing_fields['billing_birthday'] = array(
        'type' => 'tel',
        'label' => __('Datum rojstva'),
        'placeholder' => __('dd/mm/yyyy', 'placeholder'),
        'pattern' => __('\d{1,2}/\d{1,2}/\d{4}', 'pattern' ),
        'class' => array('form-row-first'),
        'required' => true,
        'clear' => true
    );

    $billing_fields['billing_socialno'] = array(
        'type' => 'tel',
        'label' => __('Davčna številka'),
        'placeholder' => _x('8-mestna številka', 'placeholder'),
        'class' => array('form-row-last'),
        'required' => false,
        'clear' => true
    );

    return $billing_fields;
}

// Change field type to tel for woocommerce checkout
add_action( 'woocommerce_after_checkout_form', 'change_checkout_field_input_type');
function change_checkout_field_input_type() {
    echo "<script>document.getElementById('billing_postcode').type = 'tel';</script>";
    echo "<script>document.getElementById('billing_birthday').type = 'tel';</script>";
}

2)这里的键错误,这就是为什么字段值不会显示在管理员订单编辑页面中。

它是'birthday''socialno'而不是'billing_birthday''billing_socialno'

// Setting custom fields Keys/Labels pairs in admin edit order pages and allow edit this fields correctly.
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_admin_billing_fields($billing_fields) {
    $billing_fields['birthday'] = array( 'label' => __('Datum rojstva', 'woocommerce') );
    $billing_fields['socialno'] = array( 'label' => __('Davčna številka', 'woocommerce') );

    return $billing_fields;
}

3)正确获取要在订单编辑页面中显示的缺失字段值。

// Get the field values to be displayed in admin Order edit pages
add_filter('woocommerce_order_formatted_billing_address', 'add_woocommerce_order_fields', 10, 2);
function add_woocommerce_order_fields($address, $order ) {
    $address['billing_birthday'] = get_post_meta( $order->get_id(), '_billing_birthday', true );
    $address['billing_socialno'] = get_post_meta( $order->get_id(), '_billing_socialno', true );
    return $address;
}

4)其他未更改的挂钩功能:

//Doda user meta v backend profil
add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
function add_woocommerce_customer_meta_fields($fields) {
    if (isset($fields['billing']['fields'])) {
        $fields['billing']['billing_birthday'] = array(
            'label' => __('Datum rojstva', 'woocommerce'),
            'description' => 'Pa kaj bo končno ratalo memo milo?'
        );
        $fields['billing']['billing_socialno'] = array(
            'label' => __('Davčna številka', 'woocommerce'),
            'description' => ''
        );
    }
    return $fields;
}

add_filter( 'woocommerce_found_customer_details', 'add_woocommerce_found_customer_details', 10, 3);
function add_woocommerce_found_customer_details($customer_data, $user_id, $type_to_load) {
    if ($type_to_load == 'billing') {
        $customer_data[$type_to_load . 'billing_birthday'] = get_user_meta($user_id, $type_to_load . 'billing_birthday', true);
        $customer_data[$type_to_load . 'billing_socialno'] = get_user_meta($user_id, $type_to_load . 'billing_socialno', true);

    }
    return $customer_data;
}

//add_filter('woocommerce_formatted_address_replacements', 'add_woocommerce_formatted_address_replacements', 10, 2);
function add_woocommerce_formatted_address_replacements($replace, $args) {
    $replace['{billing_birthday}'] = !empty($args['billing_birthday']) ? 'Datum rojstva' . $args['billing_birthday'] : '';
    $replace['{billing_socialno}'] = !empty($args['billing_socialno']) ? 'Davčna številka' . $args['billing_socialno'] : '';
    return $replace;
}

add_filter('woocommerce_localisation_address_formats', 'add_woocommerce_localisation_address_formats', 10, 1);
function add_woocommerce_localisation_address_formats($formats) {
    $formats['default'] = $formats['default'] . "\n{billing_birthday}\n{billing_socialno}";
    return $formats;
}

代码位于活动子主题(或活动主题)的function.php文件中。


电子邮件通知 - 显示自定义字段(及其标签)。

通过您的活动子主题覆盖Woocommerce模板emails/email-addresses.php

可以通过复制来覆盖此模板: plugin/woocommerce/templates/emails/email-addresses.phpyourtheme/woocommerce/emails/email-addresses.php ...... 官方文件:Template structure & Overriding templates via a theme

您将在第34行(在结算电话之后)插入以下内容:

<?php
    // Billing birthday
    $billing_birthday = get_post_meta($order->get_id(), '_billing_birthday', true );
    echo $billing_birthday ? '<br/>'.__('Datum rojstva', 'woocommerce').': '.$billing_birthday : '';

    // Billing socialno
    $billing_socialno = get_post_meta($order->get_id(), '_billing_socialno', true );
    echo $billing_socialno ? '<br/>'.__('Davčna številka', 'woocommerce').': '.$billing_socialno : '';
?>

经过测试和工作。


0
投票

如果您想添加自定义字段,则不仅仅是添加新的结算明细,而且无论如何它都很简单。

我建议你使用hook woocommerce_admin_order_data_after_billing_address,所以它会是这样的:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'mishafunction' );        
function mishafunction( $order ){        
    $x = get_post_meta( $order->get_order_number(), 'CUSTOM FIELD NAME', true );
    ?>
    <div class="address">
    <p<?php if( !$x ) echo ' class="none_set"' ?>>
        <strong>Datum rojstva:</strong>
        <?php echo ( $x ) ? $x : '' ?>
    </p>
    </div>
    <div class="edit_address"><?php
    woocommerce_wp_text_input( array(
        'id' => 'CUSTOM FIELD NAME',
        'label' => 'Datum rojstva:',
        'value' => $x,
        'wrapper_class' => 'form-field-wide'
    ) );
    ?></div><?php
}

add_action( 'woocommerce_process_shop_order_meta', 'misha_save_it' );

function misha_save_it( $order_id ){
    update_post_meta( $order_id, 'CUSTOM FIELD NAME', wc_clean( $_POST[ 'CUSTOM FIELD NAME' ] ) );
}

您还可以查看完整的教程here

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