Only Allow 1 Product in the Cart

I have a website on which I sell a service. I want to limit only one product in the cart. For that I was reading and I found this article.
( I added the snippet in functions.php of my child theme, but no avail. Please advise how should I get this implemented