16 replies [Last post]
yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294

i have a site that sells memberships using ubercart, and the one product has an attribute (drop down) so the customer can pick which "group" they are joining.

when I create a group (organic groups using a node type called Group), I want to also add a value to the pull down in the product with that group name.

so I was going to do something like make a rule. on create content, type = Group, execute php

and in the php, I was going to put the code to add an option to the attribute, instead of having to go to admin/store/attributes/2/options/add and do it as a human

I'm importing 60 Groups, and creating the group nodes ok. and I'd like to set up the options programmatically too. And each time I do an import, I'll have to do it again, so it makes sense to set this up. Also, I'll be passing on the admin of the site to others, and it is easier for them to just "create a group". I'll have a hard time getting them to remember to go into the store admin and find the place to add an option to an attribute. They are just going to want to "create a group" (which is just a node) and fill out that form and have the pull down option on the product magically have the name of the new group listed there.

so I figure I could populate some kind of array like mynewoption->name="The name"; mynewoption->price=0; mynewoption->cost=0;

then uc_attribute_option_add($mynewoption)

... but I dont know how to find out what the function is called, or what the structure needs to look like.

I just know the page to do it as a human is admin/store/attributes/2/options/add

I've looked at the documentation api examples http://www.ubercart.org/docs/api
and it might be there... but I dont know for sure what I'm looking for.

I also looked in the code
http://drupalcode.org/viewvc/drupal/contributions/modules/ubercart/uc_at...
and
http://drupalcode.org/viewvc/drupal/contributions/modules/ubercart/uc_at...

How do I do this? How do I find where the info I need is, and maybe the answer is out there... but I'm not googling for the right thing...

Thanks for any leads.

