No replies
itmartin's picture
Offline
Joined: 01/19/2012
Juice: 5
Was this information Helpful?

For my marketplace site I need attributes to be automatically assign to product, depends on product taxonomy. I do not want to use product classes.
On a site http://dgtlmoon.com/ubercart-automatically-add-product-attributes-options I have found this code:

function MYMODULE_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'product_node_form') {
$form['#submit'][] = 'MYMODULE_product_add_attribute';
}
}

function MYMODULE_product_add_attribute(&$form, &$form_state) {

// cheating, but use this original function
module_load_include('inc', 'uc_attribute', 'uc_attribute.admin');

// auto assign product attribute based on taxonomy (saves admin from thinking too much)
foreach($form_state['values']['taxonomy'] as $term_selection) {
$aid = false; // reset the attribute ID
if(is_array($term_selection)) {
if ( $tid = current($term_selection) ) {
switch($tid) {
case 1:
case 4:
$aid = 2;
break;
case 5:
$aid = 3;
break;
}
}
}
if($aid) {
$existing_attributes = uc_product_get_attributes(arg(1));
if( !isset($existing_attributes[$aid] )) {
$attribute=(array)uc_attribute_load($aid, NULL, 'product');
unset($attribute['options']);
$uc_form_state['values']['attributes'][$aid] = $attribute;
$uc_form_state['values']['add_attributes']= array($aid);
$uc_form_state['values']['type'] = 'product';
$uc_form_state['values']['view'] = 'add';
$uc_form_state['values']['id'] = arg(1);
uc_object_attributes_form_submit( array(), $uc_form_state );
}
}
}
}

I've made custom module using this code but it does nothing.
This code is working with ubercart 2.4 only.
Can someone help me to find out why this code does not work?