Shipping API example service

From CommerceV3 Docs

Jump to: navigation, search


CV3 Documentation Resources

<?php

/*****************************************

Shipping Web Service for CommerceV3

Copyright 2005 Color Maria, Inc.,
Please direct all questions, bug reports, or suggestions for
changes to lucas@colormaria.com.
For more information, see http://docs.commercev3.com.

by Lucas Oman <lucas@colormaria.com>
Created: 2005-01-27
Updated: 2005-05-02

******************************************/


// change the name of the store here.
// example: 'mystore' from 'mystore.commercev3.com'
define('STORE','shoethatloveyou');

$g = explodeQuery();
// see function headers for description of these two data structures
$byshipto = createByShipTo($g); // organized by ship-to, used by most of these functions
$byprod = createByProd($g); // organized by sku, alternate way to organize
$additional = 0; // additional shipping amount
$message = ''; // message to be displayed in checkout process

// array containing data for response
// do not change unless you do not plan to use the sendResponse() function
$response = array(
'smeths'  => array(),
'tadd'    => 0,
'smsg'    => ''
);

// array containing prioritized shipping methods
// if upgrading shipping methods, this must be correct
// according to the methods in your store
$methods = array(
'Default',
'2nd Day Air',
'Ground'
);



// ***
// *** YOU'LL PROBABLY WANT TO PUT ALL OF YOUR OWN CALCULATIONS BELOW THIS
// ***

// example: add $4 for each prod with sku sku123
$byshipto = adjustByProd('sku123','4.00',$byshipto);
// example: add an additional $40 to the order's shipping
$additional += 40;
// example: message to display in checkout to explain the adjustments
$message = "You've been charged extra.";

// ***
// *** AND YOU'LL PROBABLY NOT WANNA TOUCH ANYTHING BELOW THIS
// ***



foreach ($byshipto as $shipto) {
  $response['smeths'][] = $shipto['method']; // must match in CV3 admin
}
$response['tadd'] = $additional; // can also be negative
$response['smsg'] = $message;

sendResponse($response);
ob_start();
print_r($_GET);
print("\n\n\n");
print_r($response);
$mailresponse = ob_get_contents();
ob_end_clean();
mail('Lucas Oman <lucas@colormaria.com>','Shipping Service: '.STORE,$mailresponse);


/*********************************
* FUNCTIONS
*********************************/

/*
adjustment amt per each prod
*/
function adjustByProd($sku,$amt,$byshipto) {
  foreach ($byshipto as $shipto) {
    if (isset($shipto['prods'][$sku])) {
      $addl += $shipto['prods'][$sku]['qty'] * $amt;
    }
  }
  return $addl;
}

/*
change/upgrade ship-to for if recieving product
$upgrade == FALSE if you want to simply set every ship-to with prod to this method,
            TRUE if you want to upgrade to this method, but don't change if
                 current method has higher rank (as decided by $methods array above)
*/
function changeMethod($sku,$method,$upgrade,$byshipto) {
  $flipped = array_flip($methods);
  foreach ($byshipto as $key => $shipto) {
    if (isset($shipto['prods'][$sku])) {
      if ($upgrade) {
        if ($flipped[$method] < $flipped[$shipto['method']]) {
          $byshipto[$key]['method'] = $method;
        }
      } else {
        $byshipto[$key]['method'] = $method;
      }
    }
  }
  return $byshipto;
}

/*
explode all the lists into arrays
s* keys are CDLs linked by ship-to
a* keys are all-order CDLs
*/
function explodeQuery() {
  $g = array();
  foreach ($_GET as $key=>$get) {
    if (preg_match('/^s/i',$key) || preg_match('/^a/i',$key)) {
      $get = explode(',',urldecode($get));
      $g[$key] = $get;
    } else {
      $g[$key] = urldecode($get);
    }
  }
  return $g;
}

/*
creates structure organized by ship-to

Format of structure:
array(
  0 => array(
    'zip'
    'state'
    'country'
    'method'
    'price'
    'prods' => array(
      <sku1> => array(
        'weight'
        'qty'
        'price'
      )
      <sku2> => array(
        ...
      )
      ...
    )
  )
  1 => array(
    ...
  )
  ...
)
*/
function createByShipTo($g) {
  $byshipto = array();
  $skucount = 0;
  foreach ($g['sgrps'] as $key=>$num) {
    $prods = array();
    // create list of products for this ship-to
    for ($i=0;$i<$num;$i++) {
      $sku = $g['askus'][$skucount+$i];
      $prods[$sku] = array(
       'qty'    => $g['aqtys'][$skucount+$i],
       'price'  => $g['aprices'][$skucount+$i],
       'weight' => $g['aweights'][$skucount+$i]
       );
    }
    // throw in the rest of this ship-to's info
    $byshipto[] = array(
     'zip'    => $g['szips'][$key],
     'state'  => $g['sstates'][$key],
     'country'=> $g['scountries'][$key],
     'method' => $g['smeths'][$key],
     'price'  => $g['sprices'][$key],
     'prods'  => $prods
     );
    $skucount += $num;
  }
  return $byshipto;
}

/*
creates structure of all products

Format:
array (
  <sku1> => array (
    'weight'
    'qty'
    'price'
  )
  <sku2> => array (
    ...
  )
  ...
)
*/
function createByProd($g) {
  $byprod = array();
  foreach ($g['askus'] as $key=>$sku) {
    if (!isset($g[$sku])) {
      $g[$sku] = array(
       'weight' => $g['aweights'][$key],
       'qty'    => $g['aqtys'][$key],
       'price'  => $g['aprices'][$key],
       );
    }
  }
  return $byprod;
}

/*
format and send response back to CV3

Format of reply:
smeths=<ship methods (comma delimited string)>
tadd=<total order shipping adjustment (signed float)>
smsg=<shipping message for display in checkout (HTML string)>
*/
function sendResponse($response) {
  foreach ($response as $key=>$line) {
    if (is_array($line)) {
      $line = implode(',',$line);
    }
    $response[$key] = $key.'='.$line;
  }
  $response = implode("\n",$response);
  // finally, send the response back
  print($response);
}

?>
Personal tools