Persistent Cart - Improve for shared user table scenario
Please consider the "#" to be an underscore character in the below post. If I had used the underscore it would have interpreted it as a command to italicize the text.
The persistent cart is a record in the user_meta table with key
"#woocommerce#persistent#cart#" . get#current#blog#id()
But this is not a unique identifier when a shared user table is being employed.
To illustrate. Suppose there are two websites, each with its own woocommerce store.
The db prefix for the first site is "main".
The db prefix for the second one is "specialevent"
The second site does not use its own user table. Instead it is set to share the first one's with the following in its wp config.
define( 'CUSTOM#USER#TABLE', 'main#users' );
define( 'CUSTOM#USER#META#TABLE', 'main#usermeta' );
However, the command
always returns "1" no matter what site you visit.
It is not a unique identifier.
Consequently when a user goes to one site and creates a persistent cart it will overwrite whatever persistent cart existed in the other site by overwriting the user meta data with key #woocommerce#persistent#cart#1
Using the db prefix instead of "get#current#blog#id()" would be a possible way to go. This is the way wordpress does the "capabilities" user meta data for example. For each user, each site has its own user meta record as follows:
P.S. Where this can be a big problem is if you are trying to keep track of stock by causing the items ordered to be reserved in some way when added to cart and unreserved when removed from the cart. When the cart gets over-written, such reservations will not be removed correctly.
More info about shared user table: