Is it possible to run the SKU Auto Generate script on existing products? I found this module after setting up my catalog. What are ways for me to have a previous SKU re-generated now?

Thanks for any help you can provide.

*Update*

I see the corresponding code to my request in the .module file, starting on line 72. How can I change it to overwrite existing auto-skus. I guess I already ran the auto sku on everything and then changed the pattern, now need to re-run it.

    // Handle SKU field.
    switch ($class_settings['asku']) {
        case 1:
          //Do not overwrite SKU for existing products
          $form['base']['model']['#value']= isset($form['nid']['#value'])?$form['base']['model']['#default_value']:'';
          $form['base']['model']['#type']= 'hidden';
          break;
        case 2:
         // new node?
         if (!isset($form['nid']['#value'])) {
           $form['base']['model']['#value']= t('auto SKU');
           $form['base']['model']['#description'].= ' ' . t('(leave as "%auto" for auto generated)', array('%auto' => t('auto SKU')));
         }
         break;
       case 3:
         //Do not overwrite SKU for existing products
         $form['base']['model']['#value']= isset($form['nid']['#value'])?$form['base']['model']['#default_value']:t('auto SKU');
         $form['base']['model']['#disabled']= 'true';
         $form['base']['model']['#description'].= ' ' . t('(auto generated)');
         break;
    }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

scotwith1t’s picture

This part of the form is just on the admin screen where you select which method to use, from what I can tell. I too would like to see a way to overwrite the existing sku on node save using the pattern i set up. This way I can use existing VBO "Node Save" functionality to update old skus that I set up using an incorrect token instead of having to turn off power tools and manually update each one. Any help would be appreciated! Even if it's a hack to the existing code, I'd do it, run my updates and then undo the hack...thanks!

CraigBertrand’s picture

I just turned an existing content type with about 4,000 nodes into an ubercart class and would love for the auto sku to work on these. I would also be okay with a hack and back approach.

matthand’s picture

@CraigBertrand

I think the solution for this lies with VBO (Views Bulk Operations) and executing a Custom PHP Script.

@scot.self

The portion of the code above tells you the 3 possible switches that may trigger an auto SKU creation. My problem is that it only works when creating a new node, and not when updating an existing one. The module seems to ignore all existing products.

I think your VBO idea is a good one. I already executed a few different custom PHP scripts using VBO but couldn't get the auto-sku to trigger. I tried inserting the text 'auto SKU' into the model field, as the module code seemed to suggest, but that did not work. I think I still need to go to the edit form after inserting the 'auto SKU' text into the product model field and I'm not sure how to code that directly into the PHP script in VBO.

Here's the code that I executed as a custom PHP script using VBO that needs more work:

$object->model = 'auto SKU';
node_save($object);

Any ideas on how to make it happen? Maybe if the PHP script calls the auto sku function outright?

Here's the part of the code that actually sets the auto sku, maybe we can execute some of this in VBO:

  if ($op == 'insert' || $op == 'update') {
    $class_settings = db_fetch_array(db_query("SELECT * FROM {uc_power_tools} WHERE pcid = '%s'", $node->type));
    if ($class_settings['pcid'] != $node->type) {
      return;
    }
    // Handle auto sku in here since node has not been created in presave state,
    // therefore [nid] is not available yet.
    if ($op == 'insert') {
      if ($class_settings['asku'] == 1 || $class_settings['asku'] == 3 || ($class_settings['asku'] == 2 && $node->model == t('auto SKU'))) {
        uc_product_power_tools_sku_set($class_settings['asku_settings'], $node);
        db_query("UPDATE {uc_products} SET model = '%s' WHERE nid = %d", $node->model, $node->nid);
      }
      // Handle Stock settings here.
      if (module_exists('uc_stock')) {
        $result = db_fetch_array(db_query("SELECT * FROM {uc_product_stock} WHERE sku = '%s' AND nid = %d", $node->model, $node->nid));
        if ($result['sku'] != $node->model) {
          db_query("INSERT IGNORE INTO {uc_product_stock} (sku, nid, active, stock, threshold) VALUES ('%s', %d, 1, %d, %d)", $node->model, $node->nid, $class_settings['stock_settings'], $class_settings['stock_threshold']);
        }
        else {
          db_query("UPDATE {uc_product_stock} SET active=1, stock=%d, threshold=%d WHERE sku = '%s' AND nid = %d", $class_settings['stock_settings'], $class_settings['stock_threshold'], $node->model, $node->nid);
        }
      }
    }
  }
CraigBertrand’s picture

I would be happy to use VBO and a Custom PHP Script. I however do not know php. Please post whatever solution you end up finding. Appreciate your help.

irohit786’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev

did u find any solution?? i think it might be helpful for d7 also

stesind’s picture

I wrote a little module to update existing products with title, node ID or URL. It requires views bulk operations (VBO). Just use git to clone it into your modules directory from my Drupal sandbox:

git clone --recursive --branch master stesind@git.drupal.org:sandbox/stesind/1909676.git uc_product_actions_sku

jpdaut’s picture

I would like to try your module but git asks for stesind@git.drupal.org's password. Can you provide it?

Thank you.

kscheirer’s picture

Status: Active » Needs review

That's cause the link he provided is his personal account - ie with write access to the repo.
git clone --branch master http://git.drupal.org/sandbox/stesind/1909676.git should be good enough for a clone, or take a look at his sandbox directly, https://drupal.org/sandbox/stesind/1909676.

kscheirer’s picture

Component: Documentation » Code
Category: support » feature
Priority: Major » Normal
FileSize
68.22 KB
2.71 KB

Here's a patch to the existing module that adds a checkbox to the admin form (see attached screenshot). When selected and submitted, all nodes in that product class will have the new SKU pattern applied. If this approach is good, it should be fairly easy to add VBO support as a patch as well. Feedback appreciated.

Stesind's sandbox requires VBO, and doesn't support using a token pattern, it just does simple SKUs like node title.

kscheirer’s picture

Issue summary: View changes

Added corresponding code from module.