Skip to content
This repository
branch: STABLE_5_0-bra…
Fetching contributors…

Cannot retrieve contributors at this time

file 189 lines (146 sloc) 4.017 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
UserTag weight Order attribute
UserTag weight addAttr
UserTag weight Routine <<EOR
sub {
my ($attr, $opt) = @_;
$opt ||= {};

my $cart;
if($opt->{cart}) {
$cart = $Vend::Session->{carts}{$opt->{cart}} || [];
}
else {
$cart = $Vend::Items;
}

my $wsub;

my $field = $opt->{field} || 'weight';
my $table = $opt->{table};
my $osub;

if($opt->{options}) {
BUILDO: {
my $oattr = $Vend::Cfg->{OptionsAttribute}
or last BUILDO;
my $odb = dbref($opt->{options_table} || 'options')
or last BUILDO;
my $otab = $odb->name();
my $q = qq{
SELECT o_group, weight FROM $otab
WHERE sku = ?
AND weight is not null
AND weight != ''
};
my $sth = $odb->dbh()->prepare($q)
or last BUILDO;
if($oattr and $odb) {
$osub = sub {
my $it = shift;
my $oweight = 0;
if($it->{$oattr} eq 'Simple') {
$sth->execute($it->{code});
while(my $ref = $sth->fetchrow_arrayref) {
my ($opt, $wtext) = @$ref;
next unless length($it->{$opt});
my $whash = get_option_hash($wtext);
next unless $whash;
$oweight += $whash->{$it->{$opt}};
}
}
return $oweight;
};
};
}
}

if($attr) {
$attr = $opt->{field} || 'weight';
$wsub = sub {
return shift(@_)->{$attr};
};
}
elsif($opt->{fill_attribute}) {
$attr = $opt->{fill_attribute};
$wsub = sub {
my $it = shift;
return $it->{$attr} if defined $it->{$attr};
my $tab = $table || $it->{mv_ib} || $Vend::Cfg->{ProductFiles}[0];
$it->{$attr} = tag_data($tab,$field,$it->{code}) || 0;
return $it->{$attr};
};
}
else {
$wsub = sub {
my $it = shift;
my $tab = $table || $it->{mv_ib} || $Vend::Cfg->{ProductFiles}[0];
return tag_data($tab,$field,$it->{code});
};
}

my $total = 0;
for(@$cart) {
$total += $_->{quantity} * $wsub->($_);
next unless $osub;
$total += $_->{quantity} * $osub->($_);
}

unless($opt->{no_set}) {
$::Scratch->{$opt->{weight_scratch} ||= 'total_weight'} = $total;
}

return $total unless $opt->{hide};
return;
}
EOR

UserTag weight Documentation <<EOD
=head1 NAME

ITL tag [weight] -- calculate shipping weight from cart

=head1 SYNOPSIS

 [weight]
 [weight
    attribute=1*
    cart=cartname*
    field=sh_weight*
    fill-attribute=weight*
    hide=1|0*
    no-set=1|0*
    table=weights*
    weight-scratch=sh_weight*
 ]

=head1 DESCRIPTION

Calculates total weight of items in shopping cart, by default setting
a scratch variable (default "total_weight").

=head2 Options

=over 4

=item attribute

If set, weight tag will calculate from the field in the item itself instead
of going to the database. This is the most efficient, and can be enabled
by using this in catalog.cfg:

AutoModifier weight

The default is not set, using the database every time.

=item cart

The cart to calculate for. Defaults to current cart.

=item field

The fieldname to use -- default "weight". This applies both to attribute
and database.

=item fill-attribute

Sets the attribute from the database the first time, and uses it thereafter.
Sets to weight of a single unit, of course.

=item hide

Don't display the weight, only set in Scratch. It makes no sense to
use hide=1 and no-set=1.

=item no-set

Don't set the weight in scratch.

=item options

Scan the options table for applicable options and adjust weight
accordingly. Only works for "Simple" type options set in the
OptionsEnable attribute, and the o_group and weight fields must
represent the option attribute and the weight text. The weight text is a
normal Interchange option hash string type, i.e.

titanium=-1.2, iron=1.5

where "titanium" and "iron" are the values of an option
setting like "blade".

Will only work if your options table is SQL/DBI.

=item table

Specify a table to use to look up weights. Defaults to the table the
product was ordered from (or the first ProductFiles).

=item weight-scratch

The scratch variable name to set -- default is "total_weight".

=back

=head1 AUTHOR

Mike Heins

=cut
EOD
Something went wrong with that request. Please try again.