Home > Back-end >  Display product images DYNAMICALLY in Woocommerce product gallery
Display product images DYNAMICALLY in Woocommerce product gallery

Time:11-28

I have a Woocommerce shop with hundreds of items with unique images. Some of these items could re-use the same "common" image, and I already have a script for calling the common images reliably. However, I do not know how to include the common image in the product page gallery without modifying the postmeta table record. By calling the update_post_meta function, I can insert the image ID and have it show up in the gallery. But doing it this way is not really a good approach because if I ever have to change the common image down the road, then it will start to become a pain in the butt. I'd much rather keep the script dynamic, and only load the common images as the product page generates the PHP. In other words, I do not want any table records being updated in the WP tables just to display common images. Is there a way to do this? I'm convinced there is a way to do this by modifying my theme files, however, I wanted to know if there was already a built-in Wordpress action/filter that allows images to be inserted dynamically without modifying the theme files

CodePudding user response:

This filter should work

add_filter('woocommerce_product_get_gallery_image_ids', 'add_dynamic_gallery_images', 10, 2);

function add_dynamic_gallery_images($ids, $product){
  $ids[] = 100; //image ID to add
  return $ids;
}
  • Related