0

I would like to edit the template of my Single product page.

I tried to use the WC hook woocommerce_single_product_summary in my child's functions.php to add custom field right below the title, but unfortunately the hooks behave strange, as described below.

I tried to add this line of code:

add_action('woocommerce_single_product_summary', 'display_speaker_name', 6);

function display_speaker_name() {
    echo '<p>' . get_field('speaker_name') . '</p>';
}

But what it does is, that it displays the custom field on the top of the summary section above the title. When i try to change the priority to 11, it displays at the bottom of the section, below the categories. I looked into the WC documentation and the priority of woocommerce_template_single_title hook should be 5. So if I understand that correctly, the proper priority for my custom hook should be between 6-9, if I want to display it below the title, right?

I even looked to the wc_template_hooks.php and the priority of the default WC hooks is set properly there. Also I tried to remove the original WC title hook and then add it with the priority 5, but unfortunately it did not work as well.

Do you have any ideas, how to solve this problem? I am using OceanWP theme. Thank you!

EDIT: See comments on the first answer for the solution.

microHoffman
  • 134
  • 1
  • 3
  • 12

1 Answers1

0

From /templates/content-single-product.php

<div class="summary entry-summary">
        <?php
        /**
         * Hook: woocommerce_single_product_summary.
         *
         * @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' );
        ?>
    </div>

So to go after the whole thing, you have to use a priority higher than 60.

add_action('woocommerce_single_product_summary', 'display_speaker_name', 70);

Howard E
  • 5,454
  • 3
  • 15
  • 24
  • Unfortunately I want to display speaker name **after the title**, not after the whole thing. And priority 6-9 of speaker name (which should be right after the title) does not work, it puts it on the top, above the title. And when I set a priority 11 of speaker name, it displays at the bottom of the whole section. Do you have any idea why is that? Thank you! – microHoffman Apr 16 '20 at 13:07
  • Does your theme have a template override? – Howard E Apr 16 '20 at 13:45
  • Thank you! That was it. If anyone else wondered - OceanWP uses their own hooks. For my case use this hook - https://docs.oceanwp.org/article/580-after-single-product-title . Thank you Howard! :) – microHoffman Apr 16 '20 at 14:39