Skip Navigation

[Resolved] How to Custom Image field url as Product Featured Image

This support ticket is created 4 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1782675

Hi Support team,
I am using toolset with a WooCommerce website but I have stored images of the products in a custom field created by toolset and field name is "product-image" and I want to use the custom field image as product featured image to display that image in different parts of the site like when product is added to cart then it should display as product thumbnail etc.

So please help me if there is any way I can copy the custom field URL and set it as featured image of the product as well.

I was just testing a function but it doesn't work.

add_filter(‘aepc_feed_item’, function($fields) {
$item_id = $fields['g:id'];

// TODO HERE Get the new image link and assign to ‘g:image_link’
$fields['g:image_link'] = get_post_meta($item_id, 'wpcf-product-image', true);

return $fields;
});

#1783351

I was just testing a function but it doesn't work.

add_filter('aepc_feed_item', function($fields) {

Hello, I'm not familiar with the filter called aepc_feed_item so I'm not sure what it's intended for. I did a quick search online and found that it seems to be related to a 3rd-party plugin called "Pixel Caffeine":
https://wordpress.org/support/topic/custom-label-3/

I'm not sure if this will do what you want, and since it's not a Toolset function I cannot guarantee it will work as expected. The plugin description says this:
The easiest, most powerful, 100% free, WordPress Plugin to manage Facebook Pixel and Facebook Product Catalog.
That doesn't sound like it has anything to do with WooCommerce products displayed on your website. As a test, you could try setting the value using a hard-coded image URL from your Media Library. This would help you verify whether or not the function does what you want, before you spend too much time with this code. If it doesn't work with a hard-coded image URL, it certainly will not work with the custom field value.

So please help me if there is any way I can copy the custom field URL and set it as featured image of the product as well.
It's not simple, because Featured Images in WordPress are set using image IDs, not image URLs. If you want set a Featured Image based on the value of another custom field and make that change permanent in the database, normally you would do that when the product is saved or updated in wp-admin, or in a front-end Toolset Form. Let us assume your Products are created and edited in wp-admin, not using Toolset Forms. In this case, you could use the save_post hook to trigger your own custom code: https://developer.wordpress.org/reference/hooks/save_post/
Custom code solutions are a bit outside the scope of support we provide here in the forum, but I can help debug any code that touches the Toolset APIs. Some other tickets you can use as a reference:
https://toolset.com/forums/topic/setting-as-featured-image-from-post-image-field/
https://toolset.com/forums/topic/featured-image-from-custom-field/
https://toolset.com/forums/topic/featured-image-from-image-field/

#1783621

Hi, I know that's a third party code,

Anyway, I found a very close solution that works but not pulling the right images from the targeted product custom field where image is stored, I tried to customize the code but its not completely working and breaking the shortcode that I am using to pull image. So can you please help me make this code work by making required updates?

add_filter( 'woocommerce_cart_item_thumbnail', 'change_woocommerce_cart_item_thumbnail', 20, 3 ); 
function change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key ){ 
    // HERE your targeted product ID
    $targeted_id = $cart_item['product_id'];

    if( $cart_item['product_id'] == $targeted_id || $cart_item['product_id'] == $targeted_id ){
        echo "<img src='[types field='product-image' output='raw'][/types]'>";
    }
}
#1783633

Please igonre above message because I found the solution by myself. I made all required updates to fetch the field value and pull it to the cart page.

add_filter( 'woocommerce_cart_item_thumbnail', 'change_woocommerce_cart_item_thumbnail', 20, 3 ); 
function change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key ){ 
    // HERE your targeted product ID
    $targeted_id = $cart_item['product_id'];
	$image_url = get_post_meta( $targeted_id, 'wpcf-product-image', true );
    if( $cart_item['product_id'] == $targeted_id || $cart_item['product_id'] == $targeted_id ){
        echo "<img src='$image_url'>";
    }
}
#1783663

My issue is resolved now. Thank you!

Here is the working function.

add_filter( 'woocommerce_cart_item_thumbnail', 'change_woocommerce_cart_item_thumbnail', 20, 3 );
function change_woocommerce_cart_item_thumbnail( $thumbnail, $cart_item, $cart_item_key ){
// HERE your targeted product ID
$targeted_id = $cart_item['product_id'];
$image_url = get_post_meta( $targeted_id, 'wpcf-product-image', true );
if( $cart_item['product_id'] == $targeted_id || $cart_item['product_id'] == $targeted_id ){
echo "<img src='$image_url'>";
}
}