Hi,

I've to build a complex site with a lot of custom product types (from hotel room to a balneotherapy package), I've chosen Commerce to run the eCommerce part.

No problem to create a custom entity and manage associated fields or workflow. But I'm stuck on doing the same with Commerce products.

There was this clue : http://drupal.org/node/1014602 and now I can see the product type in product types list, but no way to edit it or to add fields...

Does someone have a simple but working example of a product type creation?

Regards,
TahitiClic

Comments

rszrama’s picture

Status: Active » Closed (won't fix)

Sorry, we aren't handling support requests for documentation in the tracker... we'd lose too much good information to the netherworld of the issue queue. Instead, please refer to the example in my Commerce Dev installation profile for now... http://github.com/rszrama/commercedev

tahiticlic’s picture

Ok, thanks Ryan, I've posted the same post on the Commerce forum (General Discussion).

I shall have a look on your example.

[Edit] I've downloaded the example, there's no such example (I want to create a Product Type programmatically that works fine). I'll investigate the Commerce code, hoping that it is possible : since it was in Ubercart I think so

rszrama’s picture

I'm sorry, I mistook your post to be referring to a product display node type. Look in commerce_product_ui.install for an example of creating a product type through the Product UI - but those can be edited / deleted. Really it should just take an implementation of hook_commerce_product_type_info() in your module. On a simple test, this allows me to manage fields for the product type just fine:

function example_commerce_product_type_info() {
  return array(
    array(
      'type' => 'example',
      'name' => t('Example'),
      'description' => '',
      'help' => '',
    ),
  );
}
tahiticlic’s picture

Hummm... I should review my install since that's what I did and it didn't work.
I had to return an object by the way.

I'll try with a Commerce dev version install.

rszrama’s picture

Status: Closed (won't fix) » Closed (works as designed)

Ahh, yeah, alpha5 still used objects, but that has all since been converted to arrays.

tahiticlic’s picture

Well... I've tried with a fresh D7 install, with up to date dev versions of dependancies and Commerce. That doesn't work at all : could you precise which versions you're using?

Using this, it works well :

function test_commerce_install() {

  $product_type = commerce_product_ui_product_type_new();

  $product_type['type'] = 'prestation';
  $product_type['name'] = t('Prestation');
  $product_type['description'] = t('A basic prestation.');
  $product_type['is_new'] = TRUE;

  commerce_product_ui_product_type_save($product_type, FALSE);
  
  return "creation ok";

}

tahiticlic’s picture

Status: Closed (works as designed) » Active
rszrama’s picture

Status: Active » Closed (works as designed)

I really do think this work is working, though I'm not sure why your code wouldn't suffice. Install functions shouldn't actually be returning anything... they're just executed when a module is first installed. Have you tried using the hook I posted above? Just put that in an example.module and see what happens.

Dev should be up to date, but there was a lack of synchronization between Git and CVS that could have it out of date. Use my dev version from http://github.com/rszrama/drupalcommerce.

tahiticlic’s picture

That's this version I've used and your exact code.

That's not a real install function (I've return a value only to avoid blank screen), since it would be useless using hook_commerce_product_info, that's just a fast test to see if it can work this way.

I guess I will stand with this for a while and then use hook_commerce_product_info later. Right now with all most recent versions it doesn't work on a fresh install. Maybe something is wrong with my install (I've used D7 stable version, not the dev one), I'll investigate as soon I'll have time.

Thanks for your time though, I go back on Commerce exploration! ;-)

rszrama’s picture

Hmm, ok... that's very weird. You should be fine using D7 1.0, my Github repo, and dev versions of dependencies. Ahh, I do see that you mention using hook_commerce_product_info(). The most current code changed this to hook_commerce_product_type_info() (see my example above).

tahiticlic’s picture

Indeed, that was a problem, now using the hook, I've the product type listed in products list, but when I click on I've this output :
"This product type cannot be edited, because it is not defined by the Product UI module."

Is this normal? I guess I'll have to do something more.

Kingdutch’s picture

I realise this is quite an old post however for people who find this through google the above code will give an error as each product type needs a key, the correct snippet would be:

<?php
function example_commerce_product_type_info() {
  return array(
    'example' => array(
      'type' => 'example',
      'name' => t('Example'),
      'description' => '',
      'help' => '',
    ),
  );
}
?>
cllamas’s picture

