Skip to content
Advertisement

Display custom field value in WooCommerce single product pages via hook

I don’t know much about PHP yet, but I’m trying to place some content in my single product pages by using the woocommerce_before_add_to_cart_form hook.

I succeeded in making a code that prints my text and the Total sales of my products. The code looks like this:

JavaScript

But instead of product total sales I want my product custom field value to be displayed.

This is the custom field I added fullfilment_production_time custom field:

custom field

I tried changing the get_total_sales() to get_fullfilment_production_time() but that didn’t work.

I also tried this one:

JavaScript

Any advice?

Advertisement

Answer

There is a mistake in your code when using get_post_meta() function in a wrong way. You can use different ways to get a product custom field value:

  1. The WooCommerce way using WC_Data method get_meta() this way:
JavaScript
  1. The old WordPress way using get_post_meta() function this way:
JavaScript
  1. For Advanced custom fields plugin using get_field() function this way:
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement