I have read about hooks and actions and I know that when we see do_action('some_hook') it means that somewhere add_action('some_hook', function() {...}); or function some_hook() exists in a theme's functions.php.

But what is it?! :

From woocommerce/includes/class-wc-checkout.php line #943:

public function process_checkout() {
    try {
        /////////////
        do_action( 'woocommerce_before_checkout_process' ); <----!!!

        if ( WC()->cart->is_empty() ) {
            throw new Exception( sprintf( __( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to shop</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'shop' ) ) ) );
        }

        do_action( 'woocommerce_checkout_process' ); <-----!!!

I can't find woocommerce_checkout_process and woocommerce_before_checkout_process anywhere in files! What do they do?

up vote 12 down vote accepted

It is possible for action hooks to be provided without being used, which is what's happening here.

woocommerce_before_checkout_process and woocommerce_checkout_process are hooks provided by WooCommerce, but WooCommerce does not itself attach callback functions to either of these hooks. They are provided to allow plugins and themes to run code at the time that the respective hooks are triggered.

If you'd like to attach a function to the woocommerce_checkout_process hook (for example), you'd add the following code to your theme or plugin:

add_action( 'woocommerce_checkout_process', 'wpse_woocommerce_checkout_process' );
function wpse_woocommerce_checkout_process() {
    // Do something...
}

This code will run when the line calling do_action( 'woocommerce_checkout_process' ); in WooCommerce is executed.

  • I added an example of how to attach a callback function. You don't call do_action in your code (unless you are defining your own hook which is not the case here). You'd use add_action(). – Dave Romsey Feb 26 at 22:43
  • 1
    Got it! Thank you very much! – Zhi V Feb 26 at 22:44

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.