【问题标题】:Add to Cart button replacement for specific category in WooCommerce? [closed]WooCommerce 中特定类别的添加到购物车按钮替换? [关闭]
【发布时间】:2021-02-03 08:43:38
【问题描述】:

我尝试使用 Change the add to cart text for a specific product category in Woocommerce 3 答案代码(第三个函数) 更改 Woocommerce 网站上特定类别的“添加到购物车”按钮的文本和链接。

我刚刚对代码进行了此更改(针对所需的产品类别)

if( has_term( array('evoheat-heat-pumps'), 'product_cat' ) )

但我需要的是显示自定义文本“联系人”,并在单击此按钮时重定向到特定 url,如 https://www.example.com/contact-us/

但到目前为止,我只能更改文本。

如何替换 WooCommerce 中特定产品类别的“添加到购物车”按钮?

【问题讨论】:

    标签: php wordpress button woocommerce product


    【解决方案1】:

    请尝试以下操作,将添加到购物车按钮替换为特定产品类别的自定义链接按钮:

    // 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 文件中。经过测试并且可以工作。

    【讨论】:

    • 感谢您的回复,但不太好用。这已将按钮更改为应有的联系人,但按下时,它仍会添加到购物车,然后转换为样式略有不同的联系人按钮,第二次按下时会将您带到联系人 URL。
    • @seangolding1987 抱歉,这是不可能的……您只是忘记删除一些其他自定义代码,或者有一个插件出现问题(甚至可能是您的主题自定义)。替换按钮是一个简单的按钮,它不能向购物车添加任何东西,按下它会转到“联系我们”……对于样式,您可以添加任何相关的类或/和添加一些自定义 CSS 规则。这是经过测试的,适用于店面主题下的最后一个 woocommerce 版本。
    • 嗨@LoicTheAztec,非常感谢您的帮助。这是导致问题的缓存问题。删除了缓存,现在效果很好!再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 2017-10-10
    • 2019-12-13
    • 1970-01-01
    • 2016-07-18
    • 2018-02-10
    • 1970-01-01
    • 2018-10-30
    相关资源
    最近更新 更多