My SKU naming system for products uses integers like 10001, 10002, etc. Next product added should just get the next available number.

Therefore I would like automatic generation of SKU so that the SKU for the next product is +1 higher than the last product added.
(Similar to AUTO_INCREMENT column in a MySQL table)

Example:

  • Highest SKU so far is 10213
  • Next product added will get SKU 10214

This seems like a basic feature?

I noticed the Unique SKU contib module which may or may not do what I want. But it is apparently for Ubercart 1 only.
http://www.ubercart.org/contrib/4312

Is there a way to achieve this with Product Power Tools?

Note: I am aware that Product Power Tools can autogenerate SKU based on node id. But since I have other node types than products, this is not quite what I want.

Comments

willvincent’s picture

Status: Active » Postponed

I don't believe this can be done presently, but I've added it to the list.

nodiac’s picture

+1 for Auto Increment! Yes! Please!

Every Ubercart site I've built has had sequential skus as a requirement.

design.er’s picture

Hello, nearly 1 year has passed since this feature request. Is it already possible?

Edit: Oh nice, it's already possible. I used the drupal.org search and thought I'm in Übercart's issue queue - didn't realize that this is an other module. It's pretty handy. Thanks a lot! :)

Stomper’s picture

Could you explain how you did it/basics of setting up a custom auto SKU, please?

nodiac’s picture

Yes! Please!

Stomper’s picture

Agreed, directions please.

TR’s picture

Version: 6.x-1.10 » 7.x-1.x-dev
Status: Postponed » Active

New features will go into the Drupal 7 version first. A patch to implement this feature would be highly appreciated.

jonathan_hunt’s picture

Here's how I solved it in D6, via a hook_form_alter():

    // Show next available SKU. Make it insertable by click.
    case 'product_node_form':
      $sku = db_result(db_query("SELECT MAX(u.model) FROM {node} n LEFT JOIN uc_products u ON n.vid=u.vid"));
      drupal_add_js('$(document).ready(function() { $(".sku-add").click(function() {var sku = $(".sku-add").text(); $("#edit-model").val(sku); }); })', 'inline');
      $form['base']['model']['#description'] .= ' '. t('Next available is <a class="sku-add">@sku</a>.', array('@sku' => $sku));
      break;
Stomper’s picture

Where do we insert this code?

jonathan_hunt’s picture

@Stomper That would go in a hook_form_alter() in a module you create.

Stomper’s picture

So there's no way to generate an auto SKU without a custom module?

JordiTR’s picture

Issue summary: View changes

If you create a module that's the proper way to add #8 in Drupal 7:

<?php
function yourmodulename_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'product_node_form':
      // Show next available SKU. Make it insertable by click.
      $sku = db_query("SELECT MAX(u.model) FROM {node} n LEFT JOIN uc_products u ON n.vid = u.vid")->fetchField();
      drupal_add_js('
        (function ($) {
        Drupal.behaviors.optionsMultiLevel = {
          attach: function (context) {
            $("a.sku-add").click(function() {
              var sku = $(this).text();
              $("#edit-model").val(sku);
            });
          }
        };
        })(jQuery);
      ', 'inline');
      $form['base']['model']['#description'] .= '. '. t('Next available is <a class="sku-add" style="cursor: pointer;">@sku</a>.', array('@sku' => $sku));
      break;
  } 
}
?>

Thanks Jonathan Hunt!

iantresman’s picture

I would guess this could be done in conjunction with the CCK Serial Field module which "provides an auto-increment (serial) field", which in turn can be used as a CCK file tokens in the Pattern for the SKU.

caco13’s picture

@iantresman, could you explain how to do that?