Plugin Author
myCred
(@designbymerovingi)
Hi.
That depends on which shopping cart you are using and how / where you want to show this error message.
If you are using WooCommerce, you can select to insert the total cart amount in points in the cart page or the checkout page (or both).
Hi Gabriel,
I am using woocommerce with mycred and i am using mycred as the only payment gateway, what i wanted is, for example customer has 10 points but the product values is 30, so the customer should not be able to add this product into the cart, when he tries to add this product into the cart a error message should be displayed in shop itself saying not enough points instead of taking the customer to checkout page.
Hi Gabriel,
Could you please help me out on this..
Plugin Author
myCred
(@designbymerovingi)
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.
Hi Gabriel,
Thank you so much for the reply, i’ll use the above code you have given.
Plugin Author
myCred
(@designbymerovingi)
Let me know if you require any further assistance.