pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

將 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)
主站蜘蛛池模板: 上海网站建设-上海网站制作-上海网站设计-上海做网站公司-咏熠软件 | 网站优化公司_北京网站优化_抖音短视频代运营_抖音关键词seo优化排名-通则达网络 | 电地暖-电采暖-发热膜-石墨烯电热膜品牌加盟-暖季地暖厂家 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 能耗监测系统-节能监测系统-能源管理系统-三水智能化 | 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 油液红外光谱仪-油液监测系统-燃油嗅探仪-上海冉超光电科技有限公司 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 木材烘干机,木炭烘干机,纸管/佛香烘干设备-河南蓝天机械制造有限公司 | 运动木地板厂家_体育木地板安装_篮球木地板选购_实木运动地板价格 | 温州中研白癜风专科_温州治疗白癜风_温州治疗白癜风医院哪家好_温州哪里治疗白癜风 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 正压密封性测试仪-静态发色仪-导丝头柔软性测试仪-济南恒品机电技术有限公司 | 广东机电安装工程_中央空调工程_东莞装饰装修-广东粤标建设有限公司 | sus630/303cu不锈钢棒,440C/430F/17-4ph不锈钢研磨棒-江苏德镍金属科技有限公司 | 扒渣机厂家_扒渣机价格_矿用扒渣机_铣挖机_撬毛台车_襄阳永力通扒渣机公司 | 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 日本东丽膜_反渗透膜_RO膜价格_超滤膜_纳滤膜-北京东丽阳光官网 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 山东齐鲁漆业有限公司【官网】-工业漆专业生产厂家 | 天津力值检测-天津管道检测-天津天诚工程检测技术有限公司 | 桥架-槽式电缆桥架-镀锌桥架-托盘式桥架 - 上海亮族电缆桥架制造有限公司 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 齿轮减速马达一体式_蜗轮蜗杆减速机配电机-德国BOSERL齿轮减速电动机生产厂家 | 大米加工设备|大米加工机械|碾米成套设备|大米加工成套设备-河南成立粮油机械有限公司 | 档案密集架_电动密集架_移动密集架_辽宁档案密集架-盛隆柜业厂家现货批发销售价格公道 | 代办建筑资质升级-建筑资质延期就找上海国信启航 | 远程会诊系统-手术示教系统【林之硕】医院远程医疗平台 | 广州迈驰新GMP兽药包装机首页_药品包装机_中药散剂包装机 | 智慧食堂_食堂管理系统_食堂订餐_食堂消费系统—客易捷 | EPDM密封胶条-EPDM密封垫片-EPDM生产厂家 | 活性氧化铝球|氧化铝干燥剂|分子筛干燥剂|氢氧化铝粉-淄博同心材料有限公司 | 定量包装秤,吨袋包装称,伸缩溜管,全自动包装秤,码垛机器人,无锡市邦尧机械工程有限公司 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 企典软件一站式企业管理平台,可私有、本地化部署!在线CRM客户关系管理系统|移动办公OA管理系统|HR人事管理系统|人力 | 中式装修设计_全屋定制家具_实木仿古门窗花格厂家-喜迎门 | 硬齿面减速机_厂家-山东安吉富传动设备股份有限公司 | 电子海图系统-电梯检验系统-智慧供热系统开发-商品房预售资金监管系统 |