Php 将产品列表中的产品页面链接替换为自定义链接(woocommerce)

Php 将产品列表中的产品页面链接替换为自定义链接(woocommerce),php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我只有两种产品。我想在产品列表页面上提供每个产品的自定义链接 我正在使用下面的代码 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())

我只有两种产品。我想在产品列表页面上提供每个产品的自定义链接

我正在使用下面的代码

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;
add_action('woocommerce_后面的_shop_loop_item','add_a_custom_button',5);
函数添加自定义按钮(){
全球$产品;
如果(is_product()==3557 | | is_shop()| | is_product_category()| | is_product_tag()):
?>

我修改了您在
woocommerce\u-after\u-shop\u-loop\u-item
操作挂钩中使用的代码。您可以通过
$product->get\u-id()
获取prodcut 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;
}
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;
}