以下重新访问的代码将添加自定义列,并显示自定义签出字段“随附发票”值:
// 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文件。测试和工作。
'wc_actions'
'order_actions'