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

禁用特定 WooCommerce 產品的添加到購物車按鈕

Disabling Add to Cart Button for Specific WooCommerce Products(禁用特定 WooCommerce 產品的添加到購物車按鈕)
本文介紹了禁用特定 WooCommerce 產品的添加到購物車按鈕的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試禁止將某些產品在產品編輯器上勾選了訂購單"復選框(請參閱下面的代碼).

I'm trying to disable adding to cart certain products which have the "Call to Order" checkbox ticked (see code below) on the product editor.

add_action( 'woocommerce_product_options_general_product_data', 'custom_general_product_data_custom_fields' );
/**
 * Add `Call to Order` field in the Product data's General tab.
 */
function custom_general_product_data_custom_fields() {
    // Checkbox.
    woocommerce_wp_checkbox(
        array(
            'id'            => '_not_ready_to_sell',
            'wrapper_class' => 'show_if_simple',
            'label'         => __( 'Call to Order', 'woocommerce' ),
            'description'   => __( '', 'woocommerce' )
            )
    );
}

add_action( 'woocommerce_process_product_meta', 'custom_save_general_proddata_custom_fields' );
/**
 * Save the data values from the custom fields.
 * @param  int $post_id ID of the current product.
 */
function custom_save_general_proddata_custom_fields( $post_id ) {
    // Checkbox.
    $woocommerce_checkbox = isset( $_POST['_not_ready_to_sell'] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_not_ready_to_sell', $woocommerce_checkbox );
}

add_filter( 'woocommerce_is_purchasable', 'custom_woocommerce_set_purchasable', 10, 2);
/**
 * Mark "Not ready to sell" products as not purchasable.
 */
function custom_woocommerce_set_purchasable() {
    $not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell' , true);

    return ( 'yes' == $not_ready_to_sell ? false : true );

}

add_filter( 'woocommerce_product_add_to_cart_text', 'custom_product_add_to_cart_text' );
/**
 * Change "Read More" button text for non-purchasable products.
 */
function custom_product_add_to_cart_text() {
    $not_ready_to_sell = get_post_meta( get_the_ID(), '_not_ready_to_sell', true );

    if ( 'yes' === $not_ready_to_sell ) {
        return __( 'Call to Order', 'woocommerce' );
    } else {
        return __( 'Add to Cart', 'woocommerce' );
    }
}

勾選了復選框的產品實際上是不可購買的,這是預期的結果.

The products that have the checkbox ticked, are in fact not purchasable, which is the desired outcome.

我遇到的問題是,當我在產品目錄頁面上點擊添加到購物車"的可購買產品(那些沒有勾選復選框的產品)時,我被重定向到產品頁面和默認的 WooCommerce 消息對不起,這個產品無法購買."出現.應該發生的是,當單擊添加到購物車"按鈕時,產品會自動添加到購物車中.

The problem I'm having is when I click "Add to Cart" for purchasable products (those without the checkbox ticked) on the product catalog page, I am redirected to the product page and a default WooCommerce message "Sorry, this product cannot be purchased." appears. What should be happening is that when the "Add to Cart" button is clicked, the product is automatically added to the cart.

同樣從單個產品頁面,我可以毫無問題地添加可購買的購物車.

Also from the single product page, I can add the purchasable cart without a problem.

我不確定為什么會這樣.有任何想法嗎?

I am not sure why this is happening this way. Any ideas?

推薦答案

我已經測試了你的代碼,它運行沒有問題......我沒有你描述的有問題的行為......所以其他事情正在制造麻煩強>:

I have tested your code and it work without problems… I don't have the problematic behavior you describe… So something else is making trouble:

您首先需要進行數據庫備份...然后您應該嘗試:

You will need first to make a database backup… Then you should try to:

  1. 檢查您的其他自定義設置中是否存在禁用 Ajax 添加到購物車并顯示該消息的內容.嘗試評論您的其他自定義設置以找出有問題的自定義設置.
  2. 嘗試禁用所有與 Woocommerce 相關的第三方插件(Woocommerce 除外).如果問題解決了,再讓他們一個一個地重新啟用以找到有罪的.

問題也可能來自主題.

現在自從 Woocommerce 3 并引入了 CRUD 對象,你的代碼有點過時了.

Now since Woocommerce 3 and introduced CRUD Objects, your code is a bit outdated.

這是重新訪問和增強的代碼版本(適用于 Woocommerce 3+):

Here is revisited and enhanced code version (for Woocommerce 3+):

// Add a custom field in the Product data's General tab (for simple products).
add_action( 'woocommerce_product_options_general_product_data', 'add_general_product_data_custom_field' );
function add_general_product_data_custom_field() {
    woocommerce_wp_checkbox( array( // Checkbox.
        'id'            => '_not_ready_to_sell',
        'label'         => __( 'Call to Order', 'woocommerce' ),
        'wrapper_class' => 'show_if_simple',
    ) );
}

// Save custom field value
add_action( 'woocommerce_admin_process_product_object', 'save_general_product_data_custom_field', 10, 1 );
function save_general_product_data_custom_field( $product ) {
    $product->update_meta_data( '_not_ready_to_sell', isset( $_POST['_not_ready_to_sell'] ) ? 'yes' : 'no' );
}

// Make not purchasable, products with '_not_ready_to_sell' meta data set to "yes" (for simple products)
add_filter( 'woocommerce_is_purchasable', 'filter_woocommerce_set_purchasable', 10, 2);
function filter_woocommerce_set_purchasable( $purchasable, $product ) {
    return 'yes' === $product->get_meta( '_not_ready_to_sell' ) && $product->is_type('simple') ? false : $purchasable;

}

// Change button text to "Call to Order" for simple products not purchasable.
add_filter( 'woocommerce_product_add_to_cart_text', 'filter_product_add_to_cart_text', 10, 2 );
function filter_product_add_to_cart_text( $button_text, $product ) {
    if ( 'yes' === $product->get_meta( '_not_ready_to_sell' ) && $product->is_type('simple') ) {
        $button_text =  __( 'Call to Order', 'woocommerce' );
    }
    return $button_text;
}

代碼位于活動子主題(或活動主題)的 function.php 文件中.它可以工作.

Code goes on function.php file of your active child theme (or active theme). It could works.

這篇關于禁用特定 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)
主站蜘蛛池模板: 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 上海洗地机-洗地机厂家-全自动洗地机-手推式洗地机-上海滢皓洗地机 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 仿清水混凝土_清水混凝土装修_施工_修饰_保护剂_修补_清水混凝土修复-德州忠岭建筑装饰工程 | 高压负荷开关-苏州雷尔沃电器有限公司 | 建筑资质代办-建筑企业资质代办机构-建筑资质代办公司 | 宝宝药浴-产后药浴-药浴加盟-艾裕-专注母婴调养泡浴 | 电表箱-浙江迈峰电力设备有限公司-电表箱专业制造商 | 礼堂椅厂家|佛山市艺典家具有限公司| 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 液压压力机,液压折弯机,液压剪板机,模锻液压机-鲁南新力机床有限公司 | 手持式浮游菌采样器-全排二级生物安全柜-浙江孚夏医疗科技有限公司 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | LOGO设计_品牌设计_VI设计 - 特创易 | 济南电缆桥架|山东桥架-济南航丰实业有限公司 | 合肥宠物店装修_合肥宠物美容院装修_合肥宠物医院设计装修公司-安徽盛世和居装饰 | 钢化玻璃膜|手机钢化膜|钢化膜厂家|手机保护膜-【东莞市大象电子科技有限公司】 | 河南中专学校|职高|技校招生-河南中职中专网 | 工业插头-工业插头插座【厂家】-温州罗曼电气 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 缝纫客| 江苏远邦专注皮带秤,高精度皮带秤,电子皮带秤研发生产 | 纯水电导率测定仪-万用气体检测仪-低钠测定仪-米沃奇科技(北京)有限公司www.milwaukeeinst.cn 锂辉石检测仪器,水泥成分快速分析仪-湘潭宇科分析仪器有限公司 手术室净化装修-手术室净化工程公司-华锐手术室净化厂家 | 山东PE给水管厂家,山东双壁波纹管,山东钢带增强波纹管,山东PE穿线管,山东PE农田灌溉管,山东MPP电力保护套管-山东德诺塑业有限公司 | 定量包装机,颗粒定量包装机,粉剂定量包装机,背封颗粒包装机,定量灌装机-上海铸衡电子科技有限公司 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 国际学校_国际学校哪个好_国际课程学校-国际学校择校网 | 直流大电流电源,燃料电池检漏设备-上海政飞 | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 编织人生 - 权威手工编织网站,编织爱好者学习毛衣编织的门户网站,织毛衣就上编织人生网-编织人生 | 深圳市宏康仪器科技有限公司-模拟高空低压试验箱-高温防爆试验箱-温控短路试验箱【官网】 | 杭州门窗厂家_阳光房_包阳台安装电话-杭州窗猫铝合金门窗 | 深圳法律咨询【24小时在线】深圳律师咨询免费 | 自动化展_机器人展_机床展_工业互联网展_广东佛山工博会 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 老房子翻新装修,旧房墙面翻新,房屋防水补漏,厨房卫生间改造,室内装潢装修公司 - 一修房屋快修官网 |