Rémi
Rémi

Reputation: 27

Get product custom fields ACF in post page wordpress

I am trying to get a product custom field ( ACF ) based on product ID in a post page.

I tried things like this $price[] = get_sub_field('price', $productId); but it doesn't seems to work when taking something else than the post id.

I managed to get the title though by doing like this <?php echo $product->get_title(); ?> but not sure on how to go to get the custom fields.

Upvotes: 0

Views: 1339

Answers (1)

Gufran Hasan
Gufran Hasan

Reputation: 9401

As we know $product->ID will not work anyway. So I think now your problem is how to get the product ID.

=> In Archive pages like shop or in Single product pages: Using get_the_id() will give you mostly always the product id (the post ID).

$price= get_post_meta( get_the_id(), 'price', true );

reference link

You can also use global $product; with $product->get_id()

global $product;
$price = get_post_meta( $product->get_id(), 'price', true );

Upvotes: 1

Related Questions