php - 在 Woocommerce 管理订单列表自定义列中显示结帐字段值
问题描述
我被一项任务困住了。我想在 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!';
函数中,它会将“Rechnung beilegen”输出到 MY_COLUMN_ID_2 的每一行中。像这样:
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_name
into中的选择获得输出MY_COLUMN_ID_2
?
任何帮助表示赞赏。
解决方案
以下重新访问的代码将添加一个自定义列,并将显示自定义结帐字段“Enclosed Invoice”值:
// 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 文件中。测试和工作。
从 Woocommmerce 3.3 版开始,管理订单列表操作列已被重命名
'wc_actions'
,而不是'order_actions'
推荐阅读
- c# - 模型绑定忽略具有 JsonIgnore 属性的属性
- python - How to build a Python function to build Pandas DataFrames dynamically?
- r - 显示 3 个因素 ggplot geom
- python - 在 Pandas 中出现错误:IndexError: index 0 is out of bounds for axis 0 with size 0
- c# - How to store and list out variables in Selenium code?
- c# - Unity Windows Overlay
- postgresql - st_intersects 返回的观测值过多
- html - @media only screen and (max-width: --px) not working?
- wordpress - Udating custom WooCommerce mini cart when cart contents get changed
- angular - Importing Semantic-UI only CSS in an Angular (v8.0.0) project