久久久久久久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)
主站蜘蛛池模板: 色婷婷综合久久久中字幕精品久久 | 日韩有码在线观看 | www.一区二区三区 | 亚洲精品久久久一区二区三区 | 国产午夜精品一区二区 | 成人性视频免费网站 | 欧美伦理一区 | 久久午夜精品福利一区二区 | 91久久久精品国产一区二区蜜臀 | 国产一级视频在线观看 | 亚洲人成在线播放 | 国产日韩欧美一区二区 | 国产专区在线 | 99视频在线免费观看 | 亚洲人成人一区二区在线观看 | 日韩国产精品一区二区三区 | 一级aaaa毛片| 日韩欧美在 | 亚洲精品18| 国产一区二区视频免费在线观看 | 国产欧美一区二区精品忘忧草 | 欧美电影网 | 人成在线视频 | 精品久久久久久久久久久久久久 | 伊人二区 | 99爱视频| 国产精品久久 | 国产特级毛片 | 免费国产一区二区 | 精品一区二区久久久久久久网站 | 日韩精品无码一区二区三区 | 久久久久久艹 | 九九精品在线 | 亚洲成人在线免费 | 精品视频一区二区三区在线观看 | 亚洲一区二区三区视频在线 | 久久综合亚洲 | 亚洲国产精品成人无久久精品 | 婷婷色国产偷v国产偷v小说 | 国产一区二区影院 | 欧美亚洲国产一区二区三区 |