I’m trying to display video of a WooCommerce product on shop page using Advanced Custom Fields plugin .
I want to create a condition, if a product has a video, then show it instead of woocommerce_template_loop_product_thumbnail
.
$file = get_field('archive_video'); if( $file ) { ?> <video width="200" muted loop autoplay src="<?php echo $file['url']; ?>"></video> <?php } else { /** * @hooked woocommerce_template_loop_product_thumbnail - 10; */ }
Am I choosing the correct approach?
Here how it should look like:
Advertisement
Answer
Try the following way using hooks instead of overriding template:
add_action( 'woocommerce_before_shop_loop_item_title', 'action_template_loop_product_thumbnail', 9 ); function action_template_loop_product_thumbnail() { global $product; $file = get_field('archive_video', $product->get_id()); if( isset($file['url']) && ! empty($file['url']) ) { remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 ); echo '<video width="200" muted loop autoplay src="' . $file['url'] . '"></video>'; } }
Code goes in functions.php file of your active child theme (or active theme). It should works.