Hi.
Sorry for the delay in my reply.
There are a few ways to do this. One example is to check if the user can afford a product as it is added to the cart. If they cant afford it, we tell WooCommerce not to add the product to the cart and instead trigger the “Sorry, "Product Name" cannot be purchased.” message.
Example:
add_filter( 'woocommerce_is_purchasable', 'mycred_deny_add_to_cart_if_insolvent', 10, 2 );
function mycred_deny_add_to_cart_if_insolvent( $purchasable, $product ) {
// Make sure myCRED is enabled and the product is purchasable
if ( ! function_exists( 'mycred' ) || ! $purchasable ) return $purchasable;
// Current Users ID
$user_id = get_current_user_id();
// Load myCRED
$mycred = mycred();
// Exempt Administrators from this rule
if ( $mycred->can_edit_creds( $user_id ) ) return $purchasable;
// Get price
$price = $product->get_price();
// Get users balance
$balance = $mycred->get_users_balance( $user_id );
// If balance is lower return false.
if ( $balance < $price )
return false;
return $purchasable;
}
Another approach would be to adjust the WooCommerce template files via your theme and wrap the “Add to Cart” button around a similar check and only showing the button if the user can afford the product.
One thing to keep in mind is that the above code will not check the users balance against the users total cart cost. It will only compare their balance against each products price.