禁用特定WooCommerce产品的添加到购物车按钮

问题描述 投票:1回答:1

我正在尝试禁用添加到购物车的某些产品,这些产品在产品编辑器上勾选了“订购”复选框(请参阅下面的代码)。

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消息“对不起,此产品不能是购买“。出现。应该发生的是,当点击“添加到购物车”按钮时,产品会自动添加到购物车。

此外,从单个产品页面,我可以毫无问题地添加可购买的购物车。

我不确定为什么会这样。有任何想法吗?

php wordpress woocommerce product custom-fields
1个回答
1
投票

我已经测试了你的代码,它没有问题......我没有你描述的有问题的行为......所以别的东西在制造麻烦:

您首先需要进行数据库备份...然后您应该尝试:

  1. 检查在您的其他自定义项中是否存在禁用Ajax添加到购物车并显示该消息的内容。尝试评论您的其他自定义以找到有罪的。
  2. 尝试禁用与Woocommerce相关的所有第三方插件(Woocommerce除外)。如果问题消失了,请逐一重新启用它们以查找有罪。

这个问题也可能来自这个主题。


Woocommerce 3 and introduced 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文件中。它可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.