Display checkout field value in Woocommerce admin order list custom column

天涯浪子 提交于 2021-01-29 02:24:42

问题


I am stuck with a task. I would like to add an extra column to the shop order columns in woocommerce backend. This extra column should display an echo output if a customer checked a checkbox field on checkout.

So to add the extra column it is not that hard. I did it this way.

add_filter('manage_edit-shop_order_columns', 'invoice_order_overview');

function invoice_order_overview($columns) {
$new_columns = (is_array($columns)) ? $columns : array();
unset($new_columns['order_actions']);

//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['MY_COLUMN_ID_2'] = 'Extra Column';
//stop editing

$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}

So now I would like to display something in this added column. The function for the checkbox at the checkout page is as follow. It already displays an echo output at the order edit page.

// Add custom checkbox field to checkout 
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'     => __('Rechnung beilegen? (Sonst nur Lieferschein)'),
),  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 style="background: #dba029; padding: 1em !important; 
color: #fff; font-weight: 700;"><strong>Rechnung beilegen! </strong> 
</p>';
}

So I thought it should be possible to grab that $my_field_name variable and put it to my new extra column like this.

add_action('manage_shop_order_posts_custom_column', 'invoice_order_overview_value', 2);

function invoice_order_overview_value($column) {
global $post;

if ($column == 'MY_COLUMN_ID_2') {

$my_field_name = get_post_meta( $order->get_id(), 'my_field_name', true );
   if( $my_field_name == 1 )
      echo 'Rechnung beilegen!';
   } 
}

But this is giving me an "undefined variable" error in the added column.

If I only put echo 'Rechnung beilegen!'; into the function it outputs "Rechnung beilegen" into every row in MY_COLUMN_ID_2. Like this:

add_action('manage_shop_order_posts_custom_column', 'invoice_order_overview_value', 2);

function invoice_order_overview_value($column) {
global $post;

if ($column == 'MY_COLUMN_ID_2') {

      echo 'Rechnung beilegen!';
   } 
}

So the question is: How can I get the output based on the selection made in $my_field_name into MY_COLUMN_ID_2 ?

Any help is appreciated.


回答1:


The following revisited code will add a custom column and will display the custom checkout field "Enclosed Invoice" value:

// Add custom checkbox field to checkout
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( '_enclosed_invoice', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Enclose invoice? (Otherwise only delivery note)'),
    ),  WC()->checkout->get_value( '_enclosed_invoice' ) );

    echo '</div>';
}

// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_create_order', 'save_order_custom_meta_data', 10, 2 );
function save_order_custom_meta_data( $order, $data ) {
    if ( isset($_POST['_enclosed_invoice']) )
        $order->update_meta_data('_enclosed_invoice', '1' );
}

// 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 ){
    if( $my_field_name = $order->get_meta( '_enclosed_invoice' ) )
        echo '<p style="background: #dba029; padding: 1em !important; color: #fff; font-weight: 700;"><strong>Enclosed invoice!</strong></p>';
}

// Add custom column before "Actions" column in admin orders list
add_filter('manage_edit-shop_order_columns', 'add_enclosed_invoice_order_column', 10, 1 );
function add_enclosed_invoice_order_column( $columns ) {
    // Woocommerce compatibility since version 3.3
    $actions_key = isset($columns['wc_actions']) ? 'wc_actions' : 'order_actions';

    $order_actions = $columns[$actions_key];

    unset($columns[$actions_key]);

    $columns['enclosed_invoice'] = __("Enc. Invoice", "woocommerce");

    $columns[$actions_key] = $order_actions;

    return $columns;
}

// Display data to custom column in admin orders list
add_action( 'manage_shop_order_posts_custom_column' , 'display_enclosed_invoice_order_column_data' );
function display_enclosed_invoice_order_column_data( $column ) {
    global $the_order, $post;

    if( $column  == 'enclosed_invoice' ) {
        if( $enclosed_invoice = $the_order->get_meta( '_enclosed_invoice' ) ) {
            echo __("Yes", "woocommerce");
        } else {
            echo ' - ';
        }
    }
}

Code goes in function.php file of your active child theme (active theme). tested and works.

Since Woocommmerce version 3.3, the admin order list actions column has been renamed 'wc_actions' instead of 'order_actions'



来源:https://stackoverflow.com/questions/53339405/display-checkout-field-value-in-woocommerce-admin-order-list-custom-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!