Add my own function to existing WooCommerce hook

I’m trying to customise my WooCommerce child theme with parent theme Storefront. The parent theme creates single product template (content-single.php) like this:

/**
 * @hooked storefront_post_header - 10
 * @hooked storefront_post_meta - 20
 * @hooked storefront_post_content - 30
 */
do_action( 'storefront_single_post' );

and these functions are hooked to build out the page (/inc/structure/hooks.php):

add_action( 'storefront_single_post', 'storefront_post_header', 10 );
add_action( 'storefront_single_post', 'storefront_post_meta', 20 );
add_action( 'storefront_single_post', 'storefront_post_content', 30 );

For reference, this is the storefront_post_header function (inc/structure/post.php):

if ( ! function_exists( 'storefront_post_header' ) ) {
/**
   * Display the post header with a link to the single post
   * @since 1.0.0
 */
function storefront_post_header() { ?>
    <header class="entry-header">
    <?php

    if ( is_single() ) {
        storefront_posted_on();
        the_title( '<h1 class="entry-title" itemprop="name headline">', '</h1>' );

    } else {
        if ( 'post' == get_post_type() ) {
            storefront_posted_on();
        }

        the_title( sprintf( '<h1 class="entry-title" itemprop="name headline"><a href="https://wordpress.stackexchange.com/questions/207268/%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' );
    }
    ?>
    </header><!-- .entry-header -->
    <?php
}

I want to output my function, storefront_post_header_categories (below), after storefront_post_header. I thought I would be able to add the following to my child theme functions.php to do that :

add_action( 'storefront_single_post', 'storefront_post_header_categories', 15 );
function storefront_post_header_categories() {
  echo "code to display categories here";
}

This doesn’t work, or output anything to the front-end. Nor does is give a WP_DEBUG error.

What am I doing wrong? Thanks for your help and let me know if you need any more info.

1 Answer
1

storefront_single_post hook pertains only to single posts and not products although products are considered posts of type ‘product’

This is the hook you need:

add_action( 'woocommerce_single_product_summary', 'storefront_post_header_categories', 6 );
function storefront_post_header_categories() {
    echo "code to display categories here";
}

You will find it in plugins/woocommerce/content-single-product.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
         */
        do_action( 'woocommerce_single_product_summary' );

Leave a Comment