Adjustment of the formular for the item price with multipliers

whatifif's picture
Offline
Joined: 07/29/2011
Juice: 5
Adjustment of the formular for the item price with multipliers

Hi Jed,

I adjusted the price-calculation formular so that the pricing is flexble to be applied for most cases.
In this price calculation, the final price is caculated as follows:

$item->price= ($item->price + (accumulated $option[price]) )*(accumulated $option[multiplier])

This logic can be applied for many useful cases:
1. if $option[price]) is given and $option[multiplier] is set to 0 or 1 , then this logic performs the original price caculation. All option prices are just added to item price.

2. if $option[price]) is set to 0 and $option[multiplier] is not 0, then this logic perform the original price times the accumulated $option[multiplier]. This can be applied to $/Kg * Kg type pricing system like pricing meat or milk by weight or volume.

3. if $option[price]) is not 0 and $option[multiplier] is not 0, then this logic performs each option's contribution to the final price. Any option can affect the final price by way of each option's price and each option's multiplier.

Notice the 3 adjusted line:
1. $op_multiplier = 1;// this is set to 1 initially.
2. $op_multiplier *= $option['multiplier'];//$op_multiplier is accumulated
3. $item->price = $op_prices*$op_multiplier;// final $item->price is multiplied by accumulated $op_multiplier

Here I attached my adjusted function.

function uc_attribute_cart_item($op, &$item) {
switch ($op) {
case 'load':
$options = _uc_cart_product_get_options($item);

$op_costs = 0;
$op_prices = $item->price;
$op_weight = 0;

// this is set to 1 initially
$op_multiplier = 1;

foreach ($options as $option) {
$op_costs += $option['cost'];
$op_prices += $option['price'];
if ($option['multiplier'] < 0 || $option['multiplier'] > 0) {
//$op_prices = $op_prices * $option['multiplier'];

//$op_multiplier is accumulated
$op_multiplier *= $option['multiplier'];
}
$op_weight += $option['weight'];
}
$item->cost += $op_costs;
//$item->price = $op_prices;

// final $item->price is multiplied by accumulated $op_multiplier
$item->price = $op_prices*$op_multiplier;
$item->weight += $op_weight;

$combination = array();
foreach ((array)$item->data['attributes'] as $aid => $value) {
if (is_numeric($value)) {
$attribute = uc_attribute_load($aid, $item->nid, 'product');
if ($attribute && ($attribute->display == 1 || $attribute->display == 2)) {
$combination[$aid] = $value;
}
}
}
ksort($combination);

$result = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = %d AND combination LIKE '%s'", $item->nid, serialize($combination));
$model = db_result($result);

if (!empty($model)) {
$item->model = $model;
}
break;
}
}

Price adjustments by multipliers By: tortoise (44 replies) Mon, 05/05/2008 - 18:40