通过 WooCommerce 产品设置中的自定义复选框禁用添加到购物车按钮

问题描述

我们希望防止将某些即将推出的产品添加到购物车。

我们希望有一个复选框来选择我们要防止添加到购物车的特定产品。我们现在有复选框和保存代码

我也发现了这个:Remove add cart button in Woocommerce for a specific product categoryhttps://wisdmlabs.com/blog/the-right-way-to-hide-add-to-cart-button-in-woocommerce/

我不确定,防止特定产品添加到购物车的最佳方法是什么。 有没有人建议最好的方法是什么?

// Add new checkBox to product edit page (General tab)
add_action( 'woocommerce_product_options_general_product_data','upcoming_checkBox_to_products' );        
  
function upcoming_checkBox_to_products() {           
woocommerce_wp_checkBox( array( 
'id' => 'custom_upcoming','class' => '','label' => 'Prevent add to cart'
) 
);      
}
  
// -----------------------------------------
// Save checkBox via custom field
  
add_action( 'save_post','save_upcoming_checkBox_to_post_Meta' );
  
function save_upcoming_checkBox_to_post_Meta( $product_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['custom_upcoming'] ) ) {
            update_post_Meta( $product_id,'custom_upcoming',$_POST['custom_upcoming'] );
    } else delete_post_Meta( $product_id,'custom_upcoming' );
}

// -----------------------------------------
// Prevent add to cart

解决方法

  • 通过代码中添加的注释标签进行说明

要将复选框添加到库存产品选项,请使用:

// Add checkbox
function action_woocommerce_product_options_inventory_product_data() {
    // Checkbox
    woocommerce_wp_checkbox( array( 
        'id'             => '_prevent_add_to_cart_button',// Required,it's the meta_key for storing the value (is checked or not)
        'label'          => __( 'My label','woocommerce' ),// Text in the editor label
        'desc_tip'       => false,// true or false,show description directly or as tooltip
        'description'    => __( 'Prevent add to cart','woocommerce' ) // Provide something useful here
    ) );
}
add_action( 'woocommerce_product_options_inventory_product_data','action_woocommerce_product_options_inventory_product_data',10,0 );
        
// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
    // Isset,yes or no
    $checkbox = isset( $_POST['_prevent_add_to_cart_button'] ) ? 'yes' : 'no';

    // Update meta
    $product->update_meta_data( '_prevent_add_to_cart_button',$checkbox );
}
add_action( 'woocommerce_admin_process_product_object','action_woocommerce_admin_process_product_object',1 );

要禁用简单和可变产品的添加到购物车按钮,请使用:

// Is_purchasable (simple)
function filter_woocommerce_is_purchasable( $purchasable,$product ) {
    // Get meta
    $hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
    
    // Compare
    if ( $hide_add_to_cart_button == 'yes' ) {
        $purchasable = false;
    }

    return $purchasable;
}
add_filter( 'woocommerce_is_purchasable','filter_woocommerce_is_purchasable',2 );

// Is_purchasable (variable)
function filter_woocommerce_variation_is_purchasable( $purchasable,$product ) {
    $hide_add_to_cart_button = get_post_meta( $product->get_parent_id(),'_prevent_add_to_cart_button',true );

    // Compare
    if ( $hide_add_to_cart_button == 'yes' ) {
        $purchasable = false;
    }

    return $purchasable;
}
add_filter( 'woocommerce_variation_is_purchasable','filter_woocommerce_variation_is_purchasable',2 );

注意:有多种方法可以禁用/删除添加到购物车按钮,因此这取决于您是要完全隐藏还是禁用该按钮。