更改 WooCommerce 中特定产品类别的存档页面上的产品 link

Change product link on archives pages for a specific product category in WooCommerce

我想使用特定产品类别的 woocommerce 网络挂钩更改商店页面中的产品 link。

我知道如何使用 woocommerce_before_shop_loop_item 挂钩删除产品 link,但我想更改商店页面中特定类别的产品 link。 感谢任何帮助。

已更新: 以下功能将替换产品 link 并通过商店和档案页面上的自定义产品添加到购物车 link产品类别。

您需要在此代码中为每个定义替换自定义 link 和产品类别 2 次:

add_action( 'woocommerce_before_shop_loop_item', 'customizing_loop_product_link_open', 9 );
function customizing_loop_product_link_open() {
    global $product;

    // HERE BELOW, replace clothing' with your product category (can be an ID, a slug or a name)
    if( has_term( array('clothing'), 'product_cat' )){
        remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
        add_action( 'woocommerce_before_shop_loop_item', 'custom_link_for_product_category', 10 );
    }
}

function custom_link_for_product_category() {
    global $product;

    // HERE BELOW, Define the Link to be replaced
    $link = $product->get_permalink();

    echo '<a href="' . $link . '" class="woocommerce-LoopProduct-link">';
}

// Changing the link on the button add to cart in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_loop_add_to_cart_button_link', 10, 2 );
function change_loop_add_to_cart_button_link( $button, $product  ) {

    // HERE BELOW, replace 'clothing' with your product category (can be an ID, a slug or a name)
    if( has_term( array('clothing'), 'product_cat', $product->get_id() ) ){
        // HERE BELOW, Define the Link to be replaced
        $link = $product->get_permalink();
        $button_text = __( "View product", "woocommerce" );
        $button = '<a class="button add_to_cart_button" href="' .  . '">' . $button_text . '</a>';
    }
    return $button;
}

代码进入活动子主题(或活动主题)的 function.php 文件。

已测试并有效