Display only "completed" items on account page Woocommerce

I have customised the “My Account” area of Woocommerce to have a custom page on the side menu entitled “Collections”. In here (see attached image) it shows all the orders made by the user.

I would like it so it just displays the “completed” as this is a collection of the products they have brought.

At the moment to get this to play on the custom page I have used this:

function shortcode_my_orders( $atts ) {
    extract( shortcode_atts( array(
        'order_count' => -1
    ), $atts ) );

    ob_start();
    wc_get_template( 'myaccount/my-orders.php', array(
        'current_user'  => get_user_by( 'id', get_current_user_id() ),
        'order_count'   => $order_count
    ) );
    return ob_get_clean();
}
add_shortcode('my_orders', 'shortcode_my_orders');

and used the shortcode creation to display:

echo do_shortcode( ' [my_orders order_counts=2] ' );

Please advise,
thank you!!!
Jason

Hi There,

Please update your code to this:

function shortcode_my_orders( $atts ) {
    extract( shortcode_atts( array(
        'order_count' => -1
    ), $atts ) );

    ob_start();
    wc_get_template( 'myaccount/my-orders.php', array(
        'current_user'  => get_user_by( 'id', get_current_user_id() ),
        'order_count'   => $order_count,
        'post_status'	=> 'wc-completed'
    ) );
    return ob_get_clean();
}
add_shortcode('my_orders', 'shortcode_my_orders');

Hope it helps :slight_smile:

Thank you for the update! I tried this not nothing seemed to change - did I have to add anything else?

Kind Regards,
Jason

Hi Jason,

Please keep your old code, then also add the following code:

add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_orders_query' );
function custom_my_orders_query($args){
	$args['post_status'] = array('wc-completed');
	return $args;
}

Thank you very much, looks like we are almost there!

It does work fine on the custom collection page but on the general orders page it now only shows completed when this should be kept to all orders (processing etc)

Kind Regards,
Jason

Hi there,

What’s your custom collection page and the general order page? Would you mind providing your login credentials in a secure note?

You might need something like this, but I’m not sure the identity of your collections page ( I just added custom-collection as page slug/name as a sample)

add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_orders_query' );
function custom_my_orders_query($args){
	if ( is_page('custom-collection') ) $args['post_status'] = array('wc-completed');
	return $args;
}

Thanks!

Good morning and thank you for the reply, that makes sense what you said but it did not seem to work.

In the functions page here is what I have put together to create the collections page:

Created the shortcode to show completed orders

function shortcode_my_orders( $atts ) {
    extract( shortcode_atts( array(
        'order_count' => -1
    ), $atts ) );

    ob_start();
    wc_get_template( 'myaccount/my-orders.php', array(
        'current_user'  => get_user_by( 'id', get_current_user_id() ),
        'order_count'   => $order_count
    ) );
    return ob_get_clean();
}
add_shortcode('my_orders', 'shortcode_my_orders');

Filter to completed:

add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_orders_query' );
function custom_my_orders_query($args){
	 $args['post_status'] = array('wc-completed');
	return $args;
}

Created the custom page:

// 1. Register new endpoint to use for My Account page
// Note: Resave Permalinks or it will give 404 error
 
function bbloomer_add_collection_endpoint() {
    add_rewrite_endpoint( 'collection', EP_ROOT | EP_PAGES );
}
 
add_action( 'init', 'bbloomer_add_collection_endpoint' );
 
 
// ------------------
// 2. Add new query var
 
function bbloomer_collection_query_vars( $vars ) {
    $vars[] = 'collection';
    return $vars;
}
 
add_filter( 'query_vars', 'bbloomer_collection_query_vars', 0 );
 
 
// ------------------
// 3. Insert the new endpoint into the My Account menu
 
function bbloomer_add_collection_link_my_account( $items ) {
    $items['collection'] = 'Collection';
    return $items;
}
 
