D
D
danyfir2018-03-02 13:38:17
WordPress
danyfir, 2018-03-02 13:38:17

How to display number of woocommerce orders?

I want to make a note on the site "1,868 orders have been placed on our site."
5a9928eae4c5d645904835.png
How to get this number? I didn’t find something in the woocommerce and wordpress API :(
From somewhere it is taken in the admin panel

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ADvi, 2018-03-02
@ADvi

function display_woocommerce_order_count( $atts, $content = null ) {
    $args = shortcode_atts( array(
        'status' => 'completed',
    ), $atts );
    $statuses    = array_map( 'trim', explode( ',', $args['status'] ) );
    $order_count = 0;
    foreach ( $statuses as $status ) {
        if ( 0 !== strpos( $status, 'wc-' ) ) {
            $status = 'wc-' . $status;
        }
        $order_count += wp_count_posts( 'shop_order' )->$status;
    }
    ob_start();
    echo number_format( $order_count );
    return ob_get_clean();
}
add_shortcode( 'wc_order_count', 'display_woocommerce_order_count' );

where the shortcode with the parameters [wc_order_count status="completed,pending"] will display the corresponding values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question