请尝试以下操作,将添加到购物车按钮替换为特定产品类别的自定义链接按钮:
// Replacing the loop add to cart button by a custom button for specific product category
add_filter( 'woocommerce_loop_add_to_cart_link', 'filter_loop_add_to_cart_link_callback', 10, 2 );
function filter_loop_add_to_cart_link_callback( $button, $product ) {
$terms = array('evoheat-heat-pumps'); // Here your product categories ( Ids, slugs or names )
if( has_term( $terms, 'product_cat', $product->get_id() ) ){
$button = contact_us_replacement_button();
}
return $button;
}
// Replacing the single product add to cart button for a specific product category
add_action( 'woocommerce_single_product_summary', 'action_single_product_summary_callback', 1 );
function action_single_product_summary_callback() {
global $product;
$terms = array('evoheat-heat-pumps'); // Here your product categories ( Ids, slugs or names )
if( has_term( $terms, 'product_cat', $product->get_id() ) ){
// For variable product types
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'contact_us_replacement_button', 20 );
}
// For all other product types
else {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'contact_us_replacement_button', 30 );
}
}
}
// The button "Contact us" replacement button function
function contact_us_replacement_button(){
$href = home_url('/contact-us/'); // The link
$text = __( "Contact us", "woocommerce" );
// Output button
echo '<a class="button" href="'.$href.'">' . $text . '</a>';
}
代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。