Lesson 2.1. WooCommerce Hooks

Tuesday, 11:00 AM

The designer: "SKU and categories right under the product title."

You try three things:

  1. remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta' ) — nothing.
  2. Move it to init — nothing.
  3. Copy the template into the theme — it works, and now the copy is yours forever.

All it needed was one number. WooCommerce adds the meta at priority 40, and remove_action without a priority looks for 10.

Goal

After the lesson you find a hook and its priority in a minute and rearrange the product page without copying templates.

Key concepts

  • Action — "do something at this moment", filter — "change a value and return it"
  • WooCommerce templates are built from hooks with priorities: woocommerce_single_product_summary
  • remove_action() works only with the same callback and the same priority
  • Dynamic hooks: woocommerce_order_status_{status}, woocommerce_product_get_{prop}
  • Where to look: template code, developer.woocommerce.com, Query Monitor → Hooks

Diagram

The product summary is one hook. Each block is a callback, and the priority sets the order:

flowchart TB
  S(["woocommerce_single_product_summary"])
  S --> T["5 · title"]
  T --> R["10 · rating"]
  R --> P["10 · price"]
  P --> E["20 · short description"]
  E --> A["30 · Add to cart"]
  A --> M["40 · meta: SKU, categories"]
  M --> SH["50 · sharing"]

Code

Move the price below the short description and add a badge:

PHP
<?php
add_action( 'init', static function () {
	remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
	add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 25 );
} );

add_action( 'woocommerce_single_product_summary', static function () {
	global $product;
	if ( $product instanceof WC_Product && $product->is_on_sale() ) {
		echo '<p class="badge-sale">' . esc_html__( 'Sale ends Sunday', 'my-plugin' ) . '</p>';
	}
}, 26 );

Filter: change the button text for one category.

PHP
<?php
add_filter( 'woocommerce_product_single_add_to_cart_text', static function ( string $text, WC_Product $product ): string {
	return has_term( 'preorder', 'product_cat', $product->get_id() ) ? __( 'Pre-order', 'my-plugin' ) : $text;
}, 10, 2 );

Common mistakes

SymptomCauseFix
remove_action does nothingA different priority, or it runs before the hook is addedCheck the priority in WooCommerce code; remove on init or wp
A filter "breaks" the pageThe callback returns nothingA filter always returns a value
No visible change in a block themeThe product page is rendered by blocks, not a PHP templateSee lesson 2.2 and module 5

Weak and strong answer

Question: How do you move the price on the product page?

Weak answer. Copy single-product/content-single-product.php into the theme and move the code.

Strong answer. In a classic theme — remove_action with priority 10 and add_action with a new priority. No template copy. In a block theme the price is a block in the Single Product template, so I move it in the Site Editor or in the theme's templates/single-product.html.

Why the second is stronger: The hook survives updates, and the candidate tells a classic theme from a block theme.

Practice

Move the meta block (SKU, categories) right under the product title, then change the button text for the preorder category. Two tasks in the IDE below, verified with Run.

On your own site: open a product page with Query Monitor, go to the Hooks panel and find every woocommerce_single_product_summary callback with its priority.

TEXT

Interview Questions

An action does something at a given moment and returns nothing. A filter receives a value, changes it and must return it. Technically both run on the same WP_Hook system.

Look in includes/wc-template-hooks.php, in Query Monitor → Hooks, or dump $wp_filter['hook_name'].

A hook whose name contains a variable part. For example, woocommerce_order_status_completed or woocommerce_product_get_price — WooCommerce builds them from a status or property name.

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.