[ic] calling shipping routines from admin pages / ActionMap

Ethan Rowe ethan at endpoint.com
Mon Mar 7 12:23:09 EST 2005


Olaf Kolling wrote:

>Hello experts,
>
>i need your help ;-)
>
>For the creation of an product-exportfile i need the shipping cost
>for each product-sku.
>
>Is there a way to call the shipping routines from a perl routine
>embedded in an admin-page?
>
>I would need all possible shipping methods for the sku in question so
>i can then pick the cheapest one to put into the exportfile.
>The call should take care of selecting the possible methods (i.e. only
>those allowed for a given country, product weight and so on) just like
>in the normal store-front.
>
>Maybe a call like this?
>%methods = getShipping(sku,'DE');
>wich returns a hash with all possible methods:
>(waren-->1.50, buch-->2.00, paket-->4.95)
>  
>Is there any builtin function or a way to call the normal shipping tag
>and supplying all needed params (wich it normaly takes out of the
>basket)?
>
>
>Thanks in advance...
>
>
>Yours
>   Olaf Kolling
>  
>
Olaf,

The shipping tag has an option named "possible".  Thus, as long as a 
valid postal code, province, etc. has been set in the session for the 
shipping logic to make a determination as to what shipping modes are 
available for that session, then you can call:

$modes = $Tag->shipping( { possible => 1 } );

That would populate $modes with a space-separated list of shipmodes 
valid for the particular location.

I don't know how elaborate your shipping logic is, so advice in this 
area can only be of limited value.  However, I would suggest, per sku, 
building a temporary named shopping cart, with only that sku in it -- 
nothing else.  Use some name on the cart such that the cart is 
guaranteed to be safe and not mess up anything else in the session (and 
vice versa).  You can then pass the name of that cart to the shipping 
tag again and test the cost of that sku per mode.

This could be rather slow and painful, depending on how many skus, 
shipping modes, and the complexity of the shipping logic.  But it ought 
to at least have the benefit of being correct in the end. :)  Thus, for 
some skus in @skus:

$Session->{carts}{temporary} ||= [];
... make certain your postal code/province/etc. are set in the session, 
and then...
my @modes = split /\s+/, $Tag->shipping( { possible => 1 } );
my %costs;
for my $sku ( @skus ) {
    @{$Session->{carts}{temporary}} = (
       {
          sku => $sku,
          quantity => 1,
       }
    );
    my @costs = sort { $a <=> $b } map { $Tag->shipping( $_, { cart => 
'temporary' } ) } @modes;
    $costs{$sku} = shift @costs;
}
delete $Session->{carts}{temporary};

That would leave you with a %costs hash having the lowest shipping price 
per sku, I believe.

There may well be some way that's less awful than this of which I'm 
currently unaware.  I'll be well-pleased to learn it if this is the case.

Good luck.
    - Ethan

-- 
Ethan Rowe
End Point Corporation
ethan at endpoint.com


More information about the interchange-users mailing list