WooCommerce-外部/附属产品标题和存档页面上的图像到外部链接(“新建”标签)

时间:2018-12-06 20:45:07

标签: php wordpress woocommerce affiliate woothemes

我已将以下代码添加到我的function.php文件中,以使我的外部/关联产品标题和产品归档页面上的图像链接到外部链接(在新标签中打开)-由Oleg Apanovich提供。

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 target="_blank" href="' . $product->get_product_url() . '" class="">';
}
}

function woocommerce_add_aff_link_close(){
$product = wc_get_product(get_the_ID());

if( $product->is_type( 'external' ) ) {
    echo '</a>';
}
}


function woocommerce_template_loop_product_link_open() {
global $product;

if( $product->is_type( 'external' ) ) {
    $link = apply_filters( 'woocommerce_loop_product_link', $product->get_product_url(), $product );
    echo '<a target="_blank" href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
} else {
    $link = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
    echo '<a href="' . esc_url( $link ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
}
} 

您可以在此处查看该网站:https://dallavita.com/shop/

当我单击外部/附属产品图片和标题时,此代码将按预期工作。但是,我还有两个重复的“购买项目”按钮,它们显示并在单击时打开,但不在新选项卡中。

外部会员产品当前如何显示以及如何悬停: enter image description here

有关我希望如何显示外部/关联产品的详细信息: enter image description here

理想情况下,我希望在存档页面上具有外部/关联产品的样式,几乎与简单产品的样式完全匹配(而并非具有“添加到购物车”功能显示,而是购买产品) “按钮文本)。然后,当单击外部/附属产品标题,图像,价格或购买按钮时,它将在新选项卡中打开外部链接。

任何帮助将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

以下是将target="_blank"添加到存档页面上的链接以在新标签页中打开它们的方法:

function ns_open_in_new_tab($args, $product) 
{
    if( $product->is_type('external') ) {
        // Inject target="_blank" into the attributes array
        $args['attributes']['target'] = '_blank';
    }    

    return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );

用您自己的名称空间缩写替换ns_部分。