更新:添加了 WC 3+ 兼容性
对于您的情况,您需要自定义 3 个相关的自定义挂钩函数:
### Custom Product link ###
// Removing the default hooked function
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
// Add back the hook to a custom function
add_action ( 'woocommerce_before_shop_loop_item', 'custom_loop_product_link', 10 );
function custom_loop_product_link() {
$custom_link = get_permalink( 16 ); // link to a custom page
echo '<a href="' . $custom_link . '" class="woocommerce-LoopProduct-link">';
}
### Custom add-to-cart link ###
add_filter( 'woocommerce_loop_add_to_cart_link', 'customizing_add_to_cart_button', 10, 2 );
function customizing_add_to_cart_button( $link, $product ){
// CUSTOM ADD TO CART text and link
$add_to_cart_url = site_url('/custom_link/');
$button_text = __('View now', 'woocommerce');
// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
$product_type = method_exists( $product, 'get_type' ) ? $product->get_type() : $product->product_type;
$link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
esc_url( $add_to_cart_url ),
esc_attr( $product_id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product_type ),
esc_html( $button_text )
);
return $link;
}
### Custom link Redirect after add-to-cart ###
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect', 10, 1 );
function my_custom_add_to_cart_redirect( $url ) {
$url = get_permalink( 16 ); // an example: here redirect is on page ID 16
return $url;
}
该代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。