bandedCouriers

stereoid

No avatar

2006-12-19 19:28

Wizzud,

Is it possible to have courier charges that increase with order size (amount)?
Please let me know - as I really need this function.

You write in the About_bandedCouriers:
"Banded Couriers is based on the principle of diminishing costs, ie. the larger the order the lower the courier charges. If you have courier charges that increase with order size this plugin WILL NOT WORK for you!"

stereoid

No avatar

2007-03-03 12:24

Hello all!

I know this problem can be solved easily by someone in this forum. Please let me know which line to edit, and what to edit. I just need a little help by one of you, guys! Here is the code:

<?php
/*
* bandedCouriers.php
* Banded Couriers v2.1
*/
@include_once (DIR_PLUGINS.'bandedCouriers/config.php');
@include_once (DIR_PLUGINS.'bandedCouriers/lang-en.php'); // default
@include_once (DIR_PLUGINS."bandedCouriers/lang-{$config['language']}.php");

// NEW : client run-before function
if( !function_exists('throwCourierBand') ){
/*
* Intercepts fPrice on a tpl call to couriers_select.tpl, block LIST_LIST
* @return void
*/
function throwCourierBand(){
global $iOrder, $aList;
// price is supplied from listCouriers in core/couriers.php, but it needs the appropriate band extracting
if(isset($aList['fPrice']) && $aList['fPrice'] != '' && isset($iOrder) && is_numeric($iOrder) && ($aBasket = dbScanTheBasket($iOrder)) !== false){
$aList['fPrice'] = throwPriceForCourier($aList['fPrice'], $aBasket);
}
} // end function throwCourierBand
}

// NEW : admin run-before function
if( !function_exists('throwCourierBands') ){
/*
* Intercepts fPrice on a tpl call to couriers_list.tpl, block LIST_LIST
* @return void
*/
function throwCourierBands(){
global $tpl, $aList;
// price is supplied from listCouriers in core/couriers.php, but the bands now need expanding to a readable format
$aBands = unbandCourierPrice($aList['fPrice']);
$aList['fPrice'] = '';
foreach($aBands as $v){
$aList['sBandItemsValue'] = trim($v['items'] . ',' . $v['value'], ',');
$aList['fBandPrice'] = $v['price'];
$aList['fPrice'] .= trim($tpl->tbHtml('bandedCouriers.tpl', 'LIST_PRICEBANDS'));
}
} // end function throwCourierBands
}

// NEW : admin run-before function
if( !function_exists('throwCourierForm') ){
/*
* Intercepts aData on a tpl call to couriers_form.tpl, block FORM
* @return void
*/
function throwCourierForm(){
global $aData;
if( isset( $aData['fPrice'] )){
$aBands = unbandCourierPrice($aData['fPrice']);
$count = count($aBands);
for($i=0;$i<$count;$i++){
$aData["iBand$i"] = $aBands[$i]['items'];
$aData["fPrice$i"] = $aBands[$i]['price'];
$aData["fValue$i"] = $aBands[$i]['value'];
}
}
} // end function throwCourierForm
}

// NEW
if( !function_exists('unbandCourierPrice') ){
/*
* Expands a courier price string to its constituent band parts
* @param string
* @return array of arrays
*/
function unbandCourierPrice($fPrice){
// format of fPrice is either <price>
// or <price>:<items>[;<price>:<items>[ ... ]]
// or <price>:<items>:<value>[;<price>:<items>:<value>[ ... ]]
$aBands = array();
$aBand = explode(';',$fPrice);
foreach($aBand as $v){
$aExp = explode(':',$v);
$aBands[] = array( 'price'=>$aExp[0]
, 'items'=>(count($aExp)>1 ? $aExp[1] : 1)
, 'value'=>(count($aExp)>2 ? $aExp[2] : '')
);
}
return $aBands;
}
}

// NEW
if( !function_exists('throwPriceForCourier') ){
/*
* Returns appropriate courier price given basket contents
* @param string courier price bands
* @param array summary of basket contents
* @return string courier price
*/
function throwPriceForCourier($sBands, $aBasket){
$aBands = unbandCourierPrice($sBands);
$rtn = ''; $best = 0;
foreach($aBands as $v){
// find the LOWEST courier price from all the bands that match items AND values to the current basket contents...
if( ($v['items'] == '' || $v['items'] <= $aBasket['items']) && ($v['value'] == '' || $v['value'] <= $aBasket['value']) ){
$new = (float)$v['price'];
if($rtn == '' || $new < $best){
$best = $new;
$rtn = $v['price'];
}
}
}
if($rtn == '' && isset($aBands[0]['price'])){ // just to make sure ...
$rtn = $aBands[0]['price'];
}
return $rtn;
} // end function throwPriceForCourier
}

