Fix Easy Digital Downloads Redirect to Checkout Empty Cart Varnish 4

Varnish is the server-side caching method of choice for numerous hosting companies. WPEngine, Cloudways and Flywheel, just to mention a few, use Varnish to deliver super fast page load times for their clients. One of the potential frustrations with Varnish can be making it work with e-commerce shops like WooCommerce or Easy Digital Downloads. Typically this involves carts being empty or checkout not working.

What usually happens is users end up skipping a lot more Varnish caching than is possible just to prevent the errors they are experiencing. After all it makes little sense to have a fast e-commerce store if you cannot sell anything reliably. This popular method of redirecting to checkout creates some issues with Easy Digital Downloads which we hopefully will solve now. I will be posting a full Varnish vcl for Easy Digital Downloads (EDD) in the future.

You have probably already set Varnish to pass the EDD cart and checkout pages along with the edd-cookies but the cart is still empty. By looking at varnishlog I discovered the problem was a 303 response which, according to Wikipedia, is a temporary redirect used after POST requests.

- BerespProtocol HTTP/1.1
- BerespStatus 303
- BerespReason See Other

Since there was no exception for this status the edd-cookie was being removed! This guide will solve your problem of Easy Digital Downloads showing an empty cart when redirect to checkout is enabled through Varnish cache.

Fix Easy Digital Downloads Redirect to Checkout Empty Cart Varnish 4

Check if you have your purchases set to Redirect to Checkout

Go to Downloads > Settings > Misc > Misc Settings

easy-digital-downloads-misc-settings-cart

If you are adjusting this setting for the first time click Save Changes

Open your Varnish vcl

sudo nano /etc/varnish/default.vcl

In your sub vcl_backend_response section you likely have a subsection where certain URLs are whitelisted from having cookies removed. Because EDD uses a 303 response for redirecting to checkout, this has to be added to the list of exclusions. Add !beresp.status == 303 into your if statement which removes cookies , you will need to prefix it with && meaning and.

This will prevent the cookie removal for this special redirect Easy Digital Downloads uses.

sub vcl_backend_response {
    #beresp.status exception necessary when redirecting purchase to checkout    
    if (!(bereq.url ~ "wp-(login|admin)|cart|checkout") &&
        !bereq.http.cookie ~ "wordpress_logged_in|resetpass|edd_saved_cart" &&
        !beresp.status == 303  ) {
        unset beresp.http.set-cookie;
        set beresp.ttl = 2w;
        set beresp.grace = 3d;
    }
} #end backend_response

Ctrl+X, Y and Enter to Save and Exit.

Test the Varnish syntax is correct

varnishd -C -f /etc/varnish/default.vcl

If you do not get any errors then reload Varnish

sudo service varnish reload

Start up a fresh browser or clear all of your cookies and test the purchase redirect to checkout functionality with Easy Digital Downloads.

Sources

Preventing Empty Cart with EDD

3 thoughts on “Fix Easy Digital Downloads Redirect to Checkout Empty Cart Varnish 4”

Comments are closed.