Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Last active February 5, 2019 16:37
Show Gist options
  • Save kartikparmar/f6ea64a2897deb618f3584349557c82e to your computer and use it in GitHub Desktop.
Save kartikparmar/f6ea64a2897deb618f3584349557c82e to your computer and use it in GitHub Desktop.
Function to display content of WooCommerce orders dashboard widget.
<?php
/**
* Create the function to output the contents of our Dashboard Widget.
*/
function wc_orders_dashboard_widget_function() {
$args = array(
'post_type' => 'shop_order',
'post_status' => 'All',
'posts_per_page' => 5
);
$orders = get_posts( $args );
if( count( $orders ) > 0 ) {
?>
<table width="100%" class="vao_orders_table">
<tr>
<th><?php _e( 'Order Id', 'woocommerce' ); ?></th>
<th><?php _e( 'Order Status', 'woocommerce' ); ?></th>
<th><?php _e( 'Action', 'woocommerce' ); ?></th>
</tr>
<?php
foreach ( $orders as $key => $value ) {
?>
<tr>
<td>
<?php
$order = new WC_Order( $value->ID );
// 1. Get the order ID and its link
if ( $order ) {
echo '<a href="' . admin_url( 'post.php?post=' . $order->get_order_number() . '">Order #' . $order->get_order_number() . '</a>';
?>
</td>
<td>
<?php
// 2. Get status of the order
echo esc_html( wc_get_order_status_name( $order->get_status() ) );
}
?>
</td>
<td>
<?php
// 3. Adding action buttons for each order
do_action( 'woocommerce_admin_order_actions_start', $order );
$actions = array();
if ( $order->has_status( array( 'pending', 'on-hold' ) ) ) {
$actions['processing'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $value->ID ), 'woocommerce-mark-order-status' ),
'name' => __( 'Processing', 'woocommerce' ),
'action' => "processing",
);
}
if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) {
$actions['complete'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $value->ID ), 'woocommerce-mark-order-status' ),
'name' => __( 'Complete', 'woocommerce' ),
'action' => "complete",
);
}
$actions['view'] = array(
'url' => admin_url( 'post.php?post=' . $value->ID . '&action=edit' ),
'name' => __( 'View', 'woocommerce' ),
'action' => "view",
);
$actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $order );
foreach ( $actions as $action ) {
printf( '<a class="button tips %s" href="%s" data-tip="%s">%s</a> ', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) );
}
do_action( 'woocommerce_admin_order_actions_end', $order );
?>
</td>
</tr>
<?php
}
?></table><?php
// 4. Adding All orders link which will redirect to Orders page of WooCommerce
printf( '<div id="vao_orders"><span class="dashicons dashicons-cart"></span> <a href="%s">' . __( 'View all orders' ) . '</a></div>', admin_url( 'edit.php?post_type=shop_order' ) );
}else{
// If no orders then display No Orders message
echo __( 'No Orders.' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment