Woocommerce - external/affiliate product image to external link (buy URL)

可紊 提交于 2019-12-04 21:58:14
Sadoo

WooCommerce uses a template for those images. They are product-thumbnails.php and 'product-image.php' files located in single-product folder in your WooCommerce templates folder.

For me these files are located in theme/woocommerce/single-product/ and "theme" is your theme (in this case Avada).

Based on what your theme does to WooCommerce templates (some themes rewrite them completely) you can just change some of those template files directly and add your links.

But the cleaner way would be to use actions that are used widely in WooCommerce and add your link to the images using them. The way I see it in an older version of Avada the themes modifies 'woocommerce_single_product_image_html' filter hook which shows the product main image.

If you share the files product-thumbnails.php and product-image.php with us we could find an easy solution.

I personally don't have access to your themes to see what exactly it does with images.

For now, you can put this code in your functions.php file and see how it turns out:

add_filter('woocommerce_single_product_image_html', 'change_product_image_link', 10, 2);

function change_product_image_link( $html, $product_id) {
  $product = wc_get_product($product_id);
  if (is_singular('product') &&
      $product &&
      $product->is_type('external')){

    $attachment_count = count($product->get_gallery_attachment_ids());
    $gallery          = $attachment_count > 0 ? '[product-gallery]' : '';
    $props            = wc_get_product_attachment_props(get_post_thumbnail_id(), $post);
    $image            = get_the_post_thumbnail(
                            $post->ID,
                            apply_filters('single_product_large_thumbnail_size',
                                          'shop_single'),
                            array(
                                'title' => $props['title'],
                                'alt'   => $props['alt'],
                            ));
    return sprintf(
        '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s">%s</a>',
        $product->get_product_url(),
        esc_attr($props['caption']),
        $image
    );
  }
  else {
    return $html;
  }
}

To add external links to archive product images you can put this code in your functions.php:

remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open');
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 '<a href="' .
         $product->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 '</a>';
}

The second changes mixed with Avada's content-product.php file (I strongly suggest removing #2 edits from functions.php before using this edit):

<?php
/**
 * The template for displaying product content within loops
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template
   files and you (the theme developer) will need to copy the new
   files to your theme to maintain compatibility. We try to do this
   as little as possible, but it does happen. When this occurs the
   version of the template file will be bumped and the readme will
   list any important changes.
 *
 * @see     http://docs.woothemes.com/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.5.0
 */

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

global $product, $woocommerce_loop;

// Store loop count we're currently on
if (empty($woocommerce_loop['loop'])) {
    $woocommerce_loop['loop'] = 0;
}

// Store column count for displaying the grid
if (empty( $woocommerce_loop['columns'])) {
    $woocommerce_loop['columns'] = apply_filters('loop_shop_columns', 4);
}

// Ensure visibility
if (! $product || ! $product->is_visible()) {
    return;
}

// Increase loop count
$woocommerce_loop['loop']++;

// Extra post classes
$classes = array();
if ( 0 === ($woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] ||
     1 === $woocommerce_loop['columns']) {

    $classes[] = 'first';
}
if ( 0 === $woocommerce_loop['loop'] % $woocommerce_loop['columns']) {
    $classes[] = 'last';
}
?>
<li <?php post_class($classes); ?>>

    <?php
    /**
     * woocommerce_before_shop_loop_item hook.
     *
     * @hooked woocommerce_template_loop_product_link_open - 10
     */
    do_action('woocommerce_before_shop_loop_item');
    ?>

    <?php
        if ('clean' != Avada()->settings->get('woocommerce_product_box_design')) {
          if ($product->is_type('external')) {
            echo '<a href="' .
                 $product->get_product_url() .
                 '" class="product-images">';
          }
          else {
            echo '<a href="' .
                 get_permalink() .
                 '" class="product-images">';
          }
        }
    ?>

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

    <?php
        if ('clean' != Avada()->settings->get('woocommerce_product_box_design')) :
    ?>
        </a>
    <?php endif; ?>

    <div class="product-details">
        <div class="product-details-container">
            <?php
                /**
                 * woocommerce_shop_loop_item_title hook.
                 *
                 * @hooked woocommerce_template_loop_product_title - 10
                 */
                do_action('woocommerce_shop_loop_item_title');
            ?>

            <div class="clearfix">

                <?php
                    /**
                     * woocommerce_after_shop_loop_item_title hook.
                     *
                     * @hooked woocommerce_template_loop_rating - 5
                     * @hooked woocommerce_template_loop_price - 10
                     */
                    do_action('woocommerce_after_shop_loop_item_title');
                ?>

            </div>
        </div>
    </div>

    <?php
        /**
         * woocommerce_after_shop_loop_item hook.
         *
         * @hooked woocommerce_template_loop_product_link_close - 5
         * @hooked woocommerce_template_loop_add_to_cart - 10
         */
        do_action( 'woocommerce_after_shop_loop_item');
    ?>

</li>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!