WooCommerce Snippet Generator
Describe a change and get an HPOS-safe PHP snippet, with where to paste it and how to undo it.
How the snippets are written
The generator works from a fixed list of WooCommerce hooks checked against the official code reference, and refuses to invent new ones. Every snippet follows the same rules:
- HPOS-safe. Orders are read and written through
wc_get_order()and the order object (get_meta,update_meta_data,save), neverget_post_metaor direct database queries. - Checkout-aware. Each answer says whether it works with the classic checkout, the Checkout block, or both.
- Prefixed functions so they don't clash with your theme or plugins.
- A rollback plan for when something goes wrong.
Classic checkout vs the Checkout block
Many older snippets use hooks that only run in the classic (shortcode) checkout. The Checkout block, now the default on new stores, uses the Store API instead. Some common examples:
| Hook | Classic | Block | Block alternative |
|---|---|---|---|
woocommerce_cart_calculate_fees | Yes | Yes | None needed |
woocommerce_package_rates | Yes | Yes | None needed |
woocommerce_checkout_fields | Yes | Partly | Additional Checkout Fields API to add fields |
woocommerce_order_button_text | Yes | No | Edit the button text in the editor |
woocommerce_checkout_order_processed | Yes | No | woocommerce_store_api_checkout_order_processed |
Not sure which you have? Open the checkout page in the editor. If it shows the [woocommerce_checkout] shortcode or a Classic Checkout block, you're on classic.
Where to paste it and how to roll back
- 01
Code Snippets plugin (recommended)
WooCommerce's own docs recommend it. Install Code Snippets, go to Snippets → Add New, paste, and save and activate. It can deactivate snippets that cause fatal errors.
- 02
Child theme functions.php
Only in a child theme, since theme updates overwrite the parent's file. Back it up and edit over SFTP, not the built-in editor.
- 03
Roll back
Deactivate the snippet, or remove the code from functions.php. If the site is down, add
&snippets-safe-mode=1to an admin URL, or use the recovery mode link WordPress emails to the admin.
Test every snippet on a staging site or with a test order first. The generator follows documented hooks but can't see your theme or plugins, so nothing is guaranteed.
Questions people ask
What is HPOS and why does it matter for snippets?
High-Performance Order Storage keeps orders in their own tables instead of WordPress posts. Snippets that use get_post_meta or WP_Query on orders can read stale data or write to the wrong place, so these snippets use the order object instead.
Why doesn't my checkout snippet work anymore?
You're probably on the Checkout block, which ignores classic checkout hooks like woocommerce_checkout_fields and woocommerce_order_button_text. Pick Block checkout in the tool to get a supported approach.
Should I use functions.php or a plugin?
A snippets plugin is safer: code survives theme changes, and you can switch a snippet off without editing files. Use functions.php only in a child theme.
Is the code tested?
It follows WooCommerce's documented hooks, but it can't be tested against your store. Try it on staging first and keep the rollback steps handy.
My site shows a critical error after adding a snippet. What now?
With Code Snippets, add &snippets-safe-mode=1 to an admin URL and deactivate it. With functions.php, remove the code over SFTP, or use the recovery mode link WordPress emails to the site admin.
Related tools
Last reviewed 30 September 2026.
Sources: WooCommerce Code Reference: action and filter hooks · WooCommerce developer docs: HPOS extension recipe book · WooCommerce developer docs: Additional checkout fields · WooCommerce docs: Hook alternatives for Cart and Checkout blocks · WooCommerce developer docs: Using the Code Snippets plugin · Code Snippets: Safe mode · Make WordPress Core: Fatal error recovery mode
