Thanks for your post, I had a similar (but not exactly the same) issue.
Our store has price groups for every product. These price groups are actually determined by a multiplier of the product's cost to the seller. Furthermore, some products have quantity pricing as well!
I have not yet dared to change both the admin and the store to calculate the displayed price based on a base price * multiplier; however I was able to adapt the store (which has the price groups mod) to display quantity pricing based on the price group.
I took Pe7er's code above, tweaked it a bit to suit my store. However I noticed that the shopping cart does not display the correct quantity pricing nor does it show the correct subtotal. I am adding this post to help others correct that.
There are two places where you need to change or add code:
- store/includes/modules/pages/shopping_cart/header_php.php
- store/includes/classes/shopping_cart.php
store/includes/modules/pages/shopping_cart/header_php.php
If you have an un-modded header_php.php, on line 141 is:
PHP Code:
$productsPrice = $currencies->display_price($products[$i]['final_price'], zen_get_tax_rate($products[$i]['tax_class_id']), $products[$i]['quantity']) . ($products[$i]['onetime_charges'] != 0 ? '<br />' . $currencies->display_price($products[$i]['onetime_charges'], zen_get_tax_rate($products[$i]['tax_class_id']), 1) : '');
just before this line, add the following:
PHP Code:
// MOD ADJUST 'productsPriceEach' IF THERE IS QUANTITY PRICING FOR THIS PRODUCT
$prodQ = $products[$i]['quantity'];
$prodPE = $products[$i]['final_price'];
$prodID = $products[$i]['id'];
if ($prodQ > 1) { // don't bother if quantity == 1
$sql = "select discount_qty as dq from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " where products_id = '" . $prodID . "' order by discount_qty asc";
$dqCheck = $db->Execute($sql);
$count = $dqCheck->RecordCount();
$key = 0;
$dqCheckArray = array();
while (!$dqCheck->EOF) {
$dqCheckArray[$key] = $dqCheck->fields['dq'];
$dqCheck->MoveNext();
$key++;
}
if ($count > 0 && $prodQ >= $dqCheckArray[0]) { // if there are quantity discounts AND if the product quantity is at least as large as the lowest qty discount level
for($x=0;$x<$count;$x++) { // first let's get the base price which applies to this quantity
if ($x < ($count - 1) && $prodPE >= $dqCheckArray[$x] && $prodPE < $dqCheckArray[($x+1)]) { // if the quantity is >= this qty discount level but < the next higher qty discount level
$key = $x;
}
elseif ($x == ($count-1)) { // if the quantity is >= the highest qty discount level, use that one
$key = $x;
}
}
$dqVal = $dqCheckArray[$key]; // select the value for the correct qty disc level in the table
$sql = "select discount_price as dp from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " where products_id = '" . $prodID . "' and discount_qty = '" . $dqVal . "'";
$dp = $db->Execute($sql);
$discPrice = $dp->fields['dp']; // yay, now we have the base price to work with
$sql = "select customers_group_pricing as cgp from " . TABLE_CUSTOMERS . " where customers_id = '" . $_SESSION['customer_id'] . "'";
$cgpid = $db->Execute($sql);
$custGP = $cgpid->fields['cgp'];
$sql = "select group_percentage as gp from ". TABLE_GROUP_PRICING . " where group_id = '" . $custGP . "'";
$cgppct = $db->Execute($sql);
$gpPct = $cgppct->fields['gp']; // yay, now we have the percentage by which to multiply the base price
$products[$i]['final_price'] = $gpPct * $discPrice; // now, was that so hard? ;-)
}
}
// END MOD
store/includes/classes/shopping_cart.php
Now we want to change the 'calculate' function of the class 'shopping cart'. Strangely, the subtotal for the shopping cart is calculated not by adding up the product price * quantity for each item in your basket, at least not using the figures you see individually listed in your shopping cart! Instead it is calculated completely separately! So you need to change the shopping cart class with code which is similar to that above, but not exactly the same.
If you have an group pricing modded shopping_cart.php like me, find line 639:
PHP Code:
if($customers_group)
and on my line 649 is the closing bracket for a series of if... elseif... statements which look like this (again, this is in my file, YMMV):
PHP Code:
if($customers_group == GROUP_PRICE_PER_ITEM1 && $product->fields['products_group_a_price'] != 0) {
$products_price = $product->fields['products_group_a_price'];
} elseif($customers_group == GROUP_PRICE_PER_ITEM2 && $product->fields['products_group_b_price'] != 0) {
$products_price = $product->fields['products_group_b_price'];
} elseif($customers_group == GROUP_PRICE_PER_ITEM3 && $product->fields['products_group_c_price'] != 0) {
$products_price = $product->fields['products_group_c_price'];
} elseif($customers_group == GROUP_PRICE_PER_ITEM4 && $product->fields['products_group_d_price'] != 0) {
$products_price = $product->fields['products_group_d_price'];
}
replace that entire block of code - including the opening 'if' statement and the closing bracket - with this:
PHP Code:
if($customers_group) {
if ($product->fields['products_discount_type'] != '0') {
if ($qty > 1) { // don't bother if quantity == 1
$sql = "select discount_qty as dq from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " where products_id = '" . $prid . "' order by discount_qty asc";
$dqCheck = $db->Execute($sql);
$count = $dqCheck->RecordCount();
$key = 0;
$dqCheckArray = array();
while (!$dqCheck->EOF) {
$dqCheckArray[$key] = $dqCheck->fields['dq'];
$dqCheck->MoveNext();
$key++;
}
if ($count > 0 && $qty >= $dqCheckArray[0]) { // if there are quantity discounts AND if the product quantity is at least as large as the lowest qty discount level
for($x=0;$x<$count;$x++) { // first let's get the base price which applies to this quantity
if ($x < ($count - 1) && $prodPE >= $dqCheckArray[$x] && $prodPE < $dqCheckArray[($x+1)]) { // if the quantity is >= this qty discount level but < the next higher qty discount level
$key = $x;
}
elseif ($x == ($count-1)) { // if the quantity is >= the highest qty discount level, use that one
$key = $x;
}
}
$dqVal = $dqCheckArray[$key]; // select the value for the correct qty disc level in the table
$sql = "select discount_price as dp from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " where products_id = '" . $prid . "' and discount_qty = '" . $dqVal . "'";
$dp = $db->Execute($sql);
$discPrice = $dp->fields['dp']; // yay, now we have the base price to work with
$sql = "select group_percentage as gp from ". TABLE_GROUP_PRICING . " where group_id = '" . $product->fields['products_discount_type'] . "'";
$cgppct = $db->Execute($sql);
$gpPct = $cgppct->fields['gp']; // yay, now we have the percentage by which to multiply the base price
$products_price = $gpPct * $discPrice; // now, was that so hard? ;-)
}
}
}
else {
if($customers_group == GROUP_PRICE_PER_ITEM1 && $product->fields['products_group_a_price'] != 0) {
$products_price = $product->fields['products_group_a_price'];
}
elseif($customers_group == GROUP_PRICE_PER_ITEM2 && $product->fields['products_group_b_price'] != 0) {
$products_price = $product->fields['products_group_b_price'];
}
elseif($customers_group == GROUP_PRICE_PER_ITEM3 && $product->fields['products_group_c_price'] != 0) {
$products_price = $product->fields['products_group_c_price'];
}
elseif($customers_group == GROUP_PRICE_PER_ITEM4 && $product->fields['products_group_d_price'] != 0) {
$products_price = $product->fields['products_group_d_price'];
}
}
}
if you did it right, the line after the last closing curly bracket above should read "// adjusted count for free shipping"
if anyone else finds this useful I would love to know about it. I put in several hours on getting this to work!!!
in bocca al lupo...
Bookmarks