E-Commerce WordPress Solutions

How to create a Save and Restore Cart Plugin for WooCommerce

Letting shoppers save and restore their cart rescues sales that would otherwise vanish. Here is how to create a save and restore cart plugin for WooCommerce, so customers can pick up right where they left off.

Aparna Gawade Aparna Gawade 4 min read

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.

Saved Cart Options WooCommerce
Save/Restore Cart Options for WooCommerce

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’.

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.

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *