I need to populate the woocommerce gallery and featured image fields. (Advanced post creation and File Upload Pro)

Hi everyone,

I have a form that uses File Upload Pro for two fields (Hoofdafbeelding ID:18 and Detailfoto’s ID:19)

Field 18 has to add the Featured Image to a woocommerce product through Advanced Post Creation add-on, and Field 19 does the same for the woocommerce gallery. But I can’t figure out how to make this work.

I also tried to hook into the function to do it manually, but that didn’t work either:

add_action(‘gform_advancedpostcreation_post_after_creation’, function($post_id, $feed, $entry, $form) {

// Only run for form 14
if ($form['id'] != 14) {
    return;
}

/**
 * -----------------------------
 * FEATURED IMAGE (FIELD 18)
 * -----------------------------
 */
$featured = rgar($entry, '18');

if (!empty($featured)) {

    // File Upload Pro often returns array or string
    $featured_url = is_array($featured) ? reset($featured) : $featured;

    $featured_id = attachment_url_to_postid($featured_url);

    if ($featured_id) {
        set_post_thumbnail($post_id, $featured_id);
    }
}

/**
 * -----------------------------
 * GALLERY IMAGES (FIELD 19)
 * -----------------------------
 */
$gallery = rgar($entry, '19');

$ids = [];

if (!empty($gallery)) {

    // Normalize to array
    $gallery_items = is_array($gallery) ? $gallery : [$gallery];

    foreach ($gallery_items as $item) {

        $url = is_array($item) ? reset($item) : $item;

        $attachment_id = attachment_url_to_postid($url);

        if ($attachment_id) {
            $ids[] = $attachment_id;
        }
    }

    if (!empty($ids)) {
        update_post_meta($post_id, '_product_image_gallery', implode(',', $ids));
    }
}

}, 10, 4);

Hey Hellenique,

Thanks for sharing the details and the snippet.

We actually have a guide that covers uploading images to WooCommerce products via Advanced Post Creation here: How to Create WooCommerce Products with Gravity Forms | Gravity Wiz

For the Product Gallery specifically, please see this section: How to Create WooCommerce Products with Gravity Forms | Gravity Wiz

This setup would require an additional perk though: Gravity Forms Media Library | Gravity Perks by Gravity Wiz , since the uploaded files need to be added to the WordPress Media Library before WooCommerce can use them as attachments for the Product Gallery.

If this still isn’t working properly on your end after following the setup in the article, please let us know or feel free to reach out through our support channel here so we can take a closer look at your setup.

Best,