久久久久久久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)
主站蜘蛛池模板: 久久一区二区精品 | 久久久久国产精品午夜一区 | 欧美激情欧美激情在线五月 | 日日操日日干 | 国产在线拍偷自揄拍视频 | 91成人在线| 国产欧美精品区一区二区三区 | 69av在线视频 | 亚洲成av | 日韩欧美在线一区 | 6996成人影院网在线播放 | 欧美日韩一本 | 国产黄色大片在线免费观看 | 天天操天天干天天透 | 成人性视频在线播放 | 精品视频久久久久久 | 九九热这里只有精品6 | 欧美中文字幕在线 | 日本成人中文字幕在线观看 | 欧美一级二级在线观看 | 午夜在线影院 | 国产观看 | 黄色毛片免费看 | 久久久免费 | 欧美成人免费电影 | 国产极品车模吞精高潮呻吟 | 九九爱这里只有精品 | 亚洲精品日韩一区二区电影 | 精品久久久久久久久久久下田 | 在线观看的av | av影片在线 | 亚洲成人毛片 | 草比av| 四虎首页| 99久久亚洲| 国产人成在线观看 | 亚洲天堂二区 | 中文字幕在线一区二区三区 | 黄色毛片在线观看 | 国产精品视频网 | 中文字幕一级 |