4

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

1 Answer 1

21

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…

2
  • 1
    The second way is correct :) nice... Thank You so much... may I ask one more Question to you?? I Would like create link to all books of author... it is possible?? i have some function in theme with bottom (called "by...'author' added like user not an author of book) but is is in wrong place (under price - I can't find where is it) and URL show 404 error page. Commented Jun 15, 2017 at 9:39
  • Thanks again @LoicTheAztec for helping us with the understanding of WooCommerce architecture.
    – jjj
    Commented Aug 30, 2019 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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