Lesson 2.2. Templates vs Hooks

Wednesday, 2:20 PM

After a WooCommerce update the Add to cart button disappeared on variable products. Nobody touched the theme.

But the theme holds 40 template overrides copied years ago. One of them is single-product/add-to-cart/variable.php. Its only difference from the original is one icon.

Every template copy is a debt that comes due with every update.

Goal

After the lesson you choose between a hook, a block and a template copy, and explain to the client what each option will cost at the next update.

Key concepts

  • Override: a copy from woocommerce/templates/ in your-theme/woocommerce/
  • Every template has @version — WooCommerce Status shows outdated copies
  • An outdated copy = missed security fixes and new hooks
  • In a block theme the store pages are block HTML templates (single-product.html, archive-product.html)
  • Order of choice: settings → hook → block/pattern → template override as the last resort

Diagram

flowchart TD
  Q["Need to change the look"] --> T{"Block theme?"}
  T -- yes --> B["Site Editor / templates/*.html<br/>blocks, patterns"]
  T -- no --> H{"Is there a hook or filter?"}
  H -- yes --> HK["Hook in a plugin or functions.php"]
  H -- no --> O["Override the template<br/>and watch @version"]

Code

A template header with its version (check it after every WooCommerce update):

PHP
<?php
/**
 * Single Product Price
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/price.php.
 *
 * @package WooCommerce\Templates
 * @version 3.0.0
 */

A block product template in the theme (templates/single-product.html, excerpt):

HTML
<!-- wp:woocommerce/product-image-gallery /-->
<!-- wp:post-title {"level":1,"__woocommerceNamespace":"woocommerce/product-query/product-title"} /-->
<!-- wp:woocommerce/product-price {"isDescendentOfSingleProductTemplate":true} /-->
<!-- wp:woocommerce/add-to-cart-form /-->

Common mistakes

SymptomCauseFix
The product page broke after a WooCommerce updateAn outdated template overrideWooCommerce → Status → Templates: update the copies or replace them with hooks
A theme override has no effectThe theme is a block theme, the PHP template isn't usedEdit the block template
The theme holds 40 overridden templatesEvery small tweak became a copyKeep only those without a hook

Weak and strong answer

Question: Why are template overrides a risk?

Weak answer. Because you have to update them.

Strong answer. A copy "freezes" the template at the version it was copied from. New hooks, security and accessibility fixes never reach it. So I override only what can't be changed with a hook, keep the diff with the original minimal and check WooCommerce → Status → Templates after every update.

Why the second is stronger: The candidate names concrete consequences and a control process.

Practice

Two separate cases: first find the copies, then get rid of one.

Case 1. Audit the overrides (terminal)

Find which templates the theme overrides, which of them are outdated, and what exactly the price.php copy changed. On your own site the same list is in WooCommerce → Status → Templates.

wp — Module 2. Hooks and Extensions

Case 2. Replace the copy with a filter (IDE)

The diff showed that the price.php copy changes a single thing: the big class. Delete the copy and get the same result with a filter.

TEXT

Interview Questions

WooCommerce → Status → System status, the Templates section. It shows the copy's version and the core version.

Not for the parts rendered by blocks: a block has its own render. Some blocks (for example, the classic Legacy Template) still use PHP templates, but the main path is blocks.

It depends on the project. A block theme gives code-free editing of store templates and the modern checkout. A classic one fits when many extensions depend on PHP templates and checkout hooks. Back the decision with a plugin audit.

Save your progress

Sign in or create a free account to mark lessons as completed and continue the whole course. You will come back to this lesson.