Removing SKU on product edit page

Posts: 3
Joined: 12/11/2007

Hi,

I have looked the support documentation and I am trying to find out how to remove the SKU field from the product edit page. I don't want my user to have to enter an SKU.

I have edited the tables, looked through payment settings and can't seem to find it.

Thanks

Posts: 925
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

admin/store/settings/products/edit/fields

Uncheck "Model"

This should probably be changed to "SKU" to keep the terms consistent across pages.

--

<tr>.

Posts: 5269
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Hmm.. for the product edit page, there actually isn't an option for this... but you can do a few tricks. You can use JS to put in some placeholder text (maybe just a space?) and CSS to hide that field based on its ID. You could also make a small module to make the field not required or remove it from the form altogether using Drupal's hook_form_alter().

Posts: 925
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

Ryan reads more closely than I do! I missed the fact that you said "edit page" and assumed you meant "view page". I'm glad he was able to give you a responsive answer.

--

<tr>.

Posts: 3
Joined: 12/11/2007

Thanks for the response. Where and how would I add the module? I am new to ubercart and am not familiar with it yet.

Posts: 925
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

The solution is really Drupal-specific, and doesn't have anything to do with Ubercart, but let me show you how this works as my pennance for failing to properly read the question Smiling It never hurts to have too much documentation!

In uc_product.module, you will find the function uc_product_form(). This function builds the form that is used to enter information about a product. In particular, this code:

  $form['base']['model'] = array('#type' => 'textfield',
    '#title' => t('SKU'),
    '#required' => TRUE,
    '#default_value' => $node->model,
    '#description' => t('Product SKU/model'),
    '#weight' => 0,
    '#size' => 32,
  );

creates the SKU field on the form and marks it as "required" - meaning the user must enter a value or automatic validation won't allow the form to be submitted.

