woocommerce offtopic – How to add product image to a Gravity Form from the catalogue page?

Question

I have managed to add the product image to the form when the form is opened from within the product page, but I need to add the product image when the same form is opened from the catalogue product list page.

This is what I have so far:

add_filter( 'gform_pre_render', 'add_product_image' );
function add_product_image($form) {
  global $post;

  if ($form['id'] != 12) {
    return $form;
  }

  foreach ($form["fields"] as &$field) {
    if ($field["id"] == 19) {
      $field["content"] = '<img src="' . get_the_post_thumbnail_url($post->ID, 'small')  . '" alt="Product Image" />';
    }
  }
  return $form;
}

When opening the form from the catalogue page it always renders the first product image not the clicked one.

Hope that makes sense.

0
Ricardo Sanchez 1 year 2022-09-02T04:03:19-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse