A client wants to use CRED to create new WC Products. When creating the Product, the Client would like to upload multiple images that will be used to create an image gallery. The solution is to add a repeating image custom field, and use the cred_save_data hook to move those images out of the custom fields and into a product gallery.
Ticket link: https://wp-types.com/forums/topic/cred-repeating-image-fields-gallery-woo-and-non-woo/
/* ----------------------------------------------------------------------- */
// CREATE A PRODUCT IMAGE GALLERY USING A REPEATING IMAGE FIELD IN CRED
// User uploads to a repeating custom image field
// Images are automatically moved into product image gallery
// Custom fields are left blank
// keywords: cred, product, image, gallery, repeating, field
add_action('cred_save_data', 'cred_product_gallery_action',10,2);
function cred_product_gallery_action($post_id, $form_data)
{
global $wpdb;
// if a specific form
if ($form_data['id']==12345)
{
if (isset($_POST['wpcf-product-gallery-image-cred']))
{
$list_ids = array();
foreach($_POST['wpcf-product-gallery-image-cred'] as $url) {
$item_id = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $url ));
$list_ids[] = $item_id[0];
}
update_post_meta($post_id, '_product_image_gallery', implode($list_ids,','));
delete_post_meta($post_id, 'wpcf-product-gallery-image-cred');
}
}
}