Unique SKU

Contrib type: 
Module
Status: 
Initial upload
Latest version: 
0.2

Downloads

Compatibility: 
Ubercart 1.x

Allows you to enforce Product SKUs to be unique either among the current product type/class or among all products. Returns a form validation error in case of a duplicate SKU.

Simply go to the Content type or Product class settings form for each type you want to activate this for.

I took a lot of inspiration from webmasterkai's Automatic SKU module, but i'm not sure if the two will work together or not as it depends which fires first.

This is the first attempt at this and works well for me with light testing.

Comments and suggestions welcome!

Related threads: 
PreviewAttachmentSize
uc_unique_sku.tar.gz1.75 KB
fourcws's picture
Offline
Cool profile pic award.
Joined: 04/16/2008
Juice: 131
Re: Unique SKU

Looks like i found a bug or an incompatability with my server settings.

warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home1/*/public_html/sites/testing.site.com/modules/uc_unique_sku/uc_unique_sku.module on line 79.
Drupal 5.7
Configuration file Protected
Cron maintenance tasks Last run 42 min 14 sec ago
You can run cron manually.
Database schema Up to date
File system Writable (public download method)
GD library bundled (2.0.34 compatible)
MySQL database 5.0.45
PHP 5.2.5
PHP register globals Disabled
Unicode library PHP Mbstring Extension
Web server Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8g DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Sensitivity-Filter/1.0.0
torgosPizza's picture
Offline
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.
Joined: 08/14/2007
Juice: 4111
Re: Re: Unique SKU

Invalid mysql resource doesn't mean server settings, it probably means there's a variable or object not getting passed to the query function correctly.

--
Help directly fund development: Donate via PayPal!

radiantflow's picture
Offline
Joined: 04/11/2008
Juice: 43
updated

I've just uploaded a new version, which should hopefully fix that issue you were having.

Would you please try again to see if it works for you?

fourcws's picture
Offline
Cool profile pic award.
Joined: 04/16/2008
Juice: 131
fixed

That did the trick. Thanks.

pcambra's picture
Offline
Bug Finder
Joined: 01/23/2008
Juice: 251
Hi Great module, did the

Hi

Great module, did the trick for me Smiling

I've added this piece of code for suiting to my needs, so when a new product is added, the sku field is "autocompleted" whit the greatest one recorded + 1, but you can alter it if you want to use one different.

In uc_unique_sku_form_alter hook, I've added this:

  if ($form_id == 'product_node_form' AND arg(1) == 'add') {
    $sql = "SELECT greatest(max(CAST(model AS SIGNED)),max(CAST(sku AS SIGNED))) +1
FROM uc_products u
JOIN uc_product_stock us ON (u.model = us.sku)";
    $next_sku = db_result(db_query($sql));
    $form['base']['model']['#default_value'] = $next_sku;
  }

Probably this code is not product classes aware

echo's picture
Offline
Joined: 08/05/2009
Juice: 31
Re: Unique SKU

Any work on this for Ubercart 2?

Minde_'s picture
Offline
Joined: 09/29/2008
Juice: 36
Re: Re: Unique SKU

Yes, I would also love to see this for Ubercart 2.

yanku's picture
Offline
Joined: 08/25/2009
Juice: 55
Re: Re: Unique SKU

Indeed! Looking forward to a version for Ubercart 2 / Drupal 6

cjdavis@drupal.org's picture
Offline
Joined: 02/09/2009
Juice: 2
Unique SKU for Ubercart 2

I just did a quick-n-dirty port to Ubercart 2. This has NO awareness of product classes, and you cannot configure anything.

There is one major difference in functionality - I check against CURRENT revisions of products, not ALL revisions. So you are able to reuse a SKU that was previously used in a different product node. This may or may not be your desired functionality.

One thing I found in testing however, you can revert a node to a previous revision that has a duplicate SKU - validate does not appear to be called in this case.

<?php
function custom_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if(
$op == 'validate') {
 
   
//
    // prevent products from being saved with a duplicate SKU
    //
   
   
if (uc_product_is_product($node)) {
     
     
// get all products that have an identical SKU in their CURRENT revision
     
$query = "SELECT p.nid FROM {uc_products} AS p, {node} AS n ";
     
$query .= "WHERE p.vid = n.vid AND model = '%s' ";
     
$result = db_query($query,$node->model);
     
     
// load any nids returned into array
     
$nids = array();
      while (
$obj = db_fetch_object($result)) {
       
$nids[] = $obj->nid;
      }
     
     
// remove matches of this node - for the case where we are saving
      // a new revision of this same node
     
if ($node->nid && is_array($nids) && in_array($node->nid, $nids)) {
       
$key = array_search($node->nid, $nids);
        unset(
$nids[$key]);
      }
 
     
// any nids left? they are duplicates.
     
if (is_array($nids) && count($nids)) {
        
form_set_error($node->model, t('The Product Code No. field requires a unique value, and @value is already used.', array('@value' => $node->model)) );
      }
    }
  }
}
?>
koyama's picture
Offline
Joined: 08/12/2009
Juice: 36
Re: Unique SKU for Ubercart 2

Sorry, but where do I find your port to Drupal 6 / Ubercart 2 ?

koyama's picture
Offline
Joined: 08/12/2009
Juice: 36
Re: Hi Great module, did the

@pcambra: Thanks for your code for autogenerating SKU. However, in some cases it was suggesting too a low SKU. After some tests, I found that it was the JOIN causing products to be overlooked whenever they didn't have a stock entry. Changing to LEFT JOIN fixed the problem:

  if ($form_id == 'product_node_form' AND arg(1) == 'add') {
    $sql = "SELECT greatest(max(CAST(model AS SIGNED)),max(CAST(sku AS SIGNED))) +1
FROM uc_products u
LEFT JOIN uc_product_stock us ON (u.model = us.sku)";
    $next_sku = db_result(db_query($sql));
    $form['base']['model']['#default_value'] = $next_sku;
  }
mbohner's picture
Offline
Joined: 05/14/2009
Juice: 9
Unique SKU for D6 Ubercart 2.*

Would you have a link for the Unique SKU for D6 Ubercart 2.* Version?

Thanks.
MB

koyama's picture
Offline
Joined: 08/12/2009
Juice: 36
D6 / Ubercart 2.*

@mbohner: Here is what I am using for D6. Basically, it is the same as the original uc_unique_sku.tar.gz with uc_unique_sku_form_alter slightly modified.
The attachment also contains code by pcambra so that sku field is "autocompleted" with the greatest one recorded + 1.

AttachmentSize
uc_unique_sku-6.x-1.x-dev.tar.gz 1.93 KB
mbohner's picture
Offline
Joined: 05/14/2009
Juice: 9
Works

Thanks mate.

jahaswel's picture
Offline
Joined: 07/21/2009
Juice: 80
repost?

I'm getting an invalid archive error with that file. Can you test it out and repost?

thanks!
James

timani's picture
Offline
Joined: 06/22/2009
Juice: 7
Working version on Drupal 6

The module worked 98% for me. I initially got an error on the product class edit page /admin/store/products/classes/e-books for me:

warning: function_exists() expects parameter 1 to be string in /home/..../form.inc

I had to make a couple of changes but I managed to update a couple of lines and got it working on Ubercart 2.x