Community Documentation

Snippet: Total items excludes product type

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);
?>

About this page

Drupal version
Drupal 4.7.x, Drupal 5.x

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.