禁用特定WooCommerce产品的“添加到购物车”按钮 [英] Disabling Add to Cart Button for Specific WooCommerce Products

查看:102
本文介绍了禁用特定WooCommerce产品的“添加到购物车”按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图禁止在产品编辑器中将某些选中了订购单复选框(请参见下面的代码)的产品添加到购物车。

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除外)。如果问题消失了,请一个个地重新启用它们以找到罪恶感。

问题也可能来自主题。

The problem could come from the theme too.

现在,因为 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产品的“添加到购物车”按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