[ic] db access in commonadjust-called usertag - endless problems

Kaarel Jogi kaarel at hiiumaa.ee
Fri Oct 1 10:03:48 EDT 2004


Hi folks!

Situation: I need to calculate the item price from number of different
fields in products, depending on user profile.  So if the profile is
profile_a, the price would be
(products.price_a * products.amount).

Additionally i need to check second table for existence of possible
"this user special" price, which should be used instead the ones in
products db then.

I guess this is not possible directly with commonadjust functions. So i
defined a usertag to be passed sku and which would look up right place
from databases.  And i just cant get it working right, after several
days on mailinglists and irc :(

Here is what i have at moment:
CommonAdjust "& $Tag->price_lot( $item->{code} )"

And the usertag:
UserTag price_lot Order sku
UserTag price_lot Routine <<EOF
sub
{
  my ($sku) = @_;
  my $db = Vend::Data::database_exists_ref("products")
             or die qq{usertags: cannot open db};
  my $dbh = $db->[$Vend::Table::DBI::DBI]
              or die qq{usertags: cant initiate DBI};
  my $sql = "SELECT * FROM products " .
              "WHERE sku='$sku'";
  my $sth = $dbh->prepare($sql)
               or die qq{usertags: error preparing query};

  $sth->execute();
  
  my $price = 0;
  my $lot = 0;
                                                                                
  if ($sth)
  {
    my $sql_results = $sth->fetchrow_hashref();
    $price = $$sql_results{price};
    $lot = $$sql_results{lot};
  }
                                                                                
  return $price*$lot;
                                                                                }
EOF

All this mess because, apparently $Tag->data wont work in usertag.
It is not finished function, just something to demonstrate if it works.

What happens now is, that I get in error log:
Safe: Can't locate object method "TIEHASH" via package "DBI::st" at
/usr/lib/perl5/site_perl/5.8.0/i386-linux/DBI.pm line 1074.

The error message is same regardless if the tag is defined in catalog or
ic global tags dir. $dbh->prepare($sql) seems to be causing it.

I have no idea what to do about it :(

Perhaps someone has a confirmed, working example of commonadjust-called
usertag that accesses products?

regards, zrg





More information about the interchange-users mailing list