Support

Account

Forum Replies Created

  • 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
  • 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>
Viewing 2 posts - 1 through 2 (of 2 total)