Skip Navigation

[Resolved] Create a custom image field that will display the second image product

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 1 year, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2506849

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?

#2507477

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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:
hidden link

I did some research online and Elementor does seem to offer some hooks to customize the output of its widgets ( ref: hidden link ) or register a custom dynamic tag ( ref: hidden link ), 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

#2508155

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)?

#2510347

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.