My widget displays recent Woocommerce orders within the wordpress admin dashboard. It works correctly when I remove the billing_first_name field, but when I add this field it creates an error within the code.
How can I add the billing_first_name field to work correctly within my admin widget so it displays all values.
/**
* 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
}