Removing SKU on product edit page

Posts: 4
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: 1312
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: 6997
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: 1312
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: 4
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: 1312
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: 4
Joined: 12/11/2007

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

Posts: 46
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: 46
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: 116
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: 2848
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: 1730
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.

--

Wanna help send me to DrupalCon Paris? Or do you just like my work? Donate via PayPal!

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: 1730
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.

--

Wanna help send me to DrupalCon Paris? Or do you just like my work? Donate via PayPal!

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: 78
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.

Posts: 151
Joined: 01/20/2009

Hi

I followed these instructions, and have:

optional_sku.info

; $Id$
name = Optional SKU
description = Makes the SKU for a product optional
package = Custom

and

optional_sku.module

<?php
// $Id$

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

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

I've enabled the module, but the SKU still shows up as required. I have a string override that changes 'SKU' to 'Product Code'.

What have I done wrong?

Thanks

Glenn

UPDATE:

It works when I create a product, but when I create another content type which is also a product, it doesn't work.

Glenn

Posts: 81
Joined: 02/21/2009

Can anyone tell me where I can edit the "SKU" field's label in the edit form and in the output?

Thanks

Posts: 9
Joined: 02/22/2009

here is how I hide the sku input field:

uc_nosku.info

; $Id$
name = "No sku"
description = "Hides the sku input field on the edit product form and allocates its default value with 'default'."
dependencies[] = uc_product
package = "Ubercart (Optional)"
core = 6.x
php = 5.0

uc_nosku.module

<?php
// $Id$

/**
* @file
* Hides the sku input field on the edit product form
* and allocates its default value with 'default'.
*/

/**
* Implementation of hook_form_alter().
*/
function uc_nosku_form_alter(&$form, &$form_state, $form_id) {
  if (
$form_id == "product_node_form") {
     
$form['base']['model']['#default_value'] = 'default';
     
$form['base']['model']['#type'] = 'hidden';
  }
}
?>

Remember that there mustn't be a '?>' tag at the end of the .module file!

Create a directory ../sites/all/modules/ubercart/uc_nosku and save the files there.

@loocher
To change the label of the sku-field just add this line to the module above:

$form['base']['model']['#title'] = 'Just another label';
Posts: 10
Joined: 02/24/2009

I am using Drupal 6.9. I got version incompatible with version 6.9 Drupal core error at /admin/build/modules.

Can anyone point me to any documentation on using hook_form_alter in D6?

Thanks in advance.

Posts: 153
Joined: 05/15/2008
Getting busy with the Ubercode.

http://api.drupal.org/api/function/hook_form_alter/6

Incidentally, I'm working on a module that will handle autofilling of SKUs based on tokens, among a number of other things..

--

Follow me on twitter.

Posts: 10
Joined: 02/24/2009

Hi. Thanks for quick reply.
I am still getting the error when try to enable the module. This is what I have:

my_module.info :

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

my_module.module :

<?php
// $Id$

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

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

Both files are place in sites/all/modules/custom/
Can you see what wrong with the codes? I am running D6.9, Ubercart-6.x.2.0-beta3

Thanks.

Posts: 153
Joined: 05/15/2008
Getting busy with the Ubercode.

you need a line that says core="6.x" in the info file for drupal 6 modules..

you will also need to give the SKU a default value, as it's a required field. Though you could probably set the #required attribute to FALSE.

my_module.info:

; $Id$
name = My Module
description = My customizations to Ubercart
package = Custom
core = "6.x"
dependencies[] = uc_cart

my_module.module:

<?php
// $Id$

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

/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'product_node_form') {
    $form['base']['model']['#type'] = 'hidden';
    $form['base']['model']['#default_value'] = 'default value';
    // Choose which method you want to use, the above line, or comment it out and uncomment the following line:
    // $form['base']['model']['#required'] = FALSE;
  }
}

--

Follow me on twitter.

Posts: 10
Joined: 02/24/2009

Thanks a lot, tcindie@drupal.org

It works now after follow your suggestion above!

btw. I have another custom field (namely name-text which I like to limit the length to 20 chars), it is in the add-to-cart form at product page, how do I find out the form id and input text field? Where to put the <? //php print_r($node); ?> or

<?php
print($form_id);
?>

in order to find the id and field.

Thanks for your help.

Edit:

I found the answer from this mini lesson on hook_form_alter, see http://groups.drupal.org/node/4308

Thanks

Posts: 10
Joined: 02/24/2009

Sorry, trying to find out the product textfield attribute that I should use in order to change the size for the last few days but of no luck. I have found out the form id after print out in my_module.module file. The form is 'uc_product_add_to_cart_form_11', the node is node/11. This attribute is added from store/attributes, same as Size, Color(in Store Administration). My code is as follow:

<?php
// $Id$

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

/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter(&$form, $form_state, $form_id) {

if ($form_id == "uc_product_add_to_cart_form_11") {

print $form_id;

/*
$form['#parameters'][2]['attributes'][6]['#size'] = 20;
*/

$form['form_info'] = array('#value' => print_r($form, TRUE));

}

/* etc ... */

}

When browse to node/11, the form id will be printed. I have commented the $form['#parameters'][2]['attributes'][6]['#size'] = 20 as it will give blank screen. The form array printed is as attached.

As you can see attributes[6] which is a name text field, I would like to limit the number of characters to 20. Currently it has maxlength=128, size=60.

I have search through the forum on hook_form_alter but there is no example on changing the custom product attributes that is added from store admin. Any help on this.

Thanks.

AttachmentSize
form-array-output.txt2.74 KB
Posts: 10
Joined: 02/24/2009

Any suggestion or pointer on how to change the size for this text field using hook_form_alter?

Thanks in advance. I am currently on D6.9.

Posts: 2
Joined: 04/14/2009

I'm curious about this... why not just hide the SKU field and add a new field called ISBN?

--

Thanks in advance

Drupal 6.x user