Search code examples
drupaldrupal-modulesdrupal-8drupal-commercedrupal-rules

Drupal 8: Only add to cart if user has enough credits


I've a site where transactions are based purely on credits. No actual money is exchanged. In such a case, I want to restrict users from adding products to cart if they do not have credits to do so.

In steps of how this should be accomplished, here is the sequence of events which I think should work :

Whenever somebody tries to add a product :

  • Use Rule "Before adding product to cart"
  • I need to get the value of the following :
    • Get user credits
    • Get sum of current product + cart value
  • Check for condition if credits is greater than product + cart.
  • If true, add product to cart else display "Not enough credits".

I use the loyalty points module for the user credits.

How can I set up the rules for this scenario?


Solution

  • Instead of using a module I just used a custom_validation function on commerce_add_to_card_form as suggested by lamp5