ps. (this is related to: http://www.ubercart.org/forum/support/15829/practical_advice_setting_lar...)

Docc's picture
Offline
Getting busy with the Ubercode.
Joined: 07/03/2008
Juice: 168
Re: api for adding an option to an attribute? how do I add an op

As far as i know there is no API for this.
Though you wil get there with the use of internal ubercart functions and some handywork. But as you already found out. Attributes/options is a pain in the ass.

If you want dynamic options you better go a different way.

I always use the hook_form_alter function on the cart form to add my own drop downs
And then save the selection to the order with hook_add_to_cart_data

univate@drupal.org's picture
Offline
Getting busy with the Ubercode.
Joined: 03/27/2009
Juice: 465
Re: api for adding an option to an attribute? how do I add an op

There are no existing functions for creating attributes and option, the following issue/patch attempts to adds these types of functions:
http://drupal.org/node/488422

Although the other way to do this is via drupal_execute() which submitted a form in drupal, so you can in code fill out a form and submit it.

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
drupal_execute('uc_attribute_option_form', $values, 2);

// aid 2 is the group name attribute
$values['name'] = 'robo-user' . check_plain($node->nid);
drupal_execute('uc_attribute_option_form', $values, 2);

Does this look right? I got an error:
warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'uc_attribute_option_form' was given in /home/yestande/public_html/lllofil/includes/form.inc on line 372.

and I dont see an option with a name that starts with robo so I know I must have done something wrong.

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
maybe I need

maybe I need
module_load_include('inc', 'uc_attribute', 'uc_attribute.admin');
?

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
Re: maybe I need

adding the module load got rid of the warning message.

so now I have

module_load_include('inc', 'uc_attribute', 'uc_attribute.admin');
$values['name'] = check_plain($node->field_groupname[0]['value']);
drupal_execute('uc_attribute_option_form', $values, 2, NULL);

but it still did not actually add an option, why?

how can I find out? I have a sinking feeling I might have to learn to step through the stack or something like that....

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
Re: Re: maybe I need

ah, maybe I should check the log...

admin/reports/dblog
says:
nothing since I've fixed the warning message...

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
Re: Re: Re: maybe I need

looking again at http://drupalcontrib.org/api/function/uc_importer_import/5 (do a find for uc_attribute_option_form)
and http://api.ubercart.org/api/function/drupal_execute/2

I added an array level and tried
module_load_include('inc', 'uc_attribute', 'uc_attribute.admin');
$form_state = array();
$form_state['values']['name'] = check_plain($node->field_groupname[0]['value']);
drupal_execute('uc_attribute_option_form', $form_state, 2, NULL);

And I got a new message that said
# Created new option New Test 1.

(New Test 1 is what was in the groupname field of the node just saved and is what I'm sending in as the desired new name for the option)

But when I go list the options, I dont see one called New Test 1.

Feel like I'm closer, but still missing something.

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
module_load_include('inc',

module_load_include('inc', 'uc_attribute', 'uc_attribute.admin');
$form_state = array();
$myaid=2;
$form_state['values']['name'] = check_plain($node->field_groupname[0]['value']);
drupal_execute('uc_attribute_option_form', $form_state, $myaid, NULL);

is working better, saving it in the uc_attribute_options table ... but with aid 0

why is myaid not getting in?

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
It works!

I looked at the code for the function on line 401 of http://drupalcode.org/viewvc/drupal/contributions/modules/ubercart/uc_at...

and saw it was looking for the aid inside the arg, not the arg itself.

So I tried:
module_load_include('inc', 'uc_attribute', 'uc_attribute.admin');
$form_state = array();
$myaid->aid=2;
$form_state['values']['name'] = check_plain($node->field_groupname[0]['value']);
drupal_execute('uc_attribute_option_form', $form_state, $myaid);

and it works!

....

now to figure out how to enable it on a particular node. Smiling

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
enable the option on the product node

My (not prefect) code for enabling the option on the product node.
this gets run as "show a message" via a rule triggered when the content type Group (an organic group) is created.
The aid, attribute, 2 is the one that holds all the group names, and the product, 19, is the product that is membership in any group.

If you have suggestions for better ways of coding this, please comment away.

<?php
$myaid
->aid=2;
$myproductid=19
$myoptionname = $node->field_groupname[0]['value'];
$result = db_query("SELECT myoptions.oid FROM {uc_attribute_options} myoptions WHERE myoptions.name = '%s' AND myoptions.aid = %d", $myoptionname, $myaid->aid);
if (
$row = db_fetch_object($result))
{
  echo
"Option id matching that name is: ";
  echo
$row->oid;
  echo
". ";
 
$productresult = db_query("SELECT myprodoptions.oid FROM {uc_product_options} myprodoptions WHERE myprodoptions.nid = %d AND myprodoptions.oid = %d", $myproductid, $row->oid);
  if (
db_fetch_object($productresult)) {
    echo
"The option was already enabled. Contact developer. ";
  } else {
   
db_query("INSERT INTO {uc_product_options} (nid, oid) VALUES (%d, %d)", $myproductid, $row->oid);
    echo
"Enabled option $row->oid on product (node) $myproductid";
    echo
". ";
  }
  if (
$row = db_fetch_object($result))
  {
    echo
"Oops and there were other options matching that name: ";
    echo
$row->oid;
    while (
$row = db_fetch_object($result))
    {
      echo
" ";
      echo
$row->oid;
    }
    echo
". ";
    echo
"Having more than one option with the same name is going to cause problems. Contact a developer and delete the group you just made. Create a new group with a slightly different name.";
  }
}
else {
echo
"no options found with that name. something is really wrong. contact developer.";
}
?>
griz's picture
Offline
Joined: 02/25/2011
Juice: 42
Ubercart Product Actions

Hows about getting this code into this:

http://drupal.org/project/uc_product_actions

univate@drupal.org's picture
Offline
Getting busy with the Ubercode.
Joined: 03/27/2009
Juice: 465
Re: api for adding an option to an attribute? how do I add an op

All I can say is lets get some testing done on this issue:
http://drupal.org/node/488422

This should be a one liner in PHP

<?php
uc_attribute_option_save
($option, $product_id, 'product'); // I can't be bothered to look up the function name so this is probably not correct
?>
yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
Re: Re: api for adding an option to an attribute? how do I add a

I totally agree! it should be a one liner!

I'm not sure ... with some people focusing on Drupal Commerce for D7, is there still a chance of getting the api for Ubercart improved?

Andy's picture
Offline
Administrator
Joined: 08/07/2007
Juice: 1153
Re: Re: Re: api for adding an option to an attribute? how do I a

Absolutely. Read the Ubercart Roadmap for the details, but improving / building Ubercart API's is in the top three tasks for Ubercart on D7 once the port is complete. The other two are dumping Conditional Actions for Rules, and fieldable products.

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 294
Re: Re: Re: Re: api for adding an option to an attribute? how do

thanks for the link to the road map. Smiling Good Rules is up there... as that is what I'm using, or try to use! Smiling

Andy's picture
Offline
Administrator
Joined: 08/07/2007
Juice: 1153
Re: Re: Re: Re: Re: api for adding an option to an attribute? ho

As always, we are looking for suggestions, comments, recommendations etc... if you have any.