Now, you could just go ahead and change that "TRUE" to be "FALSE" and your problem would be solved. (Don't do this!) Solved until you had to upgrade Ubercart. Then this core module code would be overwritten, and of course you probably forgot all the "simple" changes you made, and you didn't write them down anywhere, so not only would your store break but it would take you a long time to identify all the problems and re-implement all those "solutions" you put in before.

No, the proper way is NOT to change the core modules, but to OVERRIDE the functions to perform the way you want them to.

In this case, what you're trying to do is to alter the product form. Luckily, Drupal gives you a way to alter any form using the hook mechanism. In particular, hook_form_alter(). The way it works is this: Whenever a form is displayed, Drupal "asks" if any module wants to alter it. So if you write a small module, your module will have the ability to make changes to the form before it is presented to the user.

Here is the minimal module you will need: Create a directory on your web server called "custom" under site/all/modules and in it create two files: my_module.info and my_module.module

my_module.info looks like this:

; $Id$
name = My Module
description = My customizations to Ubercart
package = Custom

my_module.module looks like this:

<?php
// $Id$

/**
* @file
* Module to hold my customizations to Ubercart
*/

/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter($form_id, &$form) {
  if ($form_id == 'product_node_form') {
    $form['base']['model']['#required'] = FALSE;
  }
}

This function is called by Drupal every time a form is built, so we first test to see if it's the form we're interested in then change the value we're interested in. We could also add elements to the form at this point, if that's something you need.

Now use your browser to go to the admin/build/modules menu for your site and enable your module. That's all - the SKU field should no longer be "required" when you create products. You can use this mechanism to perform all sorts of different customizations to Ubercart without ever having to modify the distributed code.

--

<tr>.

Posts: 3
Joined: 12/11/2007

Thank you for the thorough explanation. You guys are very helpful.

Posts: 45
Joined: 12/24/2007

How can i in this way hide such fields as weight and geometry sizes?

Posts: 5
Joined: 02/15/2008

Hi, i am also new to Drupal and Ubercart.
This is how i implemented the hook_form_alter to remove those 2 fields on my site.
I don't know if it's the best practice or not but it works.

<?php
function moduleName_form_alter($form_id, &$form){

// remove weight field(s)
   
$form['base']['weight'] = array(
//        '#weight' => 15,
//        '#theme' => 'uc_product_form_weight',
   
);

// remove geometrical dimension fields
   
$form['base']['dimensions'] = array('#type' => 'fieldset',
//        '#title' => t('Dimensions'),
//        '#description' => t('Physical dimensions of the packaged product.'),
//        '#weight' => 20,
//        '#theme' => 'uc_product_dimensions_form',
   
);
}
?>

If there is a better way to do this please let us know.

Many thanks to the Ubercart team for such a wonderful ecommerce solution.

Posts: 45
Joined: 12/24/2007

Thanks to all.

Posts: 1
Joined: 03/11/2008

You can find some more good examples for modifying your forms at: http://www.molecularsciences.org/drupal/modifying_your_forms

Posts: 16
Joined: 06/09/2008

TR, I'm glad you misread edit/view... you answered my question!

thanks,
amy

Posts: 1
Joined: 08/07/2008

great tip!

Posts: 107
Joined: 05/28/2008

Ryan recently showed me http://drupal.org/project/formfilter which allows complete customization of many forms on your site. It's great for hiding things like dimensions, weight, etc (Fields that we do not use).

Also, webmasterkai has contributed a module that auto-fills the SKU based on selectable tokens (and gives the option to hide the sku field altogether). http://www.ubercart.org/contrib/4307

Posts: 20
Joined: 09/23/2008

Hi All, I wanted to disable the SKU on my install too becuase I hope to convert my install into a catalogue. I don't need to sell things. Anyway, I took the code and updated the .info file to match the requirements for 6.4 on drupal. THat worked. Now I can't find the Forum id the product_node_form doesn't work anymore but I don't know why.
I got this: It is prudent to test for your form_id. This would serve to eliminate a number of difficult to debug errors. If you do not the name of your form, view the form source in the browser. It should look something like:

<input type="hidden" name="edit[form_id]" id="edit-mypage-node-form" value="mypage_node_form" />

from molecular sciences.org but it doesn't work. this is what the browser shows for the input type on the sku field:
<div class="form-item" id="edit-model-wrapper">
<label for="edit-model">SKU: <span class="form-required" title="This field is required.">*</span></label>
<input type="text" maxlength="128" name="model" id="edit-model" size="32" value="" class="form-text required" />
<div class="description">Product SKU/model.</div>
</div>

So, I am a little confused as to how to determine form id.

Can someone clarify?

Thanks,
raspberryheaven

Posts: 2244
Joined: 08/07/2007
AdministratoreLiTe!

There will be a hidden <input> element that actually has "form_id" in its name and id attribute. Get the value attribute of that element.

Posts: 20
Joined: 09/23/2008

Ohhhh ok. Thanks. That makes more sense.
Do I look at the source of the edit page or the source of the view page (the one that people see when they're just browsing and not editing?)

raspberryheaven

Posts: 20
Joined: 09/23/2008

Ok. I found it. It is indeed product_node_form so I have

<?php
// $Id$

/**
* @file
* Module to hold my customizations to Ubercart
*/

/**
* Implementation of hook_form_alter()
*/
function skudisable_module_form_alter($form_id, &$form) {
  if ($form_id == 'product_node_form') {
    $form['base']['model']['#required'] = FALSE;
  }
}

But when I put the module in my custom folder and then click on admin or any button to go and enable the module, I get a white screen of death. (I'm using a local install of drupal to work on this called Bitnami.) Anyway.....Not really sure what's up now.
The form_id value was found way down on the page after most of the input items like the SKU Field. Is that important? I thought it would have been before but then, I 'm really new to drupal module programing and php.

raspberryheaven

Posts: 20
Joined: 09/23/2008

I just got a book called Drupal 6 Modules and I was wondering, is the above code for disabling an sku out of date for drupal 6? maybe that is my problem. It's going to take me a while to figure out what I need to change but I can see this:
$forum_id and $form_values are replaced by $form and &$form_state. I tried replacing $form_id with these new values but neither worked...

Posts: 1293
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

The Form API has changed, if you download an install the Coder module (http://drupal.org/project/coder) you can process certain modules with it, and it will analyze it for coding standards as well as D5 -> D6 changes.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 20
Joined: 09/23/2008

Thanks! I'll try it out. I'm really new to module writing and have sketchy php skills so hopefully this process won't be too hard/frustrating.

Posts: 20
Joined: 09/23/2008

Hi there,
I've been trying to debug this sku disable type module on my local harddrive using Coder, the module recommended to me in a comment on this thread. But when I put my custom module in the folder, which btw, looks like the code that 's listed near the top of this thread, I get a white screen so I can't go back to the panel that show's coder's options. also for some reason, it lists my module twice........ as
skudisable (active)
skudisable_module (active)

I deleted the module from my directory and I cleared my cache but nothing seems to fix this error.
can anyone help me? I feel like this shouldn't be as complicated as it's seeming to be.

Becky

Posts: 20
Joined: 09/23/2008

Ok. So I took torgosPizza's advice and ran coder and now i have no error messages when I run it on my module here. (I'm using drupal 6 btw) BUT! when I enabled it I got an error message relating to line 13 which is the if ($form_id =='product_node_forum'){ part.

Here is the full code:

<?php
// $Id$

/**
* @file
* Module to hold my customizations to Ubercart
*/

/**
* Implementation of hook_form_alter()
*/
function skudisable_module_form_alter(&$form, &$form_state, $form_id ) {
  if ($form_id == 'product_node_form') {
    $form['base']['model']['#required'] = FALSE;

  }
}

I am not strong at php yet. Could someone help me figure out why this won't work?

Posts: 20
Joined: 09/23/2008

OMG I just figured it out. The reason it didn't work was because there were gremlins (hidden characters) on the page screwing it up. GRRR.

raspberryheaven

Posts: 1293
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

Weird,

Glad you got it figured out. I just realized you were trying to remove the SKU - there is a contrib that already does this, however it doesn't remove other fields. It can automatically generate the SKU and hide the field in the edit form. http://www.ubercart.org/contrib/4307

Other fields might need to be custom, though. Perhaps a patch to the Auto SKU contrib.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 20
Joined: 09/23/2008

Thanks, it's cool. I realized today after talking to the person I'm making this for, that I need the SKU field but that i should make it un-required. So success! I have a new question relating to the issue of hiding fields and such but I posted it at a different thread because it doesn't relate directly to sku fields: http://www.ubercart.org/forum/support/6888/can_i_extend_fieldset_go_arou...

Posts: 61
Joined: 03/19/2008

goodeit wrote:
Ryan recently showed me http://drupal.org/project/formfilter which allows complete customization of many forms on your site. It's great for hiding things like dimensions, weight, etc (Fields that we do not use).

Also, webmasterkai has contributed a module that auto-fills the SKU based on selectable tokens (and gives the option to hide the sku field altogether). http://www.ubercart.org/contrib/4307

Form filter does not seem to be able to hide fields that are 'required'. I tried hiding the SKU field, but it still showed up even in the 'preview filtered form' view. It worked for fields that are not required.

As for autosku, it does hide the field in the product edit page, but it needs to generate an sku at the same time. I had [nid] as the token pattern for this and for some reason, the nid always got printed on the product's view page, although the SKU field itself was hidden on the edit page. I had to reluctantly disable the module.

Update: Okay, got it just now. You have to turn off 'Model' in the product settings display (http://domain.com/admin/store/settings/products/edit/fields) for the nid generated by AutoSKU not to show in the product view page.