How to Customize the WooCommerce Cart Page on a WordPress Site | CSS-Tricks

How to Customize the WooCommerce Cart Page on a WordPress Site | CSS-Tricks

How to Customize the WooCommerce Cart Page on a WordPress Site
Author
Learn Development at Frontend Masters
A standard e-commerce site has a few common pages. There are product pages, shop pages that list products, and let’s not forget pages for the user account, checkout flow and cart.
WooCommerce makes it a trivial task to set these up on a WordPress site because it provides templates for them and create the pages for you right out of the box. This is what makes it easy to get your store up and running in a few minutes just by setting up some products and your payment processing details. WooCommerce is very helpful that way.
But this isn’t a post extolling the virtues of WooCommerce. Instead, let’s look at how we can customize parts of it. Specifically, I want to look at the cart. WooCommerce is super extensible in the sense that it provides a ton of filters and actions that can be hooked into, plus a way to override the templates in a WordPress theme. The problem is, those take at least some intermediate-level dev chops which may not be feasible for some folks. And, at least in my experience, the cart page tends to be the most difficult to grok and customize.
Let’s look at how to change the WooCommerce cart page by implementing a different layout. This is how a standard default cart page looks:
We’ll go for something like this instead:
Here’s what’s different:
We’re adopting a two-column layout instead of the single full-width layout of the default template. This allows us to bring the “Cart totals” element up top so it is more visible on larger screens.
We’re adding some reassurance for customers by including benefits below the list of products in the cart. This reminds the customer the value they’re getting with their purchase, like free shipping, easy exchanges, customer support and security.
We’re including a list of frequently asked questions beneath the list of products in an accordion format. This helps the customer get answers to questions about their purchase without have to leave and contact support.
This tutorial assumes that you have access to your theme files. If you don’t feel comfortable logging in to your hosting server and going to the file manager, I would suggest you install the plugin WP File Manager . With just the free version, you can accomplish everything explained here.
First, let’s roll our own template
One of the many benefits of WooCommerce is that it gives us pre-designed and coded templates to work with. The problem is that those template files are located in the plugin folder. And if the plugin updates in the future (which it most certainly will), any changes we make to the template will get lost. Since directly editing plugin files is a big ol’ no-no in WordPress, WooCommerce lets us modify the files by making copies of them that go in the theme folder .
It’s a good idea to use a child theme when making these sorts of changes, especially if you are using a third-party theme. That way, any changes made to the theme folder aren’t lost when theme updates are released.
To do this, we first have to locate the template we want to customize. That means going into the site’s root directory (or wherever you keep your site files if working locally, which is a great idea) and open up the /wp-content where WordPress is installed. There are several folders in there, one of which is /plugins. Open that one up and then hop over to the /woocommerce folder. That’s the main directory for all-things-WooCommerce. We want the cart.php file, which is located at:
/wp-content/plugins/woocommerce/templates/cart/cart.php
Let’s open up that file in a code editor. One of the first things you’ll notice is a series of comments on top of the file:
/** * Cart Page * * This template can be overridden by copying it to yourtheme/woocommerce/cart/cart.php. // highlight * * HOWEVER, on occasion WooCommerce will need to update template files and you * (the theme developer) will need to copy the new files to your theme to * maintain compatibility. We try to do this as little as possible, but it does * happen. When this occurs the version of the template file will be bumped and * the readme will list any important changes. * * @see https://docs.woocommerce.com/document/template-structure/ * @package WooCommerce/Templates * @version 3.8.0 */
The highlighted line is exactly what we’re looking for — instructions on how to override the file! How kind of the WooCommerce team to note that up front for us.
Let’s make a copy of that file and create the file path they suggest in the theme:
/wp-content/themes/[your-theme]/woocommerce/cart/cart.php
Drop the copied file there and we’re good to start working on it.
Next, let’s add our own markup
The first two things we can tackle are the benefits and frequently asked questions we identified earlier. We want to add those to the template.
Where does our markup go? Well, to make the layout look the way we laid it out at the beginning of this post, we can start below the cart’s closing table , like this:
We won’t cover the specific HTML that makes these elements. The important thing is knowing where that markup goes.
Once we’ve done that, we should end up with something like this:
Now we have all the elements we want on the page. All that’s left is to style things up so we have the two-column layout.
Alright, now we can override the CSS
We could’ve add more markup to the template to create two separate columns. But the existing markup is already organized nicely in a way that we can accomplish what we want with CSS… thanks to flexbox !
The first step involves making the .woocommerce  element a flex container. It’s the element that contains all our other elements, so it makes for a good parent. To make sure we’re only modifying it in the cart and not other pages (because other templates do indeed use this class), we should scope the styles to the cart page class, which WooCommerce also readily makes available.
.woocommerce-cart .woocommerce
These styles can go directly in your theme’s style.css file. That’s what WooCommerce suggests. Remember, though, that there are plenty of ways to customize styles in WordPress , some safer and more maintainable than others.
We have two child elements in the .woocommerce element, perfect for our two-column layout: .woocommerce-cart-form and .cart-collaterals. This is the CSS we need to split things up winds up looking something like this:
/* The table containing the list of products and our custom elements */ .woocommerce-cart .woocommerce-cart-form /* The element that contains the cart totals */ .woocommerce-cart .cart-collaterals /* Some minor tweak to make sure the cart totals fill the space */ .woocommerce-cart .cart-collaterals .cart_totals
That gives us a pretty clean layout:
It looks more like Amazon’s cart page and other popular e-commerce stores, which is not at all a bad thing.
Best practice: Make the most important elements stand out
One of the problems I have with WooCommerce’s default designs is that all the buttons are designed the same way. They’re all the same size and same background color.
Look at all that blue!
There is no visual hierarchy on the action users should take and, as such, it’s tough to distinguish, say, how to update the cart from proceeding to checkout. The next thing we ought to do is make that distinction clearer by changing the background colors of the buttons. For that, we write the following CSS:
/* The "Apply Coupon" button */ .button[name="apply_coupon"] /* Fill the "Apply Coupon" button background color and underline it on hover */ .button[name="apply_coupon"]:hover /* The "Update Cart" button */ .button[name="update_cart"] /* Brighten up the button on hover */ .button[name="update_cart"]:hover
This way, we create the following hierarchy: 
The “Proceed to checkout” is pretty much left as-is, with the default blue background color to make it stand out as it is the most important action in the cart.
The “Update cart” button gets a grey background that blends in with the white background of the page. This de-prioritizes it.
The “Apply coupon” is less a button and more of a text link, making it the least important action of the bunch.
The full CSS that you have to add to make this design is here:
@media(min-width: 1100px)   .woocommerce-cart .woocommerce-cart-form       .woocommerce-cart .cart-collaterals } .button[name="apply_coupon"] .button[name="apply_coupon"]:hover .button[name="update_cart"] .button[name="update_cart"]:hover
That’s a wrap!
Not too bad, right? It’s nice that WooCommerce makes itself so extensible, but without some general guidance, it might be tough to know just how much leeway you have to customize things. In this case, we saw how we can override the plugin’s cart template in a theme directory to future-proof it from future updates, and how we can override styles in our own stylesheet. We could have also looked at using WooCommerce hooks , the WooCommerce API , or even using WooCommerce conditions to streamline customizations, but perhaps those are good for another post at another time.
In the meantime, have fun customizing the e-commerce experience on your WordPress site and feel free to spend a little time in the WooCommerce docs — there are lots of goodies in there, including pre-made snippets for all sorts of things.
Our Learning Partner
Wanna learn about WordPress & JavaScript?
Frontend Masters has a course all about JavaScript for WordPress that gets into using JavaScript in themes, plugins, using Ajax, the REST API, and using other WordPress APIs.
Wanna learn about WordPress & JavaScript?
Frontend Masters has a course all about JavaScript for WordPress that gets into using JavaScript in themes, plugins, using Ajax, the REST API, and using other WordPress APIs.
Leave a Reply

Images Powered by Shutterstock