WooCommerce Action and Filter Hooks: A Reference

The WooCommerce hooks you need most often

WooCommerce adds hundreds of its own actions and filters on top of WordPress. With them you can change the product page, the checkout fields, order processing and emails without copying template files into the theme. The hard part is finding the right one, so here are the hooks that come up in almost every shop.

Problem: You need to change part of the product page or the checkout and do not know which hook to use.

Solution: Use the summary hooks on the product page, woocommerce_checkout_fields for checkout fields, and the order status actions for processing. To find any other hook, search the WooCommerce templates for do_action and apply_filters.

Product page

The parts of the single product summary are added to woocommerce_single_product_summary with fixed priorities: title 5, rating 10, price 10, excerpt 20, add-to-cart 30, meta 40, sharing 50. Your callback goes between them by choosing the priority:

<?php
// Between the excerpt (20) and the add-to-cart button (30)
add_action( 'woocommerce_single_product_summary', 'ha_delivery_note', 25 );

function ha_delivery_note() {
    echo '<p class="delivery-note">' . esc_html__( 'Delivery in 2–3 days', 'textdomain' ) . '</p>';
}

// Remove a part: the same hook, function and priority
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );

// Right after the add-to-cart button
add_action( 'woocommerce_after_add_to_cart_button', 'ha_after_add_to_cart' );

Checkout fields

<?php
add_filter( 'woocommerce_checkout_fields', 'ha_vat_checkout_field' );

function ha_vat_checkout_field( $fields ) {
    $fields['billing']['billing_vat'] = array(
        'type'     => 'text',
        'label'    => __( 'VAT number', 'textdomain' ),
        'required' => false,
        'class'    => array( 'form-row-wide' ),
        'priority' => 120,
    );

    return $fields;
}

add_action( 'woocommerce_checkout_update_order_meta', 'ha_save_vat_field' );

function ha_save_vat_field( $order_id ) {
    if ( ! empty( $_POST['billing_vat'] ) ) {
        $order = wc_get_order( $order_id );
        $order->update_meta_data( '_billing_vat', sanitize_text_field( wp_unslash( $_POST['billing_vat'] ) ) );
        $order->save();
    }
}

Saving through the order object (WooCommerce 3.0+) instead of update_post_meta() keeps the code working if orders are ever stored somewhere else than in the posts table.

Order status

<?php
// An order became "processing" (paid)
add_action( 'woocommerce_order_status_processing', function ( $order_id ) {
    $order = wc_get_order( $order_id );
    // notify the warehouse, create a shipment...
} );

// Any status change, with the old and the new status
add_action( 'woocommerce_order_status_changed', function ( $order_id, $old_status, $new_status ) {
    error_log( "Order $order_id: $old_status -> $new_status" );
}, 10, 3 );

Finding other hooks

The templates in wp-content/plugins/woocommerce/templates/ list in their comments which functions are attached to each hook. To find all hooks, search the plugin:

grep -rn "do_action( 'woocommerce_before_cart" wp-content/plugins/woocommerce/

NOTE: Copy a template into the theme only when no hook can do the job. Copied templates do not get the changes of new WooCommerce versions, and the status page warns about outdated copies after every major update.