Hi All,

i have a need to programmatically create a product type on install, and then create products based on user content. Right now, im stuggling with getting the product type to work, here is the code i have for product types:

/**
* Implements hook_install().
*/
function cg_commerce_signs_install() {
  drupal_static_reset();
  commerce_product_configure_product_types();

  $product_type = commerce_product_ui_product_type_new();

  $product_type['type'] = 'commerce_sign';
  $product_type['name'] = t('Commerce Sign');
  $product_type['description'] = t('A commerce sign product type.');
  $product_type['is_new'] = TRUE;

  commerce_product_ui_product_type_save($product_type, TRUE);
}

and for generating a product (in the module file) - This works fine, it gets stored in the database, but never shows yup as the type its assigned to doesn't exist.:

/**
 * Create a product programmatically.
 */
function cg_commerce_signs_create_product($product_type, $extras) {
  // Generate a new product object
  $new_product = commerce_product_new($product_type);

  $new_product->status = $extras['status'];
  $new_product->uid = $extras['uid'];

  $new_product->sku = $extras['sku'];
  $new_product->title = $extras['title'];
  $new_product->created = $new_product->changed = time();

  commerce_product_save($new_product);
  return $new_product->product_id;
}

the process i need to complete is:

On install build product type.
In the admin area, create products assign prices etc
On save of user content, generate a product variant that can be bought via the checkout.

Something like vistaprint where you have a product that you can customise and then buy.

does anyone have any pointers on what im missing here? - the first time i've used commerce API..

Thanks

Comments

WorldFallz’s picture

I think this is usually done with commerce_custom_product, but in any case it might serve as a worthwhile example. Also, for such a module specific question, you'll be more likely to get the right eyes on it if you post to the commerce issue queue.

N1ghteyes’s picture

I'll poke them on the commerce forums for now then :)

Thanks Wordfallz

N1ghteyes’s picture

just as an update, i've posted on the commerce forums, but i have managed to find the error...

im getting:

FieldException: Attempt to create an instance of field commerce_price without a bundle. in field_create_instance() (line 481 of /home/signdes/public_html/modules/field/field.crud.inc).
The website encountered an unexpected error. Please try again later.

so i guess missing a product type or something..

will post up when i have the solution, unless someone else beats me to it :)

N1ghteyes’s picture

turns out this is the issue:

function cg_commerce_signs_commerce_product_type_info() {
   return array(
    array(
      'type' => 'commerce_sign',
      'name' => t('Online Signs'),
      'description' => 'Product type for all sign designs.',
      'help' => '',
    ),
  );
}

i dont think its needed at any rate, so for now commented out. if anyone knows different shout!