hi, i need to add image gallery option in cred forms (adding and editing). One project (what we'll discuss here) are woo product image galleries, the other project is not via woocommerce.
I stumbled upon this https://toolset.com/documentation/user-guides/creating-woocommerce-products-using-cred-forms/ and that explains about everything in detail on everything possible, except, you guessed it, image galleries. It says that you can do it, and that's it?...
I also read https://toolset.com/forums/topic/how-to-set-woocommerce-product-gallery-with-cred/ but there's no details whatsoever.
How to handle the cred hook? There's full info on similar issue for downloadable products, but not for this.
I already tried via registering and that leaves me with 1 upload field that is not repeatable.
I already started with what Luo wrote: I suggest you create a custom repeating image field. Put it into your CRED form for user to submit images, after user submit the form, use action hook cred_save_data to store them as "Product Gallery"
--> when i create custom repeating field, i need to connect it to 'products'? Than it will show in the admin product pages as a new field and that's not what we need. How to do this step by step?
And then: after user submit the form, use action hook cred_save_data to store them as "Product Gallery" --> how, what, where??
I think I rather follow the other path 'Product Gallery needs to be handled using a CRED hook', but also here, due to lack on any further details: what, when, where??
Thanks for your kind help!
Hi, I will try to clarify this a bit for you.
When i create custom repeating field, i need to connect it to 'products'? Than it will show in the admin product pages as a new field and that's not what we need.
This is the best way to achieve what you want on the CRED form. In order to add a repeating image field on the CRED form, you must add a repeating image custom field to the Products post type. There is no repeating generic image field. If you do not want to show the fields in the product editor, you can collapse the post field group. If you find the field contents confusing in the product editor, you could delete the images from the custom fields programmatically at the same time you move them into an image gallery using the cred_save_data hook. Please let me know your thoughts on this, and if you feel like this is an acceptable approach.
After user submit the form, use action hook cred_save_data to store them as "Product Gallery" --> how, what, where??
The details of how this works depend on your answer to the first question. The general documentation for using the cred_save_data hook can be found here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Click the "More" button for real-world code samples. You can see how the hook is used to access information from the form and use that information to perform other tasks. This information is intended for users with general programming and PHP knowledge, but we are available to assist if you are not experienced.
I think I rather follow the other path 'Product Gallery needs to be handled using a CRED hook'
There is only one path - cred_save_data is a CRED hook. You can use the cred_save_data hook to capture information about the images that are uploaded in your repeated custom fields, and use that information to update the new Product post. This is when you will define Product Gallery images.
Hi Christian,
ok i can make that custom repeating field and connect it to products, and i'll try to find how to hide it from the backend admin when editing or creating non-cred products (to keep it simple for the customer who'll manage this)
So, that means that when a product is created via cred, the images will be seen on 'that' place instead of the default image gallery place, and via some hook action they will be moved to the default place (that will happen directly or do i need to trigger that action somehow by hand?)
I saw the hook documentation, this php will normally go into functions.php? You guessed corectly that my minimal PHP knowlegde won't be sufficient to rewrite it to that what we'll need ????
Thanks a lot for your help!
(ps in woocommerce settings -> API --> webhooks: should i do add this here as well, or is that something totally different? )
So, that means that when a product is created via cred, the images will be seen on 'that' place instead of the default image gallery place
It won't really be seen in the custom field. The code that moves images from the custom field into an image gallery will happen before the product page can be loaded in the admin area.
...and via some hook action they will be moved to the default place
Right, you will place code in the cred_save_data hook callback function that will cause this to happen right after the new product is created. It will happen so fast you will not be able to load the product page in wp-admin before the images are moved from the custom fields and into the product image gallery.
I saw the hook documentation, this php will normally go into functions.php?
Yes, you will add PHP code to functions.php, preferably in a child theme. You should not directly modify a theme's functions.php file. More info about that:
https://codex.wordpress.org/Child_Themes
woocommerce settings -> API --> webhooks: should i do add this here as well
No, that is unrelated to what we're working on. Nothing needed there.
Let me know when you need my assistance. I'll need you to create the CRED form and get that working before I can start writing code for the hook.
You're golden, Christian!
I have it ready, you probably need all access info, so i'll post it in the private reply next.
Thanks a lot!
Private reply enabled here.
Hi, to answer your question:
when i click a native taxonomy (non-toolset) such as 'product brands' or 'product categories', i get a 'Wrong Taxonomy specified.' error. Is this normal?
This is normal for any taxonomy that is not registered as a public taxonomy. WooCommerce's Product Categories and Product Tags are not public, so Types cannot modify them.
I'm checking out the other issue now, please standby.
Please add the following code to your theme's functions.php file:
/**
* Create product image gallery during CRED submission
*
*/
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']==69)
{
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');
}
}
}
Then give it a rip and let me know how it goes.
Wonderful work Christian, thanks a million! Just perfect! 🙂
Great - one more thing I should mention. In the Product editing screen, in the top right corner, click "Screen Options". You will find the ability to turn on and off individual post field groups here. This will help clean up your admin editor, but will leave the fields in place in the database.