Kingdutch,

should we put this snippet in the mymodule.install or in mymodule.module? :)

Thanks.

nicholas.alipaz’s picture

Status: Closed (works as designed) » Needs work

Reopening this support request because the answer doesn't seem to completely solve the question. #11 is the issue I see with simply implementing the hook and nothing else.

This product type cannot be edited, because it is not defined by the Product UI module

How do we get rid of that when trying to edit the product that we added with the hook_commerce_product_type_info()?

alexverb’s picture

Maybe you need to define the module?

The product type array structure includes the following keys:

type: the machine-name of the product type
name: the translatable name of the product type
description: a translatable description of the product type for use in administrative lists and pages
help: the translatable help text included at the top of the add / edit form for products of this type
revision: for product types governed by the Product UI module, this boolean determines whether or not products of this type will default to creating new revisions when edited
module: the name of the module defining the product type; should not be set by the hook itself but will be set when all product types are loaded

Always check out the api:
http://api.drupalcommerce.org/api

rszrama’s picture

Status: Needs work » Closed (works as designed)

I'm not sure I really understood comment #14, but I think the answer is just if it's defined in code, you need to edit it in code. The fields are still editable via the UI, though.

nicholas.alipaz’s picture

Okay, thanks I will review. As I see this issue, it is an issue with Documentation and a support request by users here. The answer that was accepted in this thread did not quite solve the issue for me so thank you for your clarification and hopefully it solves the issue altogether.

xaffimarc’s picture

Issue summary: View changes

Hi there!

Apologies for re-opening this ticket, but I'm really struggling with implementing this.

I'm trying to define a new product type (display and variation type) in my module .install file. I have tried the code from this discussion:

<?php
function example_commerce_product_type_info() {
  return array(
    'example' => array(
      'type' => 'example',
      'name' => t('Example'),
      'description' => '',
      'help' => '',
    ),
  );
}
?>

Sadly, not only does this not really make sense in terms of defining both display and referenced entity, i don't see how i can create fields here either.

Please, can someone point me to some resources that might help me out... ?

Really appreciate your help in advance.

xaffimarc’s picture

Update :

I have managed to create a product type (variation entity), by creating a module.install file and add the following code:

function hook_commerce_product_type_info() {
  $product_types = array();

  $product_types['ebook'] = array(
    'type' => 'ebook',
    'name' => t('E-book'),
    'description' => t('An e-book product uploaded to the site as a PDF.'),
  );

  return $product_types;
}

Now, when the module installs it creates this vraiation type, and adds a deafult "price" field... there are no other fields added here.

Can anyone help me figure our how to add custom fields to this please?

Thanks

ram4nd’s picture

Drupal 7 commerce add product variation types programmatically

$product_type = commerce_product_ui_product_type_new();

$product_type = array(
  'is_new' => TRUE,
  'type' => 'model_' . $id,
  'name' => 'Model ' . $id,
  'revision' => 0,
) + $product_type;

commerce_product_ui_product_type_save($product_type);

if (module_exists('entity_translation')) {
  variable_set('language_product_type_' . $product_type['type'], $product_type['multilingual']);
}
theThinkrium’s picture

Drupal 8 context-

Okay i know this is an old thread but i found a solution by reading the source code and several articles that seems reliable and very "drupally"

if you create the folder structure inside your code /my_module/config/install/

and then create a yml file like this pattern

commerce_product.commerce_product_type.my_type_name.yml

and than put something like whats in the actual commerce product source code

This is an example:

langcode: en
status: true
dependencies: { }
id: lower_case_example_name
label: ExampleName
description: 'This is a example product type'
variationType: default
multipleVariations: true
injectVariationFields: true
traits: { }
locked: false

You can do the same things with a variation type default
same file structure

commerce_product.commerce_product_variation_type.my_variateion_type_name.yml

langcode: en
status: true
dependencies: { }
id: recurring
label: Default
orderItemType: default
generateTitle: true
traits: { }
locked: false

Thats what im doing....

im building a recurring commerce module because i dont have time to sort through the bugs in commerce recurring too much going on...

i fully intend on contributing to commerce recurring if it helps but i dont have time right now so im building my own...