WooCommerce hooks let you change or add features without changing the main code. This keeps your store safe and easy to update.
0
0
WooCommerce hooks for customization in Wordpress
Introduction
Add a custom message on the product page.
Change the order confirmation email content.
Add extra fields to the checkout page.
Modify the price display on product listings.
Run code after a customer completes an order.
Syntax
Wordpress
add_action('hook_name', 'your_function_name', priority, accepted_args); function your_function_name() { // Your custom code here } // Or for filters: add_filter('hook_name', 'your_function_name', priority, accepted_args); function your_function_name($content) { // Modify and return $content return $content; }
add_action is used to add new behavior at a specific point.
add_filter is used to change data before it shows up.
Examples
This adds a message before the product details on the product page.
Wordpress
add_action('woocommerce_before_single_product', 'show_custom_message'); function show_custom_message() { echo '<p>Free shipping on orders over $50!</p>'; }
This changes how the price is shown by adding extra text.
Wordpress
add_filter('woocommerce_get_price_html', 'change_price_display'); function change_price_display($price) { return $price . ' <small>including tax</small>'; }
This saves a custom field value when the customer finishes checkout.
Wordpress
add_action('woocommerce_checkout_update_order_meta', 'save_custom_checkout_field'); function save_custom_checkout_field($order_id) { if (!empty($_POST['custom_field'])) { update_post_meta($order_id, '_custom_field', sanitize_text_field($_POST['custom_field'])); } }
Sample Program
This code adds a note below the product title on the product page. It uses the woocommerce_single_product_summary hook with priority 20 to place the message in the right spot.
Wordpress
<?php // Add a custom note on the product page add_action('woocommerce_single_product_summary', 'add_custom_note', 20); function add_custom_note() { echo '<p><strong>Note:</strong> This product ships within 24 hours.</p>'; } ?>
OutputSuccess
Important Notes
Hooks let you add or change features without touching WooCommerce core files.
Always use child themes or custom plugins to add hooks so updates don't erase your changes.
Use the right hook name and priority to control where your code runs.
Summary
WooCommerce hooks let you customize your store safely and easily.
Use add_action to add new things and add_filter to change existing data.
Hooks help keep your custom code organized and update-proof.