[英]Woocommerce - External/Affiliate Product Image and title to External Link (New tab)
I'm starting with programming with wordpress and woocommerce.我开始使用 wordpress 和 woocommerce 进行编程。 I need help after searching for Stack.
搜索 Stack 后我需要帮助。
I need the images and titles of the products of my store to point to the affiliate link without going through the single post page.我需要我商店产品的图片和标题指向附属链接,而无需通过单个帖子页面。 And that everything opens in a new tab.
并且所有内容都在新选项卡中打开。
Been following this thread: Woocommerce - External/Affiliate Product Image to External Link (Buy url)一直在关注这个线程: Woocommerce - 外部/附属产品图片到外部链接(购买网址)
I have used the Edit # 2 code from Sadoo and it works perfectly for me.我使用了来自 Sadoo 的 Edit # 2 代码,它非常适合我。
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>';
}
I just need the title to link like the image and everything opens in new tabs.我只需要标题像图像一样链接,所有内容都会在新标签中打开。
Can someone say how can I continue?有人可以说我该如何继续?
Thank you谢谢
If you want to open link attached to image in a new tab for a external products in your shop page then edit your code like these如果您想在商店页面中的外部产品的新选项卡中打开附加到图像的链接,请像这样编辑您的代码
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>';
}
}
Then if you want title will be opened in a new tab with external link than add these overridden woocommerce function to your functions.php file of your theme too然后,如果您希望标题将在带有外部链接的新选项卡中打开,而不是将这些覆盖的 woocommerce 函数添加到主题的 functions.php 文件中
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">';
}
}
Here is an cleaner approach to how you add target="_blank"
to links on archive pages to open them in new tab:这是一种更简洁的方法,用于将
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 );
Replace ns_
part with your own namespace abbreviation.将
ns_
部分替换为您自己的命名空间缩写。
does anyone applied this to FLATSOME theme?有没有人将此应用于 FLATSOME 主题? I tried few changes in the functions.php file but no results, please help!
我在functions.php文件中尝试了一些更改但没有结果,请帮忙! thank you
谢谢你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.