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

禁用特定 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)
主站蜘蛛池模板: 伊人久久麻豆 | 欧美精品一区二区三区四区 | 国产免费福利在线 | 国产精品久久久久免费 | 51ⅴ精品国产91久久久久久 | 国产精品综合一区二区 | 91精品国产一区二区三区 | 一区二区不卡高清 | 国产精品呻吟久久av凹凸 | 日韩视频一区 | 九九热免费在线观看 | 精品国产一区二区三区在线观看 | 国产精品一区二区视频 | 狠狠久 | 91xxx在线观看 | 69电影网 | 成人免费淫片aa视频免费 | 嫩草视频入口 | 亚洲 欧美 在线 一区 | 精品国产一区二区三区久久久四川 | 亚洲精品一区二三区不卡 | 福利网址| 黄色片免费看 | 麻豆一区二区三区 | 黄色在线免费观看视频 | 一区二区三区久久 | 99在线观看| 免费同性女女aaa免费网站 | 中文字幕一区二区三区精彩视频 | 国产精品美女一区二区 | 视频在线观看亚洲 | 国产丝袜人妖cd露出 | 久久激情视频 | 亚洲国产精品久久久久婷婷老年 | 精品自拍视频在线观看 | 久久av一区二区 | 天天躁日日躁狠狠的躁天龙影院 | 久久免费国产 | 99爱免费 | 在线国产一区二区 | 91视视频在线观看入口直接观看 |