Last updated February 27, 2007. Created by sime on February 27, 2007.
Log in to edit this page.
First, follow the instructions to create a custom charge the template.
Warning! We don't take any responsibility for use of custom charges in live E-Commerce websites.
Warning! This snippet is untested.
In this example we are going to have a charge that ignores the value of a specific product type when it calculates a "percentage of the item total".
Look for this part in custom.inc. We are cycling through the items on the bill and adding them up, then finally we apply a percentage on top.
<?php
case FLEXICHARGE_CHARGE_PCT_ITEMTOTAL:
$value = 0;
foreach ((array)$txn->items as $item) {
if (product_has_quantity($item)) {
$value += ($item->price * $item->qty);
}
else {
$value += $item->price;
}
}
return ($value * $pct * $factor);
?>By adding a conditional statement, we can avoid adding the a specific product type. Let's use donation products as an example.
<?php
case FLEXICHARGE_CHARGE_PCT_ITEMTOTAL:
$value = 0;
foreach ((array)$txn->items as $item) {
if ($item->ptype != 'donate') { // add this check
if (product_has_quantity($item)) {
$value += ($item->price * $item->qty);
}
else {
$value += $item->price;
}
} // add this closing bracket
}
return ($value * $pct * $factor);
?>