WooCommerce

Custom WooCommerce Hooks Without Another Plugin

Custom WooCommerce hooks let me change store behaviour with a little PHP instead of adding a plugin. WooCommerce fires actions at moments you can target and exposes values through filters, so you can move a notice, relabel a price, or swap a template file without touching core. The trade-off is discipline: match the callback and priority when removing a hook, keep overrides in a child theme, and test on staging.

By Saif Al Jahanwebdev-saif.com

What custom WooCommerce hooks actually solve

WooCommerce is built on the WordPress plugin API, so almost every visible piece of a store is emitted through do_action() or passed through apply_filters(). A custom hook is simply your callback attached with add_action() or add_filter() at the right point. I use them for small, single-purpose changes: adding a trust badge after the add-to-cart button, changing a checkout field label, or inserting structured data into the product footer. Each change stays in one place, ships with the theme or a site-specific plugin, and carries no settings screen, no auto-update surface and no extra admin weight. One intent per callback keeps the change reviewable.

  • add_action() for side effects: output, redirects, emails, cart changes
  • add_filter() for values: labels, prices, query args, template paths
  • One callback per change, named for what it does
  • Code lives in the theme or a site plugin, never in WooCommerce core

Choose actions and filters by what you need to change

Start by finding the hook in the template or class that emits the value you want. woocommerce_single_product_summary is an action bundle with priorities; woocommerce_after_shop_loop_item_title is another common insertion point. When you need to change a value rather than add one, reach for a filter such as woocommerce_get_price_html, woocommerce_cart_item_name or woocommerce_checkout_fields. Filters that modify arrays are the easiest to get wrong: return the value in every branch, including early returns, or you will blank a price or drop a field. I keep filters pure where possible and leave database writes to actions like woocommerce_checkout_create_order.

  • woocommerce_single_product_summary for product page placement
  • woocommerce_get_price_html to change the displayed price markup
  • woocommerce_checkout_fields to alter checkout field definitions
  • Return the filtered value on every code path

Remove a callback without breaking the store

remove_action() and remove_filter() only work when all three arguments match the original registration: hook name, callback, and priority. That is why a removal placed in functions.php often fails when the callback was registered by a plugin class or a closure. Remove class methods through the object instance, and never try to remove an anonymous function you do not hold. Timing matters too: run removals after the code that added them, usually on init at a later priority or inside wp_loaded. If you own the original call, the cleanest fix is often to change it rather than unhook and rehook it.

  • Match hook name, callback reference and priority exactly
  • Class callbacks require the same object instance
  • Run removals after the original registration, not before
  • Confirm with has_action() or has_filter() before and after

Template overrides through woocommerce_ filters

WooCommerce templates live in plugins/woocommerce/templates and are resolved through wc_get_template(). Copy the file you need to yourtheme/woocommerce/ and WooCommerce loads your version instead, with no core edit and no extra plugin. For cases where the file must come from somewhere else, the woocommerce_locate_template filter lets you return your own path for a given template name and template path. Keep the override minimal: change the markup you need, keep the surrounding actions such as woocommerce_before_single_product in place, and add a comment at the top stating the WooCommerce version you copied from. After an update, diff your override against the new core template.

  • Override path: yourtheme/woocommerce/ with the same relative structure
  • woocommerce_locate_template returns a custom template path
  • Preserve the original action hooks inside the copied template
  • Record the WooCommerce version at the time of copying

Priority discipline is the actual skill

Priorities decide order, and order decides correctness. WooCommerce uses them heavily: product summary pieces run at 10, 20, 30 and beyond, and checkout blocks follow the same pattern. When my output appears in the wrong place, I rarely add a new hook; I change the priority. I keep a short table in the project notes of every hook I attach to, its priority and why. When debugging, has_action() tells me whether a callback is registered and at what priority, while current_filter() confirms which hook is firing. On staging I use Query Monitor to inspect hook order before touching production code.

  • Default priority is 10; move later with 20 or 30
  • Use has_action() and has_filter() to inspect registered callbacks
  • Log current_filter() when the firing order is unclear
  • Document every hook, priority and reason in project notes

When a plugin is the better call

Hooks are not a religion. I keep custom code in PHP when the change is deterministic and belongs to this store: a label, a layout tweak, a field rule, a template override. I reach for a plugin when the feature needs a settings UI, must survive a theme change, or has to be maintained by someone who will not read PHP. For Saif Al Jahan builds at webdev-saif.com, the split is deliberate: small store-specific behaviour goes into a mu-plugin or child theme, while anything sold or reused becomes a properly versioned plugin. The test is simple. If the client needs to toggle it, it needs an interface.

My release check for hook changes

Hook changes are cheap to write and easy to ship half-tested. Before a release I clear WooCommerce transients, flush the cache, and walk the affected flow in a private window: product page, cart, checkout, order confirmation and the admin order screen. I check the theme error log for warnings from a filter that failed to return a value, then confirm the change still holds with a coupon, a variable product and a logged-out session. Only then do I push to production. A five-minute pass here prevents the classic failure: a price filter that blanks the shop on a Monday morning.

  • Clear WooCommerce transients and the object cache
  • Test product, cart, checkout and admin order views
  • Check logs for filter warnings and PHP notices
  • Verify logged-out, coupon and variable-product cases

Frequently asked questions

Do custom WooCommerce hooks replace plugins?

For small, store-specific changes, yes. A single add_action() or add_filter() can replace a plugin that exists only to move a button or relabel a field. Hooks are less useful when the feature needs settings, an admin screen, or updates across many stores. In my builds I keep one-off behaviour in a mu-plugin or child theme and use a real plugin only when the client must manage it themselves.

What is the difference between an action and a filter?

An action is a signal that WooCommerce fires at a specific moment: it carries data you can read but cannot change, and it is for side effects such as output or an email. A filter passes a value through your callback and expects it returned, modified or unchanged. Choose the hook by whether you need to do something or change something.

Why does remove_action() not work in my theme?

Almost always because the arguments do not match. You need the exact hook name, the exact callback reference and the same priority used when it was added. Class callbacks require the same object instance, and closures cannot be removed without keeping the variable. Also check timing: your removal must run after the original registration, commonly on init at a later priority.

How do I override a WooCommerce template safely?

Copy the template from plugins/woocommerce/templates into yourtheme/woocommerce/ with the same relative path and edit only what you need. Keep the core action hooks inside the file and note the WooCommerce version you copied from. After every WooCommerce update, diff your override against the new original so you do not carry stale markup or miss a security fix.

What priority should I use for my callback?

Start at the default 10. If your output lands above something it should follow, move to 20 or 30; if it lands below, go earlier. Look at how WooCommerce itself orders the same hook rather than guessing. Keep a note of the priority you chose and why, and re-check it after a WooCommerce update or theme change.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .