簡體   English   中英

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

[英]Disabling Add to Cart Button for Specific WooCommerce Products

我正在嘗試禁止將某些產品添加到購物車,這些產品在產品編輯器上勾選了“訂購單”復選框(見下面的代碼)。

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' );
    }
}

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

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

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

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

我已經測試了你的代碼並且它沒有問題地工作......我沒有你描述的有問題的行為......所以其他事情正在制造麻煩

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

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

問題也可能來自主題。


現在自從Woocommerce 3 並引入了 CRUD Objects ,您的代碼有點過時了。

這是重新訪問和增強的代碼版本(適用於 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 文件中。 它可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM