久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 Woocommerce 單個產品頁面上的添加到購物車按鈕

Replace Add to cart button on Woocommerce Single Product Pages for a product category(將 Woocommerce 單個產品頁面上的添加到購物車按鈕替換為產品類別)
本文介紹了將 Woocommerce 單個產品頁面上的添加到購物車按鈕替換為產品類別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試添加一個指向聯系頁面的自定義鏈接按鈕 - 在第一個 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 單個產品頁面上的添加到購物車按鈕替換為產品類別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 性色av一区 | 国产精品日本一区二区不卡视频 | 中文字幕久久精品 | 欧美精品一区在线 | 视频一区二区三区四区五区 | 欧美成人a∨高清免费观看 91伊人 | 在线视频 欧美日韩 | 国产日韩欧美一区二区 | 精精久久| 亚洲国产欧美日韩 | 国产精品精品久久久 | 久久国际精品 | 精品国产精品一区二区夜夜嗨 | 欧美精品一区二区三区在线播放 | 欧美色综合网 | 国产成人av在线播放 | 欧美日韩国产精品一区 | 另类视频在线 | www久久国产 | 国产高清在线观看 | 一区二区三区中文字幕 | 中文字幕在线第一页 | 国产成人精品一区二区三区在线 | 日韩视频精品在线 | 一区二区视频 | 久久综合狠狠综合久久 | 欧美精品一二三 | 免费爱爱视频 | 欧美性猛交一区二区三区精品 | 久久久美女 | 天天操操| 97精品国产手机 | 国产精品久久久一区二区三区 | 伊人久久综合 | 国产一二区在线 | 日韩中文字幕 | 久久久久久久国产精品影院 | 日韩在线电影 | 国产91在线播放精品91 | 精品美女久久久 | 国产精品久久久久久久久久久久午夜片 |