問題描述
我正在嘗試禁止將某些產品在產品編輯器上勾選了訂購單"復選框(請參閱下面的代碼).
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:
- 檢查您的其他自定義設置中是否存在禁用 Ajax 添加到購物車并顯示該消息的內容.嘗試評論您的其他自定義設置以找出有問題的自定義設置.
- 嘗試禁用所有與 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模板網!