Support

Account

Home Forums Add-ons Repeater Field Repeater field images to woocommerce gallery

Solved

Repeater field images to woocommerce gallery

  • I just can’t get this thing to work…
    I was able to transfer post (product) image from ACF to Featured image (‘_thumbnail_id’) but I would like to do this with all product images.

    So, what I have is this:
    My product has a Repeater called ‘other_images’ and inside is a sub field ‘image’.

    I want to add a function in functions.php which will update_post_meta for every picture to post’s ‘_product_image_gallery’.

    If I export a product, we can see how meta is stored.
    Currently, woocommerce product gallery meta is constructed like this:

    		<wp:postmeta>
    			<wp:meta_key>_product_image_gallery</wp:meta_key>
    			<wp:meta_value><![CDATA[368,369]]></wp:meta_value>
    		</wp:postmeta>

    (368 and 369 are image IDs)

    My Repeater stores image ID to seperate meta key:

    		<wp:postmeta>
    			<wp:meta_key>other_images</wp:meta_key>
    			<wp:meta_value><![CDATA[2]]></wp:meta_value>
    		</wp:postmeta>
    		<wp:postmeta>
    			<wp:meta_key>_other_images</wp:meta_key>
    			<wp:meta_value><![CDATA[field_503e39680eec9]]></wp:meta_value>
    		</wp:postmeta>
    <wp:postmeta>
    			<wp:meta_key>other_images_0_picture</wp:meta_key>
    			<wp:meta_value><![CDATA[368]]></wp:meta_value>
    		</wp:postmeta>
    		<wp:postmeta>
    			<wp:meta_key>_other_images_0_picture</wp:meta_key>
    			<wp:meta_value><![CDATA[field_12]]></wp:meta_value>
    		</wp:postmeta>
    		<wp:postmeta>
    			<wp:meta_key>other_images_1_picture</wp:meta_key>
    			<wp:meta_value><![CDATA[369]]></wp:meta_value>
    		</wp:postmeta>
    		<wp:postmeta>
    			<wp:meta_key>_other_images_1_picture</wp:meta_key>
    			<wp:meta_value><![CDATA[field_12]]></wp:meta_value>
    		</wp:postmeta>
    

    Any idea how to get this done?

  • You need to add an acf/save_post filter which is documented here http://www.advancedcustomfields.com/resources/acfsave_post/

    The WooCommerce custom field stores a serialized array of image ID values. In the your save post filter you need to loop through your repeater field and add them to an array and then store this array into the WC custom field.

    
    // priority of 20 so it runs after ACF
    add_action('acf/save_post', 'my_acf_save_post', 20);
    function my_acf_save_post($post_id) {
      $repeater = 'other_images';
      $subfield = 'image';
      // using get_post_meta because that way 
      // we're sure to get the image ID
      $count = intval(get_post_meta($post_id, $repeater, true));
      $images = array();
      for ($i=0; $i<$count; $i++) {
        $field = $repeater.'_'.$i.'_'.$subfield;
        $images[] = intval(get_post_meta($post_id, $field, true));
      }
      update_post_meta($post_id, '_product_image_gallery', $images);
    }
    
  • Hello John

    I tried to implement your code.
    I see what you did there. Found image ID’s and put them in an array.
    But they are saved incorrectly. Here’s how it looks like now:
    (this product has 3 images)

    		<wp:postmeta>
    			<wp:meta_key>_product_image_gallery</wp:meta_key>
    			<wp:meta_value><![CDATA[a:3:{i:0;i:3688;i:1;i:3689;i:2;i:3690;}]]></wp:meta_value>
    		</wp:postmeta>
  • so it appears that woocommerce may be storing a comma separated list of id values? Now that I see the difference in the XML.

    You could try

    
    // priority of 20 so it runs after ACF
    add_action('acf/save_post', 'my_acf_save_post', 20);
    function my_acf_save_post($post_id) {
      $repeater = 'other_images';
      $subfield = 'image';
      // using get_post_meta because that way 
      // we're sure to get the image ID
      $count = intval(get_post_meta($post_id, $repeater, true));
      $images = array();
      for ($i=0; $i<$count; $i++) {
        $field = $repeater.'_'.$i.'_'.$subfield;
        $images[] = intval(get_post_meta($post_id, $field, true));
      }
      if (count($images)) {
        // convert to comma separated list
        $images = implode(',' $images);
      } else {
        $images = '';
      }
      update_post_meta($post_id, '_product_image_gallery', $images);
    }
    
  • Thank you John
    That worked!

    One comma was missing
    $images = implode(',' , $images);

    I’ll paste my functions.php here if anyone in future is doing the same:
    Transferring product data (featured image, images gallery, price) from ACF to WooCommerce

    function acf_set_featured_image( $value, $post_id, $field  ){   
        if($value != ''){
    	    //Add the value which is the image ID to the _thumbnail_id meta data for the current post
    	    add_post_meta($post_id, '_thumbnail_id', $value);
        }
        return $value;
    }
    add_filter('acf/update_value/name=my_featured_image', 'acf_set_featured_image', 10, 3);
    
    function acf_set_current_price( $value, $post_id, $field  ){    
        if($value != '')	    
    		update_post_meta($post_id, '_price', $value);
        return $value;
    }
    function acf_set_regular_price( $value, $post_id, $field  ){    
        if($value != '')	
    		update_post_meta($post_id, '_regular_price', $value);
        return $value;
    }
    add_filter('acf/update_value/name=my_price', 'acf_set_regular_price', 10, 3);
    
    function my_acf_save_post($post_id) {
    	$repeater = 'other_images';
    	$subfield = 'image';
    	// using get_post_meta because that way we're sure to get the image ID
    	$count = intval(get_post_meta($post_id, $repeater, true));
    	$images = array();
    	for ($i=0; $i<$count; $i++) {
    		$field = $repeater.'_'.$i.'_'.$subfield;
    		$images[] = intval(get_post_meta($post_id, $field, true));
    	}
    	if (count($images)) {
    		$images = implode(',', $images); // convert to comma separated list
    	} else {
    		$images = '';
    	}
    	update_post_meta($post_id, '_product_image_gallery', $images);
    }
    add_action('acf/save_post', 'my_acf_save_post', 20); // priority of 20 so it runs after ACF
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Repeater field images to woocommerce gallery’ is closed to new replies.