This depends a lot on your business policy, especially when it comes to deleting the data periodically or not. There are business models that do not exclude and there are others that exclude (often a wrong choice). I'd rather never delete anything. When you need to remove something irrelevant but important for future research, move that data to another bank or other archival table, history, etc.
On how to refer a user to the cart, you can do this with the customer ID that is logged in, as already mentioned. However, for logged in users, you can use a cookie with an ID that relates to that information.
It is recommended that you save your cart data, even those data from unlogged users as this is very useful for analyzing user behavior, who abandons the cart, where the cart was abandoned, what users normally enter in the cart, quantity, value, dates, etc. All of this is very important and interesting to drive marketing campaigns and logistics itself.
Another factor is that it makes the user more user-friendly when the cart is saved. Often it can happen to lose the connection and the user want to go back to the store later. Then you will not have the job of refilling the cart. When the user returns and sees that the cart has been saved, the probability of the purchase is greater. Otherwise, he will feel that he will have more work in redoing the purchase, discouraging it and even giving up the purchase.
These are the main and most obvious points of why you should save your cart data.
Of course, over time the volume of data will begin to weigh heavily. At this point you and / or the business owners decide what to do with old data. Whether it will delete or not, or just move to a history archive, this is a particular decision. As I mentioned above, I'd rather not exclude anything. Even if it appears to be junk, sometime in the future you may need this data, either for logistics or to analyze traces of malicious users, for example.