Vote up!
Vote down!

Add two fields together to create new quantity and check for minimum

I am needing to add two fields together and make that the new quantity value before it is added to the cart. I also need to do a validation on the calculated value to make sure that it matches or surpasses the minimum quantity field that I have added to the product.

For example:

I created a custom line item with two fields that the shopper needs to fill out, for example total boys and total girls. I then added a minimum quantity field to the product itself. What I need to do is to add the boys and girls value together and then verify that that number is equal to or above the minimum quantity then put that value into the quantity field for the line item otherwise display a message that the data entered into the boys and girls fields are too low.

I can get this to semi work by applying a rule after the item has been added to the cart and then removing it however I would prefer that this all be done before it is even added to the cart.

Any suggestions would be most appreciated as there are little or no examples on using rules before a item is added to a cart but a few that deal with doing it after a item has been added to a cart and then manipulating the data.

Thanks in advance.

Asked by: criley
on April 26, 2013

1 Answer

Vote up!
Vote down!

Perhaps it can be done with only Rules, but you might want to consider a code approach which could be more straightforward.

For example:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'commerce_cart_add_to_cart_form') === 0) {
    // add custom validation
Answer by: Favio Manriquez
Posted: Apr 29, 2013


Thank you for the suggestion I ended up adding a new validation routine via a form modification. If I get the time to make it more generic maybe I will role it into a module but for now the project that I need this for can move forward.

Thanks again.

- criley on April 30, 2013