In WooCommerce, cart details are saved, only when a user proceeds to checkout and clicks the ‘Place Order’ button. These details are available under each user account, as ‘Recent Orders’. In case an order is marked as ‘Complete’, a user can reorder the same items. This functionality, to reorder a previously made order, is inherently present in WooCommerce.
But say we wanted to provide a user a similar functionality, but where a user could save a filled cart, to be sent for order at a later time. For example, if a user wanted to save cart contents, he could have an option to do so before checkout. He could then restore the saved cart and then proceed to checkout, whenever appropriate. Such a usecase would have the following options provided:
-
Save Cart Option: A user has the option to ‘Save Cart’, before checkout.
-
View Saved Carts: The cart details will be saved under the user account, as a list, similar to Recent Orders.
-
Restore Cart: The user then has the option to restore the saved cart, to make the purchase.
[space]
How to Save Cart Details
To provide the user an option to save the current cart, there needs to be a button, for example, ‘Save Cart’, alongside ‘Proceed to Checkout’. When a user clicks this button, the current cart details have to be saved, for the user. These details can be saved as meta values for the user. Here, we will have to add a function on the ‘Save Cart’ button click, as follows:
function wdm_save_cart_details() { global $woocommerce; // get user details global $current_user; get_currentuserinfo(); if (is_user_logged_in()) { $user_id = $current_user->ID; $cart_contents = $woocommerce->cart->get_cart(); $meta_key = 'cart-'.date('l dS F'); $meta_value = $cart_contents; update_user_meta( $user_id, $meta_key, $meta_value); } }
This function ‘wdm_save_cart_details’ will save the cart details as meta value for the logged in user. Additionally, you can clear the cart after a user chooses the save cart option.
$woocommerce->cart->empty_cart();
Hint: You can use the same function on any suitable hook, to save cart details, when needed. For example, cart details are saved only when user clicks, ‘Place Order’. You can instead save cart details, every time a user clicks ‘Proceed to Checkout’.
[space]
Restore Previous Cart Content
The next step is to provide a user the possibility to order a previously saved cart selection. For this, a list of saved carts should be displayed under every user account. The user can then view a saved cart, and click the ‘Restore Cart’ option to refill his shopping cart.
Here, we will have to read the saved meta data and fill the shopping cart. You would have to provide a function, similar to the one below:
function wdm_restore_cart_details($wdm_selected_cart_key) { $cart_content=get_user_meta($user_id,$wdm_selected_cart_key,true); // clear current cart, incase you want to replace cart contents, else skip this step $woocommerce->cart->empty_cart(); // add cart contents foreach ( $cart_content as $cart_item_key => $values ) { $id =$values['product_id']; $quant=$values['quantity']; $woocommerce->cart->add_to_cart( $id, $quant); } }
Remember, to redirect the person the checkout page once the cart has been filled. And there you have it. You can very easily put this code in your own custom plugin, to create a ‘Save and Restore Cart’ extension for the WooCommerce plugin.
[space]
Interested in Creating a Similar Extension to WooCommerce Plugin?
WisdmLabs is a Certified WooCommerce Expert. We can custom build an extension plugin for you.
14 Responses
I am looking to implement this in my site but.. not possible.. i am new in wordpress..
Hi Bijith,
Thank you for your comment. Can you provide some details on the problems you are facing?
Great post 🙂
Thank you Ankit :).
Hi,
This is a nice idea. But where do i have to put this lines of code to implement this on my site. Should i create the “save” and “restore” buttons ?
Hi,
To implement this on your site, you will have to put the code in a custom plugin. You will have to create the “save” and “restore” buttons as well. The “save” button should be shown on the cart page. And the details should be saved under a user’s account details. When a user views a saved cart, there should be an option to restore, using the “restore” button.
Hello, I am looking to do this in my site but i am new in wordpress can u plz tell what exactly should I do!
Thanks
How does the user (2) View saved carts?
Hi Glen,
The saved cart (and details) are saved as part of user meta. So, on every ‘My Account’ page, using a hook we have to display a list of saved carts. When a user clicks on a saved cart, the details have to be shown. This information will be read as displayed as per the user meta. There will be an option (button) to ‘Restore Cart’. Once the user clicks the button, the restore cart function will have to be called.
cart is not restoring properly, showing error “Warning: Invalid argument supplied for foreach() in…” .
I fetched the array it cart saved but not restoring.. :/
That’s strange Aditya. Let me have a look and get back to you.
Is this available for WooCommerce 2.2 and greater
Hey, we had built this solution for a client, but we aren’t selling it as a plugin.
It is not saving product details in ‘wp_woocommerce_sessions’ and not even user id