// NEW : but also defined in taxAndDiscount/tax-ff.php!
if( !function_exists( 'dbScanTheBasket' ) ){
/**
* Tally the number of products, the number of items, and the total value of the items in the basket for an order
* @return array false if basket empty
* @param int $iOrder
*/
function dbScanTheBasket( $iOrder ){
$aFile = file( DB_ORDERS_PRODUCTS );
$iCount = count( $aFile );
$aRtn = array('products'=>0, 'items'=>0, 'value'=>0);
$bFound = false;
for( $i = 1; $i < $iCount; $i++ ){
$aExp = explode( '$', $aFile[$i] );
if( $aExp[1] == $iOrder ){
$bFound = true;
$aRtn['products']++;
$aRtn['items'] += (int)$aExp[3]; // quantity
$aRtn['value'] += (int)$aExp[3] * (float)$aExp[4]; // quantity * price
}
}
unset($aFile, $aExp);
return ($bFound ? $aRtn : $bFound);
} // end function dbScanTheBasket
}

// replacement (core/couriers.php)
if( !function_exists( 'throwCourier' ) ){
/**
* Return courier data
* @return array
* @param int $iCourier
*/
function throwCourier( $iCourier ){
global $iOrder;
$aData = dbThrowCourier( $iCourier );
if( isset( $aData ) ){
list( $aList['iCourier'], $aList['sName'], $aList['fPrice'] ) = $aData;
if(isset($iOrder) && is_numeric($iOrder) && ($basket = dbScanTheBasket($iOrder)) !== false){
$aList['fPrice'] = throwPriceForCourier($aList['fPrice'], $basket);
}
return $aList;
}else{
return null;
}
} // end function throwCourier
}

// replacement (core/couriers-admin.php)
if( !function_exists( 'saveCourier' ) ){
/**
* Save courier
* @return void
* @param array $aForm
*/
function saveCourier( $aForm ){
if( is_numeric( $aForm['iCourier'] ) ){
$bExist = true;
}
else{
$aForm['iCourier'] = throwLastId( DB_COURIERS ) + 1;
$bExist = null;
}

$aForm['sName'] = changeTxt( $aForm['sName'] );

// format $aForm['fPrice'] as a string <price>:<items>:<value>[;<price>:<items>:<value>[ ... ]]
$count = $b = 0; $aPB = $aBands = array();
while(isset($aForm["iBand$b"])){
$value = ($aForm["fValue$b"] == '') ? '' : tPrice(ereg_replace( ',', '.', $aForm["fValue$b"] ));
$price = ($aForm["fPrice$b"] == '') ? '' : tPrice(ereg_replace( ',', '.', $aForm["fPrice$b"] ));
$band = (int)$aForm["iBand$b"];
if($price != '' && ((float)$value>0 || $band>0)){ // no price, no save!
$aBands[$count] = array($price, ($band>0?$band:''), ($value=='0.00'?'':$value));
$count++;
}
$b++;
}
// implode each band into a <price>:<items>:<value> string ...
for($b=0;$b<$count;$b++){ $aPB[] = implode(':', $aBands[$b]); }
// implode all bands into a <band>[;<band>[...]] string ...
$aForm['fPrice'] = implode(';', $aPB);
unset($aBands, $aPB);

dbSaveCourier( $aForm, $bExist );
} // end function saveCourier
}
?>

justtesting

No avatar

2007-09-23 01:57

I have the same problem, but no idea how to solve. :(

Anybody?

Thank you!

merci

No avatar

2007-09-24 08:34

It is hard to say, what should be changed, as we aren't author of this plugin, so we don't modify it. We have our plugin doing something similar, but it is payed (http://opensolution.org/os/templates/en_plugins.html#DK). Maybe first try to ask author of this free plugin to make this modyfication.

ledzep

No avatar

2007-10-02 01:25

This may work?

Change:
if($rtn == '' || $new < $best){

To:
if($rtn == '' || $new > $best){

idaryl

Avatar: idaryl

2007-10-02 12:09

Did that work??

Back to top
about us | contact