Zen Cart Logo
Forums / Addon Templates / Adding Length Width Price Calculations to my cart?

Adding Length Width Price Calculations to my cart?

Views: 8,305

Results 1 to 19 of 19
28 May 2009, 2:21 AM
#1
rboyle avatar

rboyle

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.

18 Sep 2009, 10:27 PM
#2
j0h0 avatar

j0h0

New Zenner

Join Date:
Sep 2009
Posts:
5
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

Can you please make a guide on how to set up this in admin?
And the "red" code is found 3 places in shopping_cart.php... so please explain better where to paste the code.

19 Sep 2009, 4:57 AM
#3
rboyle avatar

rboyle

New Zenner

Join Date:
May 2009
Posts:
4
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

Hi JOHO,

Sorry I wasn't clear. You use the blue code beneath the red code in the first 2 instances it appears, you can ignore the third instance (I did). When adding products ensure you select "Priced by Attribute".
Make a backup copy copy of your shopping_cart.php file (always, always back your files up), then replace your old shopping_cart.php file with the one you've modified below.
Ensure that you name the attributes "Length" and "Width".
I would post the file I used, but it was somewhat more advanced and had a few features I don't think other users would find useful.

If you have any problems please let me know.

Thanks
Rod

19 Sep 2009, 8:43 AM
#4
j0h0 avatar

j0h0

New Zenner

Join Date:
Sep 2009
Posts:
5
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

I have done what you said, but it does not work. I attach a screenshot from my configuration in admin.

20 Sep 2009, 5:17 PM
#5
rboyle avatar

rboyle

New Zenner

Join Date:
May 2009
Posts:
4
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

Hi JOHO,

None of the attribute configurations should make a difference. The price is determined as a result of the base price stated on the product itself - ie: You have a product XYZ that you enter into your catalogue. You've set a base price of $3.00, and selected "Priced by Attribute". You assign it a Length and Width attribute option value, with appropriate option value names (ie: 2, 3, 4, ).
Note that it depends on the Option Names being "Length" and "Width" respectively. You do not need to configure or add any additional one-time prices in the attributes.
The updated shopping_cart.php file will calculate the price of the product based on Length X Width X Base Price.
What it does is look for fields with the name Length or Width and recalculate the value accordingly.

If you continue to have problems, send me an email with a copy of your shopping_cart.php file attached and I'll look at it.

Thanks,
Rod

20 Sep 2009, 6:13 PM
#6
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Adding Length Width Price Calculations to my cart?

I see you have made Length and Width "text" types.
From the OP:
"name the Options "Length" and "Width", and their values should be numeric."
Is this relevant? I don't believe text attributes take option values, just user input.

9 Mar 2010, 10:54 AM
#7
jamesw avatar

jamesw

New Zenner

Join Date:
Mar 2010
Posts:
1
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

Hi rboyle,

I have tried to follow your steps stated above but have found that whenever i edit the 'shopping_cart.php' file my admin area throws errors.

as a test i downloaded the 'shopping_cart.php' file from my server, added a space after the red code and uploaded it again and the error occured. Do i have to edit the file in some other way?

Is this something you have come across before?

thanks in advance

17 Jan 2011, 4:55 PM
#8
eentje avatar

eentje

Zen Follower

Join Date:
Jan 2009
Location:
The Netherlands/Germany
Posts:
144
Plugin Contributions:
4

Re: Adding Length Width Price Calculations to my cart?

gjh42:

I see you have made Length and Width "text" types.
From the OP:
"name the Options "Length" and "Width", and their values should be numeric."
Is this relevant? I don't believe text attributes take option values, just user input.

Very late response but yes it is relevant... above sugestion does not work with text input only with dropdown (tested) or radiobuttons ( i imagine ).

Too bad... textinput would be what most ppl are looking for.

30 Jan 2011, 4:34 AM
#9
Join Date:
Mar 2010
Posts:
9
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

Hello, i've tried to work with this and I'm just lost. (v1.3.9h)

