Support

Account

Home Forums Add-ons Gallery Field Use gallery field to set both WooCommerce product thumbnail and gallery images

Unread

Use gallery field to set both WooCommerce product thumbnail and gallery images

  • Hello,

    I was wondering if anyone had any advice on how I might use the ACF gallery field to set the featured image as well as the product gallery images of a WooCommerce product.

    I have already followed this excellent little tutorial from cfxdesign.com, Which shows how to use an image filed to set the featured image, and then a separate gallery field to set the product gallery images.

    But I would like to accomplish this with a single gallery field, using the first image in the gallery as the featured image.

    This ACF forum thread shows how to take the first image in a gallery and set it as the featured image. So I’ve almost achieved my goal.

    Unfortunately combining these two methods leaves me with the issue that the first image in the gallery field gets set as the featured image and is also added to the product gallery, creating a duplicate image when viewing the product on the front end.

    So I am trying to figure out how to exclude the first image on the gallery field from being added to the WooCommerce product gallery. If that makes sense.

    Any input would be much appreciated. thanks!

    Here is the (probably cringe-worthy) code that I am currently using bassed of those previous tutorials.

    // Set the first image in the acf gallery field as the featured image
    
    function flex_FeaturedImageSetByACF() {
    
        $current_screen         = get_current_screen(); // Current admin screen needed to identify the current cpt
        $current_cpt_name       = $current_screen->post_type; // Current cpt name
        $current_cpt_support    = 'thumbnail'; // We want to check if the CPT supports this feature
    
        global $post;
    
        $post_id                = ( $post->ID ); // Current post ID
        $post_gallery_field     = get_field('product_gallery', $post_id ); // ACF field
    
        if  ( !empty( $post_id ) ) {
    
            if ( isset( $post_gallery_field['0'] ) ) {
    
                $post_image_id          = $post_gallery_field['0']['id']; // ACF image filed ID
                $post_image_url         = $post_gallery_field['0']['url']; // ACF image filed URL
    
                // If current cpt supports thumbnails/featured images
    
                if ( post_type_supports( $current_cpt_name, $current_cpt_support ) ) {
    
                    if ( ( $post_image_url ) AND ( ( $post_image_url ) != ( get_the_post_thumbnail() ) ) ) {
    
                        update_post_meta($post_id, '_thumbnail_id', $post_image_id);
    
                    }
    
                }
    
            } else {
    
                update_post_meta( $post_id, '_thumbnail_id', 0 );
    
            }
    
        }
    
    } add_action('acf/save_post', 'flex_FeaturedImageSetByACF', 50);
    
    // Add images in acf gallery field to the woocommerce product gallery
    
    function use_acf_fields_for_product_photos($post_id) {
        if('product' != get_post_type($post_id)) {
          return false;
        }
        $product_gallery = get_field('product_gallery', $post_id);
        if($product_gallery) {
          $product_gallery_ids_string = implode(',', wp_list_pluck($product_gallery, 'ID'));
          update_post_meta($post_id, '_product_image_gallery', $product_gallery_ids_string);
        }
      }
    add_action('acf/save_post', 'use_acf_fields_for_product_photos', 20);
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.