Search code examples
phpwordpresswoocommercehook-woocommerce

Set the right priority for a hooked function in WordPress / WooCommerce


How can I discover the right priority for a hooked function without continuous trial-errors?

I have to locate the "digital price" (precio digital) in my product page right below the normal price.

The php code is this:

add_action( 'woocommerce_single_product_summary', "show_digital_price", 10 );

As you can see now the digital price is above the normal price: https://www.editorialufv.es/catalogo/territory-inhabited/

I should change the number 10 and put, for instance, 11, or 12.

My question: is there a quick way to find out the right priority number without trial-errors method?


Solution

  • In content-single-product.php template file where woocommerce_single_product_summary hook is activated you will see that:

    <?php
        /**
         * woocommerce_single_product_summary hook.
         *
         * @hooked woocommerce_template_single_title - 5
         * @hooked woocommerce_template_single_rating - 10
         * @hooked woocommerce_template_single_price - 10
         * @hooked woocommerce_template_single_excerpt - 20
         * @hooked woocommerce_template_single_add_to_cart - 30
         * @hooked woocommerce_template_single_meta - 40
         * @hooked woocommerce_template_single_sharing - 50
         * @hooked WC_Structured_Data::generate_product_data() - 60
         */
        do_action( 'woocommerce_single_product_summary' );
    ?>
    

    That gives you the information about the priorities that are used by WooCommerce on this hook.

    So you can use for your show_digital_price() function a priority between 11 and 19

    Now sometimes other plugins or/and some themes can use some other priorities. In this case you can use this little code snippet:

    global $wp_filter;
    echo '<pre>';
    print_r( $wp_filter['woocommerce_single_product_summary'] );
    echo '</pre>';
    

    This will output all hooked functions in an array with the used priorities and accepted arguments for a defined hook (here woocommerce_single_product_summary action hook).

    Below an example of real output:

    WP_Hook Object (
        [callbacks] => Array (
            [5] => Array  (
                [woocommerce_template_single_title] => Array (
                    [function] => woocommerce_template_single_title
                    [accepted_args] => 1
                )
            )
            [10] => Array (
                [woocommerce_template_single_rating] => Array  (
                    [function] => woocommerce_template_single_rating
                    [accepted_args] => 1
                )
                [woocommerce_template_single_price] => Array (
                    [function] => woocommerce_template_single_price
                    [accepted_args] => 1
                )
            )
            [20] => Array (
                [woocommerce_template_single_excerpt] => Array (
                    [function] => woocommerce_template_single_excerpt
                    [accepted_args] => 1
                )
            )
            [30] => Array (
                [woocommerce_template_single_add_to_cart] => Array (
                    [function] => woocommerce_template_single_add_to_cart
                    [accepted_args] => 1
                )
            )
            [31] => Array (
                [WC_Subscriptions_Synchroniser::products_first_payment_date] => Array (
                    [function] => WC_Subscriptions_Synchroniser::products_first_payment_date
                    [accepted_args] => 1
                )
            )
            [40] => Array (
                [woocommerce_template_single_meta] => Array(
                    [function] => woocommerce_template_single_meta
                    [accepted_args] => 1
                )
            )
            [50] => Array (
                [woocommerce_template_single_sharing] => Array (
                    [function] => woocommerce_template_single_sharing
                    [accepted_args] => 1
                )
            )
            [60] => Array (
                [000000007484d339000000001aaf1b88generate_product_data] => Array (
                    [function] => Array (
                        [0] => WC_Structured_Data Object (
                            [_data:WC_Structured_Data:private] => Array ( )
                        )
                        [1] => generate_product_data
                    )
                    [accepted_args] => 1
                )
            )
        )
        [iterations:WP_Hook:private] => Array ( )
        [current_priority:WP_Hook:private] => Array ( )
        [nesting_level:WP_Hook:private] => 0
        [doing_action:WP_Hook:private] => 
    )
    

    As you can see in this example the Woocommerce Subscriptions plugin adds a hook at priority 31.