add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_collection_link_my_account' );
 
 
// ------------------
// 4. Add content to the new endpoint
 
function bbloomer_collection_content() {
//echo '<h3>Premium WooCommerce Support</h3></p>';
echo do_shortcode( '[my_orders]' );
}

Added to the menus on my account page:

add_action( 'woocommerce_account_collection_endpoint', 'bbloomer_collection_content' );

function wpb_woo_my_account_order() {
	$myorder = array(
		'dashboard'          => __( 'Dashboard', 'woocommerce' ),
		'wishlist'			 => __( 'Wishlist', 'woocommerce' ),
		'orders'             => __( 'Orders', 'woocommerce' ),
		'collection'          => __( 'Collection', 'woocommerce' ),
		'edit-address'       => __( 'Address', 'woocommerce' ),
		'edit-account'       => __( 'Password', 'woocommerce' ),
		'customer-logout'    => __( 'Logout', 'woocommerce' ),
	);
	return $myorder;
}
add_filter ( 'woocommerce_account_menu_items', 'wpb_woo_my_account_order' );

So the orders page should just be all orders (completed, pending, hold etc) where the collection should just be completed.

I will add your details now thank you!

Hi There,

I changed a bit in this functions. Could you please check again?

add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_orders_query' );
function custom_my_orders_query($args){
	if( is_wc_endpoint_url('collection') ) $args['post_status'] = array('wc-completed');
	return $args;
}

Good afternoon and thank you for the update - however it doesnt seem to have done anything

Kind Regards,
Jason

Hi Jason,

On your code where you created a shortcode to show completed orders, it is just getting orders not specifically completed. Please adjust that code and use wc_customer_bought_product function instead. See the following guide:


https://docs.woocommerce.com/wc-apidocs/function-wc_customer_bought_product.html

Good morning Lely and thank you for the advice. I have updated the code and now the page does not display anything… However no error has occurred.

I used the first link provided.

  1. creating the shortcode:
add_shortcode( 'my_products', 'bbloomer_user_products_bought' );
 
function bbloomer_user_products_bought() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 3;
$current_user = wp_get_current_user();
$args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'meta_query'            => array(
        array(
            'key'           => '_visibility',
            'value'         => array('catalog', 'visible'),
            'compare'       => 'IN'
        )
    )
);
$loop = new WP_Query($args);
 
ob_start();
 
woocommerce_product_loop_start();
 
while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $theid ) ) {
wc_get_template_part( 'content', 'product' ); 
} 
endwhile; 
 
woocommerce_product_loop_end();
 
woocommerce_reset_loop();
wp_reset_postdata();
 
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
  1. adding it to my custom collections page (i.e. completed orders)

    function bbloomer_collection_content() {
    echo do_shortcode( ‘[my_products]’ );
    }

What am I doing incorrectly?

Kind Regards,
Jason

Hi Jason,

I didn’t see any error. The list is just blank. Make sure that when checking in this function, you are logged in as user with the successful/completed order else it will not display anything.

Regretfully, further issue from here is outside the scope of our support as this is not related to an issue with the theme and instead has to do with your customization of it. As such, you will need to investigate this particular issue on your own or seek help from a developer should you not feel comfortable making these changes yourself. We do have an in-house custom development team that offer paid services, who may be able to assist. They can be contacted at pinnacle@theme.co if this is of interest to you. If you have any further questions about the theme, we are more than happy to provide you with assistance on these inquiries.

Thank you for your understanding.

No problem, thank you for trying to help. Just to test it I did do a completed order and again nothing displayed. It just generated a blank product “< ul >” when inspecting. Odd

Kind Regards,
Jason

Hi there,

You may also check this line

wc_get_template_part( 'content', 'product' );

And see if the correct template is being picked up correct, else, you’ll have to create your own template for that. And you may forward it to your developer :slight_smile:

Thanks!

Thank you for the update, I am currently experimenting and hopefully will crack it!

Jason

You’re welcome! :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.