Advertisement
designbymerovingi

#00000488

Sep 2nd, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. /**Mycred restrict purchase if points are not enough**/
  2. add_filter( 'woocommerce_is_purchasable', 'mycred_deny_add_to_cart_if_insolvent', 10, 2 );
  3. function mycred_deny_add_to_cart_if_insolvent( $purchasable, $product ) {
  4. // Make sure myCRED is enabled and the product is purchasable
  5. if ( ! function_exists( 'mycred' ) || ! $purchasable ) return $purchasable;
  6.  
  7. // Only applicable for product with the ID 123
  8. if ( $product->id != 123 ) return $purchasable;
  9.  
  10. // Current Users ID
  11. $user_id = get_current_user_id();
  12.  
  13. // Load myCRED
  14. $mycred = mycred();
  15.  
  16. // Exempt Administrators from this rule
  17. if ( $mycred->can_edit_creds( $user_id ) ) return $purchasable;
  18.  
  19. // Get price
  20. $price = $product->get_price();
  21.  
  22. // Get users balance
  23. $balance = $mycred->get_users_balance( $user_id );
  24.  
  25. // If balance is lower return false.
  26. if ( $balance < $price )
  27. return false;
  28.  
  29. return $purchasable;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement