-1

I'd like to display the custom attribute book_author below the product title on single product pages, like so:

Product title

book_author

Price

I've come across various solutions for achieving similar changes to woocommerce_single_product_summary but none that has successfully displayed a linked custom attribute between the title and price.

I've achieved the desired result on the shop loop using the following, so if there was a way to adapt this code to apply to single product pages also, that would be ideal.

add_action( 'woocommerce_after_shop_loop_item_title', 'display_book_author_attribute', 5 );
function display_book_author_attribute() {
    global $product;

    if ( $product->is_type('simple') ) {
        $taxonomy = 'pa_book_author';
        echo '<span class="attribute-book_author">' . $product->get_attribute($taxonomy) . '</span>';
    }
}

Any and all advice would be hugely appreciated here, thanks so much!

Edit: I can use both action hooks together for the same function, however (to my knowledge) this then ties <span class> meaning I'm unable to style the attribute differently on shop pages and product pages, ie:

```add_action( 'woocommerce_single_product_summary', 'display_book_author_attribute', 5 );
add_action( 'woocommerce_after_shop_loop_item_title', 'display_book_author_attribute', 5 );
function display_book_author_attribute() {
    global $product;

    if ( $product->is_type('simple') ) {
        $taxonomy = 'pa_book_author';
        echo '<span class="attribute-book_author">' . $product->get_attribute($taxonomy) . '</span>';
    }
}`
ojs81
  • 9
  • 3

1 Answers1

0

Use woocommerce_single_product_summary action hook. also check here WooCommerce Visual Hook Guide: Single Product Page

add_action( 'woocommerce_single_product_summary', 'display_book_author_attribute', 5 );
function display_book_author_attribute() {
    global $product;

    if ( $product->is_type('simple') ) {
        $taxonomy = 'pa_book_author';
        echo '<span class="attribute-book_author">' . $product->get_attribute($taxonomy) . '</span>';
    }
}
Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • Thanks so much for this, please see my edit above for extra detail re the issue I've had using both action hooks for the same function. – ojs81 Apr 14 '21 at 14:20