![](/img/trans.png)
[英]Disable add to cart button on Woocommerce product category archive pages
[英]Replace Add to cart button on Woocommerce Single Product Pages for a product category
我正在尝试添加一个指向联系页面的自定义链接按钮 - 在第一个if条件下,在按钮上显示带有自定义 URL 的“联系我们”文本而不是“添加到购物篮”按钮。
怎么做? 到目前为止,这是我的代码。 它显示属于类别“64”的每个产品的自定义按钮文本。 这正是我想要的。 但是如何添加该按钮将功能从购物车按钮更改为自定义链接按钮? 我在想是否必须更改此购物车按钮功能。 如何?
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text() {
global $product;
$cat_id = 64;
$product->get_category_ids();
if ( in_array( $cat_id, $product->get_category_ids() ) ) {
return __( 'Contact us', 'woocommerce' );
}
else {
return __( 'Add to Basket', 'woocommerce' );
}
}
对于您的产品类别 ID 64,以下代码将用单个产品页面中的自定义按钮和存档页面上产品的链接按钮替换添加到购物车按钮:
// The custom replacement button function
function custom_product_button(){
// HERE your custom button text and link
$button_text = __( "Custom text", "woocommerce" );
$button_link = '#';
// Display button
echo '<a class="button" href="'.$button_link.'">' . $button_text . '</a>';
}
// Replacing the single product button add to cart by a custom button for a specific product category
add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
function replace_single_add_to_cart_button() {
global $product;
// Only for product category ID 64
if( has_term( '64', 'product_cat', $product->get_id() ) ){
// For variable product types (keeping attribute select fields)
if( $product->is_type( 'variable' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
add_action( 'woocommerce_single_variation', 'custom_product_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', 'custom_product_button', 30 );
}
}
}
// Replacing the button add to cart by a link to the product in Shop and archives pages for as specific product category
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Only for product category ID 64
if( has_term( '64', 'product_cat', $product->get_id() ) ){
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
代码进入活动子主题(或主题)的 function.php 文件。
测试和工作。
在钩子中添加过滤器优先级
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text',50);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.