A simple, powerful and independent e-commerce platform.
Sell anything with ease.

How to override template files from a plugin

How to override template files from a plugin

Classic Commerce has a directory containing all of the templates that are used by your site. These files hold the markup and template structures for the front end pages and emails generated by your store. It is possible to override template files and instead use customised versions that will be more suitable for your particular application.

The single-product.php template, for example, is used to create product pages, while cart.php builds out the shopping cart page. Editing these template files will give you much more control over every aspect of your store, including the shopping cart and checkout process. This allows you to create something unique without having to rely on extensions to do the job for you.

You obviously don’t want to edit the core files because your changes will be lost when you update Classic Commerce. The widely recommended procedure is to create a folder in your theme (ideally your child theme) called classic-commerce and put the changed template files in there. Classic Commerce knows to check here first before going on to use the default versions.

But this now ties your edited files to the current theme. What happens if you or your client decide to change to a new theme? You (or someone else) will need to know about it and remember to copy that folder across.

An alternate place for your override template files

A more robust solution is to put the override templates into a plugin, and the obvious place is the special snippets plugin that was discussed in a previous tutorial: How to add code snippets to Classic Commerce

If you haven’t done so already, download the sample snippets plugin here:  cc-snippets.zip

This needs to be installed and activated in the usual way. If you look into the plugin structure you will see there is already a templates folder ready to take your template overrides. Place a copy of any of the files you want to customise in here, keeping to the same directory structure as the original. So, if you want to edit archive-product.php then put it into the main templates directory. But if you are editing cart.php then put that in a sub-directory called cart.

The final step is to tell Classic Commerce to check here for any possible template overrides, before using the default version. We do this with a code snippet that goes into the cc-snippets.php file.

[snippet id=980]


Test that the override template is working

You will now be able to make edits to your copy of the file and see them appear on the website or in emails. For a quick test, copy in archive-product.php, then open it in your favourite text editor. Around line 28, where you see this line – <header class="woocommerce-products-header"> – make a new line above and type in <p>Archive product</p>. Save the file, visit the site and check out any category page.

You should see the text has now been added to the page just above the category heading. Now you can get busy and start making your changes.

Note: There is a very slight possibility that an update to Classic Commerce might change one or more of the default templates. In this case, you will need to copy over the new template and restore your customisations. But Classic Commerce is dedicated to providing a long-term, stable and reliable e-commerce solution so it’s not something that will be very likely to occur.

Photo by Jamie Street on Unsplash