Skip to content
Advertisement

Display a custom field under WooCommerce single product title

I have problem with a custom fields in WooCommerce.

I have tille of product (book) and i want add author with custom field. I done it with register in theme custom fields and i add new one with WordPress custome field.

Custom fields call: product_author (that is my) and mbt_publisher_name (from theme)

I have found the template file title.php in theme and in woocommerce directory.

I tried to add:

<?php echo get_post_meta($id, "product_author", true); ?> 

and nothing change… orignal source from title.php

    <?php
/**
 * Single Product title
 *
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

?>

<h2 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h2>

what should I do to display that custom field under title?
Where I can find that hook?

Thanks

Advertisement

Answer

If you look at the woocommerce template content-single-product.php you will see this code (starting on line 54):

/**
 * 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' );

So woocommerce_template_single_title is hooked in woocommerce_single_product_summary action hook with a priority of 5 (so it comes first).

You can do it in 2 Ways:

1) you can use a custom function hooked in woocommerce_single_product_summary hook with a priority between 6 to 9, this way:

add_action( 'woocommerce_single_product_summary', 'custom_action_after_single_product_title', 6 );
function custom_action_after_single_product_title() { 
    global $product; 

    $product_id = $product->get_id(); // The product ID

    // Your custom field "Book author"
    $book_author = get_post_meta($product_id, "product_author", true);

    // Displaying your custom field under the title
    echo '<p class="book-author">' . $book_author . '</p>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works on WooCommerce 3.0+


Or

2) You can edit directly the template single-product/title.php located in the WooCommerce folder of your active theme (see below the reference about overriding WooCommerce templates through theme):

<?php
/**
 * Single Product title
 *
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

// Calling global WC_Product object
global $product;

$product_id = $product->get_id(); // The product ID

// Your custom field "Book author"
$book_author = get_post_meta($product_id, "product_author", true);

?>

<h2 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h2>

<p class="book-author"><?php echo $book_author; ?></p>

Official reference: Template Structure + Overriding WooCommerce Templates via a Theme

I recommend you the first method because it’s cleaner as it uses a hook and you will not need to make any changes if templates are updated. You should also better use a child themeā€¦

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement