2

I'm looking to create a new module that when enabled will create a new content type and create an ubercart product class for it automatically.

I've tried googling and searching stackexchange for a few days but couldn't find much that is current or applicable to D7/U3 (or so I think).

There's plenty of info on creating a node programmatically.

My question is: Once I have created the node, how do I programmatically "productize it"?

3
  • 1
    There are a couple of steps involved - take a look at uc_product_class_form_submit(), you're basically looking to reproduce that
    – Clive
    Dec 11, 2013 at 16:55
  • Perfect! That's just what I needed. Thanks for the nudge in the right direction. :) Dec 11, 2013 at 17:06
  • No problem - if you get the chance it'd be great if you could post the snippet you end up using an answer (for future visitors). There's an annoying 8 hour wait-period before new users can answer their own questions, but if you get a chance at some point that would be great :)
    – Clive
    Dec 11, 2013 at 17:09

1 Answer 1

2

Thanks to Clive for pointing me in the right direction. As requested here is a code snippet I used in my module. I was able to take the code found in uc_product_class_form_submit() and make minor changes to make it work for my situation.

Essentially, in my module, I'm using hook_enable to call a helper function which checks for an existing product class. If it doesn't exist, then it creates it (as well as the node type). My module will go on to do more, but this is the relevant piece. (more specifically the function uc_suggested_donation_levels_add_product_class).

<?php

/**
 * @file uc_suggested_donation_levels.module
 * Adds up to 4 donation levels including a "you choose" amount.
 */
// Constants
define('DONATION_PRODUCT_TYPE_MACHINE_NAME', 'giving_project');
define('DONATION_PRODUCT_TYPE_READABLE_NAME', 'Giving Project');
define('DONATION_PRODUCT_TYPE_DESCRIPTION', 'Adds up to 4 donation levels including a [you choose] amount.');
define('DONATION_PRODUCT_TYPE_ERROR', 'Content type already exists in database. No need to re-create.');

/**
 * Implements hook_enable.
 * When this module is enabled it will check if the product type exists,
 * if not, then it will create it and productize it.
 */
function uc_suggested_donation_levels_enable() {
  uc_suggested_donation_levels_add_product_class();
}

function uc_suggested_donation_levels_add_product_class() {
  // first check to see if this product already exists.
  if (in_array(DONATION_PRODUCT_TYPE_MACHINE_NAME, uc_product_types())) {
    // The product type already exists, abort.
    watchdog('uc_suggested_donation_levels', DONATION_PRODUCT_TYPE_ERROR, array(), WATCHDOG_INFO);
    drupal_set_message(DONATION_PRODUCT_TYPE_ERROR, 'status');
    return; 
  } 

  $pcid = DONATION_PRODUCT_TYPE_MACHINE_NAME;

  $result = db_merge('uc_product_classes')
    ->key(array('pcid' => $pcid))
    ->fields(array(
      'name' => DONATION_PRODUCT_TYPE_READABLE_NAME,
      'description' => DONATION_PRODUCT_TYPE_DESCRIPTION,
    ))
    ->execute();

  db_update('node_type')
    ->fields(array(
      'name' => DONATION_PRODUCT_TYPE_READABLE_NAME,
      'description' => DONATION_PRODUCT_TYPE_DESCRIPTION,
    ))
    ->condition('type', $pcid)
    ->execute();

  uc_product_node_info(TRUE);

  if ($result == MergeQuery::STATUS_INSERT) {
    variable_set('node_options_' . $pcid, variable_get('node_options_product', array('status', 'promote')));

    if (module_exists('comment')) {
      variable_set('comment_' . $pcid, variable_get('comment_product', COMMENT_NODE_OPEN));
    }

    module_invoke_all('uc_product_class', $pcid, 'insert');
  }
  else {
    module_invoke_all('uc_product_class', $pcid, 'update');
  }

  node_types_rebuild();

  $type = node_type_get_type($pcid);
  node_add_body_field($type, t('Description'));
  uc_product_add_default_image_field($pcid);

  menu_rebuild();

  drupal_set_message(t('New donation content type and product class has been created.'));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.