Reputation: 175
I'd like to create a product content type from within a module. I followed this very helpful guide to programmatically create a content type. Now how do I "productize" it?
If there already exists a module that does this that I can use to learn from, please do point me in it's direction. Or perhaps there is a guide floating around somewhere?
Thanks!
Upvotes: 1
Views: 1695
Reputation: 1130
I was just figuring this out, this seems to work ok, its unfortunate the api doesnt support this in a official way.
function create_uc_product_type ( $name , $pcid , $description )
{
$pcid = preg_replace ( array ( '/\s+/' , '/\W/' ) , array ( '_' , '' ) , strtolower ( $pcid ) );
db_query ( "INSERT INTO {uc_product_classes} (pcid, name, description) VALUES ('%s', '%s', '%s')" , $pcid , $name , $description );
uc_product_node_info ( TRUE );
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_READ_WRITE ) );
}
module_invoke_all ( 'product_class' , $pcid , 'insert' );
if ( module_exists ( 'imagefield' ) ) {
uc_product_add_default_image_field ( $pcid );
}
$type = node_get_types('type', $pcid);
$type->custom = 1;
node_type_save($type);
node_types_rebuild ( );
menu_rebuild ( );
drupal_set_message ( t ( 'Product class ' . $pcid . ' created.' ) );
}
Upvotes: 1
Reputation: 175
I figured it out. Apparently, if you're creating a content type that is also an ubercart product class, you cannot simply follow the tutorial that I linked to above and then "tack on" ubercart stuff. According to the tutorial above, you need to implement the following hooks to create a content type from within your module:
To create a content type that is also a product class, you need to make the following modifications to the above list:
Use hook_enable() (which fires when the module is enabled), and include the following code:
function uc_yourmodule_enable() {
db_query("INSERT INTO {uc_product_classes} (pcid, name, description)
VALUES ('%s', '%s', '%s')",
'product_class_id',
'Product Class Name',
'Product Class Description.');
node_types_rebuild();
}
As you can see, the snippet adds an entry to the uc_product_classes table, and I guess that's all ubercart needs.
Finally, I've also implemented an ubercart-specific hook further down in my module: hook_product_types()
I'm just figuring this out as I go along, so I'm happy to receive corrections or suggestions.
Upvotes: 2