6 replies [Last post]
matze999's picture
Offline
Joined: 05/13/2009
Juice: 55
Was this information Helpful?

Hi,

i was wondering if there is an easy way to attach attributes to a product class.
In fact i am doing the whole setup programmatically (i.e. first create the attributes programmatically, then the product class, both via drupal_execute).

However, i don't think it is going to be as easy as using drupal_execute to attach the attributes to the product class.

In fact the only way i can sort of see to do this right now is to copy some code out of uc_attribute/uc_attribute.admin.inc::uc_object_attributes_form but i'd really like to avoid that.

Any ideas?

thanks
matt

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Attach attributes to product class programmatically

The API for that is pretty weak right now, unfortunately. Your best bet would be to just look in the DB and see what the form submission adds into the class related tables then mimic that with your own DB queries. Sorry about that!

matze999's picture
Offline
Joined: 05/13/2009
Juice: 55
No problem, I'll try my best

No problem,

I'll try my best to get something worked out. Thanks for the reply though.

ckidow's picture
Offline
Joined: 10/27/2008
Juice: 46
Re: Attach attributes to product class programmatically

Is that the only method at the moment you would offer? Is there an alternative way?

I justed wanted do import 1400 node (product) with attrbutes by a selfmade script. Source is a csv and I am using node_save()... the only problem at the moment is to add the attributes to the nodes.

So what are you suggesting to do? First create the nodes and after that altering the DB just like you mentioned above?

incaic's picture
Offline
Joined: 10/13/2007
Juice: 115
create attributes w/drupal_execute
matze999 wrote:

(i.e. first create the attributes programmatically, then the product class, both via drupal_execute).

Matt,

How did you create the attributes using drupal_execute? I've been trying to do it to no avail. Mind sharing?

Thanks,
e.

matze999's picture
Offline
Joined: 05/13/2009
Juice: 55
Creating attributes programmatically via druapl_execute

Hey dude,

i am so sorry, i did not even see your post until now. You probably have figured it out by now, but here it is anyway:

//-- insert a random attribute into the attributes table
//-- the fields to be inserted are defined in ubercart/uc_attribute/uc_attribute.admin.inc
$form_id = 'uc_attribute_form'; //located in: ubercart/uc_attribute/uc_attribute.admin.inc
$form_state['values'] =
array(
'name' => $my_att_name,
'label' => $my_att_label,
'description' => $my_att_description,
'required' => $my_att_required,
'ordering' => $my_att_ordering,
'display' => $my_att_display,
);
//print 'Calling uc_attributes_form';
drupal_execute($form_id, $form_state);

There it is.

matt

ronaldmulero's picture
Offline
Joined: 08/29/2010
Juice: 6
#5 above actually just adds a new Store Attribute to Ubercart.

To add previously defined Store Attributes (named "A", "B", "C") and their Options to a Product Class (named "Class 1"), the code below worked for me in Ubercart 6.x-2.3:

module_load_include('inc', 'uc_attribute', 'uc_attribute.admin');
$aid_a = db_result(db_query("SELECT aid FROM {uc_attributes} WHERE name = '%s'", t('A')));
$aid_b = db_result(db_query("SELECT aid FROM {uc_attributes} WHERE name = '%s'", t('B')));
$aid_c = db_result(db_query("SELECT aid FROM {uc_attributes} WHERE name = '%s'", t('C')));
$result = db_query("SELECT * FROM {uc_product_classes} WHERE name = '%s'", t('Class 1'));
$object = db_fetch_object($result);
$type = 'class';
$view = 'add';
$form_state = array();
$form_state['values']['add_attributes'][0] = $aid_a;
$form_state['values']['add_attributes'][1] = $aid_b;
$form_state['values']['add_attributes'][2] = $aid_c;
drupal_execute('uc_object_attributes_form', $form_state, $object, $type, $view);