Custom field(s) in uc_product_table

Posts: 52
Joined: 12/16/2007

Is there a way for me to add a custom field to show in the Tables API table uc_product label? I would like to show a custom CCK field in the table as well.

Thanks~

Posts: 822
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Posts: 52
Joined: 12/16/2007

I guess so, but it says I have to add code to the module. Does that mean to add one field to the table I have to hack a module? If so, which one???

Posts: 822
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

No, you don't hack a module, you create your own module, which can hold this and any other customization you want. It's really quite easy, less than 10 lines of code - here's a little tutorial I wrote: http://www.ubercart.org/comment/8749/Re-Removing-SKU-product-edit-page

--

<tr>.

Posts: 52
Joined: 12/16/2007

Thank you for your help! I have followed your instructions and made a module. The new field should up in the Tables API settings. I enabled it. But the value does not display when the table is rendered, only the new label.

I suspect it is in the portion of the extensible tables code:

case 'data':
        $data['score'] = array('male', 'female');
        return $data;

I do not know what to do with this part.

Here is the code in my module:

<?php
function tapir_table_alter($table_id, $op) {
  if (
$table_id == 'uc_product_table') {
    switch (
$op) {
      case
'fields':
       
$fields[] = array('name' => 'field_exterior_length', 'title' => t('Exterior LxWxH'), 'weight' => 3,
                         
'enabled' => FALSE);
        return
$fields;

case
'data':
       
$data['score'] = array('male', 'female');
        return
$data;
    }
  }
}
?>

Any ideas on what I need to put in there? Thanks again for your help!

Posts: 822
Joined: 11/05/2007
Bug FinderFAQ ModeratorGetting busy with the Ubercode.

$data holds the values for your column. Instead of 'score' you need to use your column name, 'field_exterior_length', and instead of assigning it to the fixed array, you need to build an array with your values. If you're storing the values in a CCK field, iterate over the products and extract the value of those CCK fields then put them in the array.

--

<tr>.

Posts: 2008
Joined: 08/07/2007
AdministratoreLiTe!

You should also change the name of your function to yourmodule_table_alter().

Posts: 52
Joined: 12/16/2007

Thank you so much for your help so far. I have been trying in vain to implement the new column as you explained, but have so far been unsuccessful. I do have a little bit of a programming background and tend to be able to figure things out myself - but not this time.


instead of assigning it to the fixed array, you need to build an array with your values. If you're storing the values in a CCK field, iterate over the products and extract the value of those CCK fields then put them in the array.

I have tried to do the above - could you please explain further on the code that I need after $data['field_exterior_length'] ?

Again, thank you kindly for your time~

Posts: 52
Joined: 12/16/2007

Is this close?

  case 'data': 
          $data['field_exterior_length'] = array($node->field_exterior_length);
        return $data;

It doesn't display anything, I was just wondering if I'm on the right track?

Posts: 11
Joined: 04/23/2008

I'm also stuck at this same step, anyone solve this one yet?

<?php
// $Id$

function ucproductfield_table_alter($table_id, $op) {
  if ($table_id == 'uc_product_table') {
    switch ($op) {
      case 'fields':
        $fields[] = array('name' => 'mp3preview', 'title' => t('Mp3 Preview'), 'weight' => 3,
                          'enabled' => FALSE);
        return $fields;


   case 'data':
      $result = db_query("SELECT field_mp3preview_value FROM {content_type_product}");
      while ($row = db_fetch_object($result)) {
        $data['mp3preview'][] = $row->mp3preview;
}


      return $data;

     
    }
  }
}

That is what I have in my module

and

http://www.queencitybeats.com/catalog/1/club_beats

this is what displays.

Posts: 8
Joined: 09/12/2007

Hi,

you have missed undocumented third parametr of the hook_table_alter. This parameter is an array of arguments somehow needed to extend the table.

Example: Extending uc_product_table with two columns from custom table

print_r($args) of the uc_product_table arguments:

Array
(
    [nids] => Array
        (
            [0] => 304
            [1] => 641
            [2] => 454
            [3] => 622
            [4] => 391
            ...
            [48] => 435
            [49] => 633
        )

    [attributes] => Array
        (
            [class] => product-list
        )

)

In this case I can load additional data by parsing 'nids' array:

<?php
function evidence_table_alter($table_id, $op, $args) {
  if (
$table_id == 'uc_product_table') {
    switch (
$op) {
      case
'fields':
       
$fields[] = array('name' => 'evidence',
                         
'title' => t('Evidence'),
                         
'weight' => 3,
                         
'enabled' => TRUE);
       
$fields[] = array('name' => 'sold',
                         
'title' => t('Sold'),
                         
'weight' => 3,
                         
'enabled' => TRUE);
        return
$fields;

      case
'data':
        foreach (
$args['nids'] as $key => $nid) {
         
$r = db_fetch_array(db_query("SELECT eid,sold FROM {evidence} WHERE nid = %d",$nid));
         
$data['evidence'][] = $r['eid'];
         
$data['sold'][] = ( $r['sold'] === '0' ) ? t('Available') : t('Sold');
        }
        return
$data;
    }
  }
}
?>

This tapir feature is not well documented, I learn it by code research. I hope it helps someone.

Happy ÜberDrupaling Smiling

--

Wojtha
Freelance ÜberDrupaler
http://kusy.info

Posts: 12
Joined: 04/10/2008

wojtha,

Thanks for the example. That was just the info I needed. The fact that the official example was missing the third parameter had me really confused.

Posts: 4368
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Aye, thanks for sticking it out and helping wojtha. I updated the documentation for now.

Posts: 2
Joined: 08/01/2008

It seems the link for Download tapirtest.module is broken in this page: TAPIr: Learn by Example. Could anybody fix this?

I had already added a custom field to the uc_product table by using attribute module(an indirect way). Now I need to add another custom field only visible for system administrators, but the accessibility(or visibility) of attributes added by attribute module cannot be configured. Seems altering the uc_product table directly -- in the way discussed in this topic is the solution. Anybody can give me a further suggestion?

Thanks.

Posts: 4368
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

(I re-uploaded that .tar.)