1

As it is not possible to set a default price to products I was trying to achieve that by using rules. I'm checking "Before saving a commerce product" Event. Condition is "Data value is empty" and my Action would be "Set a data value", but all I get is an error because price isn't set. Anyone know why this is happening?

As this is the first time I use rules, maybe I've done something wrong.

  • I still have no clue. I'm searching now for the place, where the price-validation is done, and to set a default price here if no price is set, but I can't find it. – Andi Aug 6 '14 at 7:34
  • Price is a required field, so Drupal checks a value has been filled in as part of form validation (not code specific to commerce). The rule will only take effect on form submit, so it is too late. See also similar question drupal.stackexchange.com/questions/102659/…. If you don't mind writing a hook, try the answer below. – AdamS Sep 20 '15 at 16:31
1
/**
 * Implements hook_field_widget_form_alter()
 */
function HOOK_field_widget_form_alter(&$element, &$form_state, $context) {
  if (isset($element['#field_name'])) {
    if ($element['#field_name'] == 'commerce_price') {
      if (empty($element['amount']['#default_value'])) {
        $element['amount']['#default_value'] = 0;
      }
    }
  }
}
| improve this answer | |
  • Added code with explanation is highly appreciable :) – DEVARAJ JOHNSON Feb 17 '15 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.