I am developing a plugin for WooCommerce.
On my backend API server (not my Wordpress server but another server that maintains a duplicate of the cart record) I need a way to tell if the cart I am dealing with is the same cart as before (the cart items or details could have changed, but as far as the consumer who is using my WP site can tell they are still in the same shopping cart experience).
I need something that will serve as a unique identifier for the shopping cart.
As far as I can see there is no explicit ID on the WC_Cart object,
Is there a good sensible way to derive one?
Note: I know it is possible to use generate_cart_id() to get a unique ID for the product line in the cart (as described in this question), however what I am looking for here is an identifier for the cart itself.
Your objective is rather interesting. I've read the code of WooCommerce but sadly it does not have what you need.
I have a suggestion though. This might help. In woocommerce_init
change WC()->cart
with your own class that extends to WC_Cart
. Then in your class have a way to set your cart id. Something like below.
add_action( 'woocommerce_init', 'woocommerce_init', 30 );
function woocommerce_init() {
class myWC_Cart extends WC_Cart {
private $cart_id = 1234; // set cart id by any means necessary
public function get_cart_id() {
return $this->cart_id;
}
}
WC()->cart = new myWC_Cart();
}
This way, anywhere WooCommerce is ready or loaded, you can get the cart id by calling
WC()->cart->get_cart_id();