在Woocommerce管理员订单列表自定义列中显示结帐字段值

时间:2018-11-16 14:05:17

标签: php wordpress woocommerce checkout orders

我被一项任务困住了。我想在woocommerce后端的车间订单列中添加一个额外的列。 如果客户在结帐时选中了复选框字段,则此额外的列应显示回显输出。

因此添加额外的列并不难。我是这样做的。

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;
}

所以现在我想在此添加的列中显示一些内容。结帐页面上复选框的功能如下。它已经在订单编辑页面上显示了回显输出。

// 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>';
}

因此,我认为应该可以抓取$my_field_name变量并将其放入这样的新额外列中。

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!';
   } 
}

但这在添加的列中给了我一个“未定义的变量”错误。

如果仅将echo 'Rechnung beilegen!';放入函数中,它将在MY_COLUMN_ID_2中的每一行中输出“ Rechnung beilegen”。 像这样:

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!';
   } 
}

所以问题是:  如何根据在$my_field_nameMY_COLUMN_ID_2中所做的选择获得输出?

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

以下重新访问的代码将添加一个自定义列,并显示自定义结帐字段“封闭的发票”值:

// 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 ' - ';
        }
    }
}

代码进入您的活动子主题(活动主题)的function.php文件中。经过测试并可以正常工作。

enter image description here

  

自Woocommmerce 3.3版以来,管理订单列表操作列已重命名为'wc_actions'而不是'order_actions'