0

I'm trying to add my own custom fields to the single product summary after the main product's title output on my product page. I've looked into the woocommerce documentation (https://github.com/woothemes/woocommerce/blob/2.3.8/templates/content-single-product.php) and it looks like woocommerce_single_product_summary is my hook. So here's what I did:

// should hook into woocommerce single product summary and output info
function add_subtitle_to_product() {
    if (get_field('subtitle', get_the_ID())) {
        $output = '<h4>'.get_field('subtitle', get_the_ID()).'</h4>';
    }
    return $output;
}
   add_action( 'woocommerce_single_product_summary', 'add_subtitle_to_product', 6 );

And it wasn't working. I even made sure the get_field() call was returning true. So I decided to try a different approach:

// hook into woocommerce product summary and output test text
add_action('woocommerce_single_product_summary', function() {
    $output = '<p>Test</p>';
    return $output;
}, 7);

I tried this as a normal function with a name but that didn't work so I opted for an anonymous function that still isn't working.

What could be my issue?

3
  • this is a barely legit question, but in the future please don't ask WC and ACF questions here as all plugin specific questions are off-topic here. Commented Oct 1, 2015 at 7:13
  • Okay. I've seen questions like this asked here so that's why I asked here. I'll look to their forums in the future.
    – FranticJ3
    Commented Oct 1, 2015 at 13:13
  • the questions are being asked, but especially in WC case they rarely get any answer which is the reason are just discouraged from asking them and the modarators will close them sooner or later. Commented Oct 1, 2015 at 16:09

1 Answer 1

1

In your first code block try changing return $output; with echo $output; and that should work provided $output is not empty :)

If you are trying to append your custom field to the product summary, this can be very well achieved using woocommerce_short_description filter hook. Please see below code:


function add_subtitle_to_product() {
    global $post;
    if (get_field('subtitle', get_the_ID()))
        return $post->post_excerpt . ''.get_field('subtitle', get_the_ID()).'';
    else
        return $post->post_excerpt;
}
add_action( 'woocommerce_short_description', 'add_subtitle_to_product', 10, 2 );
1
  • Interesting. I was using echo previously but it wasn't working.. Works now though. Thanks!
    – FranticJ3
    Commented Oct 1, 2015 at 13:15

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.