Home / Disable / Make Product Non Purchasable for Wholesale Customer if Product Stock is Less Than Certain Amount
Duplicate Snippet

Embed Snippet on Your Site

Make Product Non Purchasable for Wholesale Customer if Product Stock is Less Than Certain Amount

This snippet will allow you to make a product unavailable for purchase by wholesale customers if the stock falls below a specific amount. Simply adjust the value "$product_quantity <= 50" to the desired quantity that meets your requirements

Code Preview
php
<?php
/* Make Product Non Purchasable for Wholesale Customer Iif Product Stock is Less Than X Amount */
add_filter( 'woocommerce_is_purchasable', 'wwpp_limit_purchase_based_on_stock', 10, 2 );
function wwpp_limit_purchase_based_on_stock( $is_purchasable, $product ) {
	
	//Get Product Stock Quantity 
	$product_quantity = $product->get_stock_quantity();
	
	//Get current user's wholesale role
	global $wc_wholesale_prices_premium;
    $user_wholesale_role = $wc_wholesale_prices_premium->wwpp_wholesale_roles->getUserWholesaleRole();
	
	// Check if the current user is a wholesale roles and the product stock quantity is less than 50. Change the number as needed
    if( !empty( $user_wholesale_role ) && $product_quantity <= 50 )  {
		$is_purchasable = false;
	}
	return $is_purchasable;
		
}

Comments

Add a Comment