• I am trying to redirect the product grid image to external product URL (which is specified for Add to cart button). I have some code which worked with storefront theme, but it’s not working with Virtue theme. Please, let me know the solution. Thanks,

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hey,

    Try following this guide and let me know if it works for you:
    https://woocommerce.com/videos/woocommerce-external-affiliate-product-overview/

    -Kevin

    Thread Starter shamleyk

    (@shamleyk)

    Hi Kevin,

    Thanks for the quick reply. I have already set up external product links. But when I click on a Product thumbnail on SHOP page, it takes me to a Single Product Page which I don’t want. Instead I want it to take me to the affiliate product link and open it in new tab. Following code works with storefront theme, but not with Virtue.

    remove_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_loop_product_link_close’, 5 );

    add_action(‘woocommerce_before_shop_loop_item_title’, ‘woocommerce_template_loop_product_link_open’, 15);
    add_action(‘woocommerce_before_shop_loop_item’, ‘woocommerce_add_aff_link_open’, 10);
    add_action(‘woocommerce_before_shop_loop_item_title’, ‘woocommerce_add_aff_link_close’, 10);

    function woocommerce_add_aff_link_open(){
    $product = wc_get_product(get_the_ID());
    if( $product->is_type( ‘external’ ) )
    echo ‘get_product_url() . ‘” class=”woocommerce-LoopProductImage-link”>’;
    }

    function woocommerce_add_aff_link_close(){
    $product = wc_get_product(get_the_ID());
    if( $product->is_type( ‘external’ ) )
    echo ‘
    ‘;
    }

    Thanks,

    Thread Starter shamleyk

    (@shamleyk)

    I was looking at the wrong content-product.php file. The one under Virtue theme folder is overriding standard woocommerce file. I want to change the_permalink() in below code to the external product URL. Please help as I tried many variations, nothing worked. Dont know PHP.

    <?php
    /**
    * woocommerce_before_shop_loop_item hook.
    *
    * @hooked woocommerce_template_loop_product_link_open – 10
    */
    do_action( ‘woocommerce_before_shop_loop_item’ ); ?>

    ” class=”product_item_link product_img_link”>

    <?php
    /**
    * woocommerce_before_shop_loop_item_title hook
    *
    *
    @hooked woocommerce_show_product_loop_sale_flash – 10
    * @hooked woocommerce_template_loop_product_thumbnail – 10
    */
    do_action( ‘woocommerce_before_shop_loop_item_title’ );
    ?>


    <div class=”product_details”>
    ” class=”product_item_link”>
    <?php
    /**
    * woocommerce_shop_loop_item_title hook
    *
    *
    @hooked woocommerce_template_loop_product_title – 10
    */
    do_action( ‘woocommerce_shop_loop_item_title’ );
    ?>

    <?php if ( apply_filters( ‘kadence_product_archive_excerpt’, true ) ) : ?>
    <div class=”product_excerpt”>
    <?php
    if ($post->post_excerpt){
    echo apply_filters( ‘archive_woocommerce_short_description’, $post->post_excerpt );
    } else {
    the_excerpt();
    } ?>
    </div>
    <?php endif; ?>
    </div>
    <?php
    /**
    * woocommerce_after_shop_loop_item_title hook
    *
    * @hooked woocommerce_template_loop_price – 10
    */
    do_action( ‘woocommerce_after_shop_loop_item_title’ );
    ?>

    <?php do_action( ‘woocommerce_after_shop_loop_item’ ); ?>

    Thread Starter shamleyk

    (@shamleyk)

    here is the text code –

    
    <?php 
    	/**
    	 * woocommerce_before_shop_loop_item hook.
    	 *
    	 * @hooked woocommerce_template_loop_product_link_open - 10
    	 */
    	do_action( 'woocommerce_before_shop_loop_item' ); ?>
    
    	<a href="<?php the_permalink(); ?>" class="product_item_link product_img_link">
    
    		<?php
    			/**
    			 * woocommerce_before_shop_loop_item_title hook
    			 *
    			 * @hooked woocommerce_show_product_loop_sale_flash - 10
    			 * @hooked woocommerce_template_loop_product_thumbnail - 10
    			 */
    			do_action( 'woocommerce_before_shop_loop_item_title' );
    		 	?>
        
        </a>
    		<div class="product_details">
    			<a href="<?php the_permalink(); ?>" class="product_item_link">
    			<?php 
    				/**
    			 	* woocommerce_shop_loop_item_title hook
    			 	*
    			 	* @hooked woocommerce_template_loop_product_title - 10
    			 	*/
    				do_action( 'woocommerce_shop_loop_item_title' );
    				?>
    			</a>
    
    			<?php if ( apply_filters( 'kadence_product_archive_excerpt', true ) ) : ?>
    					<div class="product_excerpt">
    						<?php
    						if ($post->post_excerpt){
    							echo apply_filters( 'archive_woocommerce_short_description', $post->post_excerpt );
    						} else {
    							the_excerpt();
    						} ?>
    					</div>
    			<?php endif; ?>
    		</div>
    		<?php
    			/**
    			 * woocommerce_after_shop_loop_item_title hook
    			 *
    			 * @hooked woocommerce_template_loop_price - 10
    			 */
    			do_action( 'woocommerce_after_shop_loop_item_title' );
    		?>
    
    	<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>

    I just pushed out a theme update. Update your site.

    Then in your child theme functions.php file add this:

    function custom_child_init() {
    	// Remove Virtue
    	remove_action( 'woocommerce_before_shop_loop_item_title', 'virtue_woocommerce_image_link_open', 5 );
    	// Add Custom
    	function custom_kt_archive_image_link_open() {
    		global $product, $kt_woo_extras;
    		if( $product->is_type( 'external' ) ) {
    				echo  '<a href="'.esc_url( $product->get_product_url() ).'" class="product_item_link product_img_link" target="_blank">';
    		} else {
    			echo  '<a href="'.esc_url( get_the_permalink() ).'" class="product_item_link product_img_link">';
    		}
    	}
    	add_action( 'woocommerce_before_shop_loop_item_title', 'custom_kt_archive_image_link_open', 2 );
    }
    add_action('init', 'custom_child_init');

    Make sure your not overriding any template files with your child theme.

    Ben

    Thread Starter shamleyk

    (@shamleyk)

    Thank you so much! It works.
    But Product Item Title is still using the old link instead of the new custom link. Can both the image and title use the same custom defined link?

    Thanks.

    Hmm for this you would need to update to 3.1.3

    then add this function:

    
    function custom_child_init_second() {
    	// Remove Virtue
    	remove_action( 'woocommerce_shop_loop_item_title', 'virtue_woocommerce_archive_title_link_start', 7 );
    	// Add Custom
    	function custom_kt_archive_title_link_open() {
    		global $product, $kt_woo_extras;
    		if( $product->is_type( 'external' ) ) {
    				echo  '<a href="'.esc_url( $product->get_product_url() ).'" class="product_item_link" target="_blank">';
    		} else {
    			echo  '<a href="'.esc_url( get_the_permalink() ).'" class="product_item_link">';
    		}
    	}
    	add_action( 'woocommerce_shop_loop_item_title', 'custom_kt_archive_title_link_open', 7 );
    }
    add_action('init', 'custom_child_init_second');

    Ben

    Thread Starter shamleyk

    (@shamleyk)

    Thank you Ben.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Virtue Product grid image redirection’ is closed to new replies.