将 Woocommerce 单个产品页面上的添加到购物车按钮替换为产品类别

时间:2023-01-16
本文介绍了将 Woocommerce 单个产品页面上的添加到购物车按钮替换为产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试添加一个指向联系页面的自定义链接按钮 - 在第一个 if 条件下显示联系我们"文本和按钮上的自定义 URL,而不是添加到购物篮"按钮.

I'm trying to add a custom link button that leads to Contact page - within first if condition that displays "Contact us" text with custom URL on the button instead of "Add to Basket" button.

怎么做?到目前为止,这是我的代码.它显示属于类别64"的每个产品的自定义按钮文本.这正是我想要的.但是如何添加该按钮将功能从购物车按钮更改为自定义链接按钮?我想如果必须更改此购物车按钮功能.如何?

How to do that? This is my code so far. It shows custom button text for each product which is part of category "64". That's exactly what I want. But how to add that button changes function from cart button to custom link button? Im figuring If have to change this cart buttons function. How?

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,以下代码会将添加到购物车按钮替换为单个产品页面中的自定义按钮以及链接到该产品的按钮档案页面:

For your product category ID 64, the following code will replace add to cart button by a custom button in single product pages and by a linked button to the product on archives pages:

// 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 文件中.

经过测试并有效.

这篇关于将 Woocommerce 单个产品页面上的添加到购物车按钮替换为产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:WooCommerce单品添加图片文件上传字段 下一篇:在 Woocommerce 中显示基于客户送货区域的自定义消息

相关文章

最新文章