How to sort products in shop page by defined category order?

I am currently working on sorting the shop page products based on a defined category order and stock count.

I got the stock count sorting working but I am unable to find a proper way to implement sort by category along with it.

I have many categories listed in my woocommerce site in that I would like to have few category products to be listed first and rest as follows.

Currently I am using the following code to sort the products by stock count and date. I need some help on adding the category sort along with it.