PHP Using variable in woocommerce shortcode

35 Views Asked by At

it seems like i have an easy question but i cannot figure it out why it does not work.

I have variable in which i am passing woocommerce product ID based on ACF chosen produkct. The product id in my case is 2118

I am using belowed code to make display woocommerce products base on chosen product in ACF.

<?php $value = the_field('powiazane_produkty');?>
<?php echo do_shortcode('[products limit="1" per_page="1"  ids="' . $value . '"]'); ?>

the $value variable works fine as it displays 2118, but why it doeasn't work in Woocommerce shortcode and it displays random products? You can see it on this pagex ex: https://onadlaniej.pl/doradztwo-zawodowe-klucz-do-rozwoju-kariery/ (search for 2118)

please help:)

Need help and support

1

There are 1 best solutions below

0
Supertramp On

try using get_field() instead of the_field() and after that maybe add an if statement that checks if $value is set before you try to echo your shortcode.