How to hide/disable the add to cart button based on product type

How to hide/disable the add to cart button based on product type

If you』re looking for a way to restrict your wholesale user from purchasing products with a specific product type, for example, you want to prevent them from purchasing simple products, variable products or even product bundles, you can do that by using a custom snippet.
The snippet below is an example to hide the add to cart button on the product and product catalog page. It checks if the logged in user is a wholesale customer based on the wholesale role key and if it is a simple product. Then it hides/disables the add to cart button.
 
add_action('wp','wwp_remove_add_to_cart_on_product_type', 99);

function wwp_remove_add_to_cart_on_product_type() {
global $current_user;
$product = wc_get_product();

if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) {
$wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
$wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole();

//Checks if the logged in user is wholesale_customer
if (!empty($wwp_wholesale_role) && in_array('wholesale_customer', $wwp_wholesale_role)) {

// Change the product type, in this example it is 'simple' for simple products
if ( $product instanceof WC_Product && $product->is_type( 'simple' ) ) {
// This filter will disable the add to cart button
add_filter( 'woocommerce_is_purchasable', '__return_false');
}
}
}
}

 
If you wish to hide these products from your WooCommerce Wholesale Order Form, you』ll need to do an additional step to hide them. You can go to  WooCommerce > Settings > Wholesale Ordering > Filters, and add the products in the Exclude Product Filter.
 
Exclude Product Filter, WooCommerce Wholesale Order Form

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注