I need a custom image field that displays woocommerce product's second image (taken from the product default gallery).
I managed to pull the second image via a shortcode with the following code:
-------------------------
function second_img_func($atts, $content){
$product = wc_get_product( get_the_ID() );
$product_id = $product-> get_id();
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id )
{
echo wp_get_attachment_image($attachment_id, 'full');
}
}
-------------------------------------
but I need this image via a custom field to be able to use it in an Elementor widget.
Any ideas please?
Hi,
Thank you for contacting us and I'd be happy to assist.
The Elementor's dynamic source feature is something that is managed and maintained by Elementor:
lien caché
I did some research online and Elementor does seem to offer some hooks to customize the output of its widgets ( ref: lien caché ) or register a custom dynamic tag ( ref: lien caché ), both of which could work to achieve, what you're looking for.
I hope this helps and for the most optimal recommendations and alternatives, it would be best to consult their official support team.
regards,
Waqar
Thank you for your reply, Waqar.
I understand that Elementor's documentation is a good resource. However, my question was not very clear. Let's forget Elementor for a minute:
How can I grab the second product image and "place it" in a toolset custom field?
Is there a way to link or synchronize both fields? (second image URL and my Toolset custom field)?
Thanks for writing back.
> Is there a way to link or synchronize both fields? (second image URL and my Toolset custom field)?
- There is no built-in method available for this, so this will require code customization.
For example, you already have the code to get the values from the WooCommerce product images. You can add a custom function attached to the "save_post" hook ( ref: https://developer.wordpress.org/reference/hooks/save_post/ ) that is executed every time a product post is saved/updated and updates the second image's value in the Toolset's custom field ( ref: https://developer.wordpress.org/reference/functions/update_post_meta/ ).
For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/