[英]Replace Product page link with custom link in PRODUCT LISTING (woocommerce)
我只有2个产品。 我想在产品列表页面上提供每个产品的自定义链接。
我正在使用下面的代码...
add_action('woocommerce_after_shop_loop_item', 'add_a_custom_button', 5 );
function add_a_custom_button() {
global $product;
if (is_product() == 3557 || is_shop() || is_product_category() || is_product_tag()):
?>
<div style="margin-bottom:10px;">
<a class="button custom-button" href="https://example.com/redirect/3557/">Buy @ Store1</a>
</div>
<?php
endif;
if (is_product() == 3541 || is_shop() || is_product_category() || is_product_tag()):
?>
<div style="margin-bottom:10px;">
<a class="button custom-button" href="https://example.com/redirect/3541/">Buy @ Store2</a>
</div>
<?php
endif;
上面代码的问题是它显示了每个产品的 html 按钮。
我修改了您在woocommerce_after_shop_loop_item
操作挂钩中使用的代码。 您可以通过$product->get_id()
获取产品 ID。
add_action( 'woocommerce_after_shop_loop_item', 'add_a_custom_button', 5 );
function add_a_custom_button() {
global $product;
if ( $product->get_id() == 3557 && ( is_shop() || is_product_category() || is_product_tag() ) ): ?>
<div style="margin-bottom:10px;">
<a class="button custom-button" href="https://example.com/redirect/3557/">Buy @ Store1</a>
</div>
<?php
endif;
if ( $product->get_id() == 3541 && ( is_shop() || is_product_category() || is_product_tag() ) ): ?>
<div style="margin-bottom:10px;">
<a class="button custom-button" href="https://example.com/redirect/3541/">Buy @ Store2</a>
</div>
<?php
endif;
}
此外,如果您想更改循环产品默认链接,您可以使用woocommerce_loop_product_link
过滤器挂钩。
add_filter( 'woocommerce_loop_product_link', 'change_product_permalink_based_on_product', 99, 2 );
function change_product_permalink_based_on_product( $link, $product ) {
if ( $product->get_id() === 3557 ){
$link = 'https://example.com/redirect/3557/';
}
if ( $product->get_id() === 3541 ){
$link = 'https://example.com/redirect/3541/';
}
return $link;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.