I'm trying to set it up so that users can pick a dropdown of 'width' and 'height' (I changed it from length) to calculate the price, but its not coming up correctly for me.

Been at it for 6 hours now and I'm fading quick.

this is my 7th Zen cart installation and I've used PHP for years, but this piece of code is a bit over my head.

I've attached the shopping cart file and the pricing chart that I'm trying to use for review and the URL for the site is here:

http://arlingtonglassblock.com
Specifically, use this product to view:
http://arlingtonglassblock.com/index.php?main_page=product_info&cPath=2&products_id=4

If you notice, all the 'options' have 'text descriptions' in the options panel. This is a must to allow people to select the correct size of their glass block.

However, I've changed 1 width (value '6') and 1 height (value '5') to test this program out and its not working.

anyone have any clues? I'm going out to get a drink now and clear my head!
(yes, I'd be willing to pay for assistance on this issue too, if that's a motivator). :D

Thanks, Patrick

PS, I've tried using the Attributes Controller with Price Factoring and it just didn't work out (I tried 10 different combinations and charted them in excel to keep track. This would be so cool the products attribute controller worked that way!)

30 Jan 2011, 11:26 AM
#10
eentje avatar

eentje

Zen Follower

Join Date:
Jan 2009
Location:
The Netherlands/Germany
Posts:
144
Plugin Contributions:
4

Re: Adding Length Width Price Calculations to my cart?

It doesn't work in 1.3.9.

It works in 1.3.8 though...

30 Jan 2011, 4:39 PM
#11
Join Date:
Mar 2010
Posts:
9
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

Okay, thanks for the heads up. I guess I need to hire someone to make this work.

Any takers?

I've restored my shopping_cart.php file back to the original format.

What would be so cool is if in the Attributes Controller page, in modifying one of the option's attributes, we could use * instead of + next to the Price in the "Prices and Weights" section.

That would (I thought) solve everything.

I even tried using "Price Factor" (which multiplies the price just fine, but doesn't multiply the option against its other option (in this case, width x height).

Am I missing something here, or does this genuinely not happen?

30 Jan 2011, 7:32 PM
#12
eentje avatar

eentje

Zen Follower

Join Date:
Jan 2009
Location:
The Netherlands/Germany
Posts:
144
Plugin Contributions:
4

Re: Adding Length Width Price Calculations to my cart?

You can post it in the commercial help section...

If you get someone who can do it i'll chip in... would have use for it myself.

30 Jan 2011, 8:01 PM
#13
kcb410 avatar

kcb410

Zen Follower

Join Date:
Dec 2008
Location:
Toronto
Posts:
348
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

eentje:

You can post it in the commercial help section...

If you get someone who can do it i'll chip in... would have use for it myself.

Hi Guys,

I would also chip in for this if the costs are reasonable.

Let me know.

30 Jan 2011, 9:32 PM
#14
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Location:
Amersfoort, The Netherlands
Posts:
2,862
Plugin Contributions:
5

Re: Adding Length Width Price Calculations to my cart?

eentje:

You can post it in the commercial help section...

If you get someone who can do it i'll chip in... would have use for it myself.

Me to, I have tried several things without luck, so let's split those costs :clap:

30 Jan 2011, 9:54 PM
#15
eentje avatar

eentje

Zen Follower

Join Date:
Jan 2009
Location:
The Netherlands/Germany
Posts:
144
Plugin Contributions:
4

Re: Adding Length Width Price Calculations to my cart?

@Design75

Je kan hem van mij kopen als het werkt... :P

30 Jan 2011, 10:50 PM
#16
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Location:
Amersfoort, The Netherlands
Posts:
2,862
Plugin Contributions:
5

Re: Adding Length Width Price Calculations to my cart?

eentje:

@Design75

Je kan hem van mij kopen als het werkt... :P

:laugh::laugh:

30 Jan 2011, 11:23 PM
#17
Join Date:
Mar 2010
Posts:
9
Plugin Contributions:
0

Re: Adding Length Width Price Calculations to my cart?

I don't speak Dutch, but I'm up for splitting the cost as well! :clap: