0

I'd like to know if there's a way to assign a default value in the Price field in Drupal Commerce. Currently, there are no field settings available.

1

It is not possible for the default price field, because Drupal locks down the field / widget settings for "locked" fields. This is a general Drupal issue, but you could work around it by using an alternate price field that you add custom to your product type paired with a product pricing rule that swaps that price in at the start of the pricing process. You'd have to do a form alter or something to hide the default price field from your product forms.

2
  • Thanks for the reply. So I can't unlock it? It seems that it's much more complicated that I expected it to be. I'm curious, do you know why this locking is done in the first place?
    – ashash
    Feb 8 '14 at 15:55
  • We had to lock it to prevent its deletion since so much of Drupal Commerce depends on that field being there. Unfortunately, Drupal 7 doesn't differentiate between a field that can't be deleted and a field that can't have its settings edited. Hopefully that's resolved in Drupal 8; I filed an issue for it but can't remember where it ended up. Feb 13 '14 at 15:51
0

You can temporarily unlock the field, set the default price and lock it again. The PHP code is as follows:

$field = field_read_field('commerce_price');
$field['locked'] = 0;  // 0: unlock; 1: lock.
field_update_field($field);

You could run it using drush eval (use backslash to escape the $ and put it all on one line) or the php block from the devel module. Credit: http://atlantic-canary.net/en/how-to-lockunlock-a-field-in-drupal.

Don't forget to lock it again after to make the field safe again.

0

Instill devel module Enable the devel module either by drush en or from admin/build/modules. It will generate several blocks and look for the one named ‘Execute PHP’ enable it by setting it to a preferred region . Write the code you want to check on the text area for example and then press execute to see the result.

so here we will use this code

$field_name = 'field_name'; $field = field_read_field($field_name); $field['locked'] = 0; // 0: unlock; 1: lock. field_update_field($field);

note: this line $field_name = 'field_name'; you have to add your filed name for example like this $field_name = commerce_price;

if you got any error such as displayed as messages “Notice: Undefined index” or “Notice: Undefined variable” and your logs will grow insanely…

the solution is to add in your settings.php the line

ini_set('error_reporting', 'E_ALL ^ E_NOTICE');

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.