Search

How to create a Save and Restore Cart Plugin for WooCommerce

Listen to this article

WooCommerce Save Cart OptionIn 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

[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]

Aparna Gawade

Aparna Gawade

14 Responses

    1. Hi Bijith,

      Thank you for your comment. Can you provide some details on the problems you are facing?

  1. 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 ?

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

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

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

  2. cart is not restoring properly, showing error “Warning: Invalid argument supplied for foreach() in…” .
    I fetched the array it cart saved but not restoring.. :/

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads