Custom Shipping API: Additive Pricing Based on Product SKU

Questions and answers about ShopSite Custom Templates

Custom Shipping API: Additive Pricing Based on Product SKU

Postby aac-matty » Mon Mar 17, 2014 12:10 pm

I'm trying to write some custom shipping that adds costs to shipping differently based on the product type. I need to use the shipping API because the magazines we sell have shipping based on our packaging (i.e. <= 6 fits in a sleeve, <= 35 fits in one box). However, we sell other items too, that do not follow that pricing.

Here's what I have so far:
Code: Select all
#!/usr/bin/perl
if ($ENV{'REQUEST_METHOD'} =~ /get/i) {
    $buffer = $ENV{'QUERY_STRING'};
print "buffer=$buffer\n";
    $method = "Get";
}
else {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    $method = "Post";
}

@nvpairs = split(/&/, $buffer);
foreach $pair (@nvpairs)
{
  ($name, $value) = split(/=/, $pair);

  $name  =~ tr/+/ /;
  $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

  print "$name = $value\n";
  $pos = index($name,"cust_country");
  if ($pos != -1)
  {
    $country = $value;
  }
  $pos = index($name,"quantity");
  if ($pos != -1)
  {
    $quantity = $value;
  }
  $pos = index($name,"weight");
  if ($pos != -1)
  {
    $total += ($quantity * $value);
  }
}
# output status=pass if everything is okay
print "status=pass\n";

print "option_count=1\n";
if ($country eq "US")
{
  print "s1option=United%20States\n";
  if ($total<=1){
  printf ("s1price=%0.2f\n",3.00);}
  elsif ($total<=6){
  printf ("s1price=%0.2f\n",5.05);}
  elsif ($total<=35){
  printf ("s1price=%0.2f\n",11.30);}
  elsif ($total<=70){
  printf ("s1price=%0.2f\n",22.60);}
  else{ #$total> 70
  printf ("s1price=%0.2f\n",33.90);}
}
#...


The value $total holds how many magazines are counted in order to determine the magazine shipping pricing. Other items we sell can have the "additional shipping costs" value filled for their respective products.

I've found that each product has a value for SKU as seen here:
http://www.shopsite.com/help/current/en-US/sc/pro/index.htm?page=/help/current/en-US/sc/pro/shipping.api.spec.html

If the SKU starts with a predetermined value (i.e. "MAG"), then it will add to $total.
In perl, I tried this:
Code: Select all
#the $sku value is defined like the other values
$pos = index($name,"sku");
#...
$pos = index($name,"quantity");
  if ($pos != -1)
  {
    if (substr($sku,0,3) == "MAG")
      $total += ($value);
  }


What can I do to get this working?
Last edited by aac-matty on Tue Mar 18, 2014 5:49 am, edited 2 times in total.
aac-matty
 
Posts: 2
Joined: Fri Mar 14, 2014 5:35 am

Re: Custom Shipping API: Additive Pricing Based on Product S

Postby Jim » Mon Mar 17, 2014 2:56 pm

The first section of code works for me. However it is adding the weights as the $total and not a count. And the shipping rate returned is based on if the weight is <1, <6, <35, <70 or >70 lbs Is that what you intended? It isn't doing a count of items like your packaging comment suggests (unless each magazine is 1lb).

I'm not sure what you are trying to accomplish with the sku part. Could you explain that a little more.
Jim
Site Admin
 
Posts: 4953
Joined: Fri Aug 04, 2006 1:42 pm
Location: Utah

Re: Custom Shipping API: Additive Pricing Based on Product S

Postby aac-matty » Tue Mar 18, 2014 5:25 am

I can see your confusion... sorry!

Go ahead and replace this:
Code: Select all
  $pos = index($name,"quantity");
  if ($pos != -1)
  {
    $quantity = $value;
  }
  $pos = index($name,"weight");
  if ($pos != -1)
  {
    $total += ($quantity * $value);
  }


With this:
Code: Select all
  $pos = index($name,"sku");
  if ($pos != -1)
  {
    $sku = $value;
  }
  $pos = index($name,"quantity");
  if ($pos != -1)
  {
    if (substr($sku,0,3) == "MAG")
      $total += ($value);
  }


First we grab our SKU. This can be used as an identifier to determine whether the product in question is the type we want to modify (in my case magazines will follow this shipping cost breakdown).

Then we grab quantity. Here we do the actual check to see if the SKU is for a magazine, and if so, add its quantity to our $total "quantity".

The price breakdown that follows is based on how many $total magazines we have in the order.

I thought this code would work, but instead it was adding them all regardless of our check. I'm new to Perl, so I figured I have some syntax wrong... This test will ensure only products with a SKU prefixed with "MAG" are added to determine our total for shipping cost. The other items in my store will have individually-set shipping costs per item.

Another way I could accomplish this would be to use a weight-based system where all magazines are weight=1 and all other products (with individually added shipping costs) are weight=0.

One last way I could do this is possibly rig a custom "Product Field" to identify my items in this fashion as well, but I don't how to go about that procedure without similar Perl if/then testing.
aac-matty
 
Posts: 2
Joined: Fri Mar 14, 2014 5:35 am


Return to Custom Template Questions

Who is online

Users browsing this forum: No registered users and 18 guests