New Zenner
- Join Date:
- May 2009
- Posts:
- 4
- Plugin Contributions:
- 0
Adding Length Width Price Calculations to my cart?
I recently had a project where the product price was calculated as a result of the combination of length and width of the product, and I thought I'd share with other users how I did it (and hopefully save them some work).
**Note: The code works, but it isn't quite as tight as I'd like. Thought I'd leave something for other users to work on.
Step 1: Edit the database_tables.php file, adding this line to the end:
define('TABLE_PRODUCT_OPTIONS_LOOKUP', DB_PREFIX . 'products_options_values_to_products_options');
Step 2: Edit the shopping_cart.php file by adding this code right beneath the following highlighted in red:
$attribute_price_query = "select *
from " . TABLE_PRODUCTS_ATTRIBUTES . "
where products_id = '" . (int)$prid . "'
and options_id = '" . (int)$option . "'
and options_values_id = '" . (int)$value . "'";
$attribute_price = $db->Execute($attribute_price_query);
Code to add below:
// Below Methods to Calculate Price of Product Based on Length-Width pairs.
// Pull the value of the Length Id from the Database.
$Length_ID_Query = "select products_options_id from " . TABLE_PRODUCTS_OPTIONS . " where products_options_name = 'Length'";
$Length_ID = $db->Execute($Length_ID_Query);
$LengthID = (int)$Length_ID->fields['products_options_id'];
// Now Compare Length ID to Products ID, and if they are equal calculate Length_Factor
if ((int)$option == (int)$LengthID) {
$Length_Factor_Query = "select products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " WHERE products_options_values_id = '" . (int)$value . "'";
$Length_Factor = $db->Execute($Length_Factor_Query);
$LengthFactor = (float)$Length_Factor->fields['products_options_values_name'];
}
// Pull the value of the Width Id from the Database.
$Width_ID_Query = "select products_options_id from " . TABLE_PRODUCTS_OPTIONS . " where products_options_name = 'Width'";
$Width_ID = $db->Execute($Width_ID_Query);
$WidthID = (int)$Width_ID->fields['products_options_id'];
// Now Compare Width ID to Products ID, and if they are equal calculate Width_Factor
if ((int)$option == (int)$WidthID) {
$Width_Factor_Query = "select products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " WHERE products_options_values_id = '" . (int)$value . "'";
$Width_Factor = $db->Execute($Width_Factor_Query);
$WidthFactor = (float)$Width_Factor->fields['products_options_values_name'];
}
if ($WidthFactor > 0 AND $LengthFactor > 0 AND $products_price > 0) {
$base_price = $products_price * $WidthFactor * $LengthFactor;
$this->total += ($base_price-$products_price) * $qty;
$this->weight += ($products_weight * $WidthFactor * $LengthFactor * $qty)-($products_weight * $qty);
// Reset Length Factor, Width Factor
$LengthFactor = 0;
$WidthFactor = 0;
}
At this point the totals in your cart will reflect the Length X Width X Product Price for all products that are priced by attribute.
**Note: You HAVE to name the Options "Length" and "Width", and their values should be numeric. Items that are not priced by attribute will price normally. You do not (should not) need to add in any 1 time or price factors for the Length/Width Values. The calculation is based on the Option Values themselves.
If you don't wish to name your options Length and Width, name them what you'd like but be sure to edit the appropriate lines above.
Now you probably noticed that the Units and Totals Columns above the products still only show the unadjusted Price before the length width Calculation.
To remedy this we're going to place some similar code again here (right beneath the following highlighted in red):
Step 3:
$attribute_price_query = "select *
from " . TABLE_PRODUCTS_ATTRIBUTES . "
where products_id = '" . (int)$products_id . "'
and options_id = '" . (int)$option . "'
and options_values_id = '" . (int)$value . "'";
$attribute_price = $db->Execute($attribute_price_query);
// Below Methods to Calculate Price of Product Based on Length-Width pairs.
// Pull the value of the Length Id from the Database.
$product_query = "select products_price, products_weight,
products_priced_by_attribute
from " . TABLE_PRODUCTS . "
where products_id = '" . (int)$products_id . "'";
if ($product = $db->Execute($product_query)) {
$products_price = $product->fields['products_price'];
$products_weight = $product->fields['products_weight'];
}
$Length_ID_Query = "select products_options_id from " . TABLE_PRODUCTS_OPTIONS . " where products_options_name = 'Length'";
$Length_ID = $db->Execute($Length_ID_Query);
$LengthID = (int)$Length_ID->fields['products_options_id'];
// Now Compare Length ID to Products ID, and if they are equal calculate Length_Factor
if ((int)$option == (int)$LengthID) {
$Length_Factor_Query = "select products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " WHERE products_options_values_id = '" . (int)$value . "'";
$Length_Factor = $db->Execute($Length_Factor_Query);
$LengthFactor = (float)$Length_Factor->fields['products_options_values_name'];
}
// Pull the value of the Width Id from the Database.
$Width_ID_Query = "select products_options_id from " . TABLE_PRODUCTS_OPTIONS . " where products_options_name = 'Width'";
$Width_ID = $db->Execute($Width_ID_Query);
$WidthID = (int)$Width_ID->fields['products_options_id'];
// Now Compare Width ID to Products ID, and if they are equal calculate Width_Factor
if ((int)$option == (int)$WidthID) {
$Width_Factor_Query = "select products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " WHERE products_options_values_id = '" . (int)$value . "'";
$Width_Factor = $db->Execute($Width_Factor_Query);
$WidthFactor = (float)$Width_Factor->fields['products_options_values_name'];
}
if ($WidthFactor > 0 AND $LengthFactor > 0) {
$base_price = $products_price * $WidthFactor * $LengthFactor;
$attribPrice = 0;
$attribPrice = $base_price - $products_price;
// Reset Length Factor, Width Factor
$LengthFactor = 0;
$WidthFactor = 0;
}
$attributes_price += $attribPrice;
And that's it. Now, for all products priced by attribute and with option values of Length and Width the price will be calculated and displayed as the product of them both.
Good Luck.