Skip to content
+91 89607 57615
Code Craft — web development and digital marketing
Lesson 926 min

Hooks: actions and filters

Change WooCommerce without editing it: add a notice with an action and rename a button with a filter.

Taught by Sandesh Sahetiya

The video is part of the full course

In this lesson you will

  • Explain the difference between actions and filters
  • Use priority and accepted arguments
  • Customise WooCommerce product pages
  • Know how hook output behaves in block themes

Hooks are how you change WordPress and plugins without editing their files. An action runs your code at a point (print something, save something) and returns nothing. A filter receives a value, changes it, and must return it.

wp-content/plugins/sunrise-shop-tweaks/sunrise-shop-tweaks.phpDownload
<?php
/**
 * Plugin Name:       Sunrise Shop Tweaks
 * Description:       Small WooCommerce changes for Sunrise Bakery, using one action and one filter.
 * Version:           1.0.0
 * Requires at least: 6.7
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Author:            Sunrise Bakery
 * License:           GPL-2.0-or-later
 * Text Domain:       sunrise-shop-tweaks
 *
 * @package SunriseShopTweaks
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Is this product in the "Cakes" category?
 *
 * @param WC_Product $product The product.
 * @return bool
 */
function sunrise_is_cake( $product ) {
	return $product && has_term( 'cakes', 'product_cat', $product->get_id() );
}

/**
 * ACTION: print a notice on cake pages.
 * Actions do something at a point in the page; they don't return anything.
 * In a classic theme this runs between the short description (priority 20)
 * and the add-to-cart button (30). In a block theme such as Twenty Twenty-Five,
 * it appears just below the price.
 */
add_action(
	'woocommerce_single_product_summary',
	function () {
		global $product;
		if ( sunrise_is_cake( $product ) ) {
			echo '<p class="sunrise-notice">' . esc_html__( 'Celebration cakes need two days\' notice. Add the date you need it in the order note at checkout.', 'sunrise-shop-tweaks' ) . '</p>';
		}
	},
	25
);

/**
 * FILTER: change the button text on cake pages.
 * Filters receive a value, change it, and must return it.
 */
add_filter(
	'woocommerce_product_single_add_to_cart_text',
	function ( $text, $product ) {
		if ( sunrise_is_cake( $product ) ) {
			return __( 'Order this cake', 'sunrise-shop-tweaks' );
		}
		return $text;
	},
	10,
	2 // This callback receives two values: the text and the product.
);

The action

woocommerce_single_product_summary runs on product pages. Our callback checks whether the product is in the Cakes category and, if so, prints a notice. The number 25 is the priority: lower runs earlier, and the default is 10.

Continue with the full course

The rest of this lesson, the narrated video, the PDF and the exercise are part of WordPress and WooCommerce. One payment, lifetime access.

Already bought it? Sign in with the email you paid with.