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.– AndiCommented Aug 6, 2014 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.– AdamSCommented Sep 20, 2015 at 16:31
Add a comment
|
1 Answer
/**
* 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;
}
}
}
}
-
Added code with explanation is highly appreciable :) Commented Feb 17, 2015 at 6:01