php - 在最近的订单小部件中添加 billing_first_name (Woocommerce)
问题描述
我的小部件在 wordpress 管理仪表板中显示最近的 Woocommerce 订单。当我删除 billing_first_name 字段时它可以正常工作,但是当我添加此字段时,它会在代码中创建一个错误。
如何添加 billing_first_name 字段以在我的管理小部件中正常工作,以便显示所有值。
/**
* Add a widget to the dashboard.
*
*/
function wc_orders_dashboard_widgets() {
wp_add_dashboard_widget(
'wc_order_widget_id', // Widget slug.
'Cartlog Orders', // Title.
'wc_orders_dashboard_widget_function' // Display function.
);
}
add_action( 'wp_dashboard_setup', 'wc_orders_dashboard_widgets' );
/**
* Pulls the order information to the dashboard widget.
*/
function wc_orders_dashboard_widget_function() {
$args = array(
'post_type' => 'shop_order',
'post_status' => 'All', //Other options available choose one only: 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-cancelled', 'wc-refunded', 'wc-failed'
'posts_per_page' => 10 //Change this number to display how many orders you want to see
);
$orders = get_posts( $args );
if( count( $orders ) > 0 ) {
?>
<table width="100%">
<tr>
<th><?php _e( 'Order Id', 'woocommerce' ); ?></th>
<th><?php _e( 'Total', 'woocommerce' ); ?></th>
<th><?php _e( 'Billing Name', 'woocommerce' ); ?></th>
<th><?php _e( 'Status', 'woocommerce' ); ?></th>
</tr>
<?php
foreach ( $orders as $key => $value ) {
?>
<tr>
<td>
<?php
$order = new WC_Order( $value->ID );
// 1. Get the order ID
if ( $order ) {
echo '<a href="'. admin_url( 'post.php?post=' . absint( $order->id ) . '&action=edit' ) .'" >' . $order->get_order_number() . '</a>';
?>
</td>
<td>
<?php
// 2. Get the order total
echo esc_html( wc_get_order_status_name( $order->get_total() ) );
}
?>
</td>
<td>
<?php
// 3. Get the billing name
echo esc_html(wc_get_order_status_name($order>get_billing_first_name() ) );
}
?>
</td>
<td>
<?php
// 4. Get the order status
echo esc_html( wc_get_order_status_name( $order->get_status() ) );
}
?>
</td>
</tr>
<?php
}
?></table>
<?php
}
解决方案
在显示订单总额后关闭 if 语句,然后为3
and重复右大括号4
。您还将传递wc_get_order_status_name()
仅适用于订单状态的所有显示值。
获取最新订单
在解决上面突出显示的问题之前,值得从检索最新订单的方法开始。您正在使用get_posts()
目前可能有效但不建议使用的方法。WooCommerce 正在转向自定义表格,因此任何假设订单是帖子的代码将来都会中断。你最好使用 WooCommerce 提供的功能。
$orders = wc_get_orders([
'limit' => 10,
'orderby' => 'date',
'order' => 'DESC',
]);
https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
这种方法的一个很好的好处是您可以返回一个WC_Order
对象数组,而不是需要转换的帖子。
显示订单
foreach ( $orders as $order ) { ?>
<tr>
<td>
<?php printf(
'<a href="%1$s">%2$s</a>',
$order->get_edit_order_url(),
$order->get_order_number()
); ?>
</td>
<td><?php echo $order->get_total(); ?></td>
<td><?php echo $order->get_billing_first_name(); ?></td>
<td><?php echo wc_get_order_status_name( $order->get_status() ); ?></td>
</tr>
<?php }
推荐阅读
- spring-security - Spring SAML:经常发送 AuthnRequest
- javascript - 使用 javascript 更新浏览器缓存
- networking - 将生产流量从外部云迁移到 Google Cloud 项目的最佳方式是什么?
- python - 链接列表中的合并排序
- vb.net - 如何将水晶报表中的文本对象字体类型更改为条形码字体
- python - 是否有适用于 Python 3.7.1 的 Microsoft Visual C++ 编译器?
- authorization - XACML 在策略中结合 PIP
- angularjs - XSLT 的自定义 XML 属性名称
- python - 通过 pyodbc 查询时未显示新创建的 SQL 用户
- vb.net - 为什么是 WebClient.DownloadStringAsync 排队请求以及如何加快它?