X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Speed up Smarty templates by taking out {math} functions (https://forum.x-cart.com/showthread.php?t=11584)

NuAlpha 01-19-2005 07:34 PM

Speed up Smarty templates by taking out {math} functions
 
To get more speed out of your templates, remove the {math} function from your product.tpl template (and others) to PHP. I will cover product.tpl, but any other template that uses {math} will also benefit if you do likewise (such as products.tpl or products_t.tpl). The {math} function is eval()'d each time it is called and is extremely slow. Some usages of the {math} function can be over 450% slower than doing the same in native PHP code.

Find the function func_select_product in func.php and add this to the $product = func_query_first( SQL statement to replace the 'savings' and 'discount' math calculations:
Code:

100-($sql_tbl[pricing].price/$sql_tbl[products].list_price)*100 AS discount, $sql_tbl[products].list_price-$sql_tbl[pricing].price AS savings,

Below the select statement, add the following to replace the 'mq' math calculation:
Code:

if ($product) {
        if ($config['General']['unlimited_products'] == 'Y')
                $product['min_quantity'] = $config['Appearance']['max_select_quantity']+1;
        else {
                $tmp_compare = $config['Appearance']['max_select_quantity']/$product['min_amount'];
                if ($tmp_compare < 2)
                        $minamount = $product['min_amount'];
                else
                        $minamount = 0;
                $product['min_quantity'] = min($config['Appearance']['max_select_quantity']+$minamount, $product['avail'])+1;
        }
}


In product.tpl:
Find your $discount and change it to:$product.discount|string_format:'%d'

Find $savings and change it to: $product.savings|string_format:'%f'

Find the {section} loop that uses $mq and change it to: $product.min_quantity

Lastly, go back and comment out all of the code in product.tpl that originally calculated these values by enclosing them in {* and *}.

If everything looks as it should when you load a product, then you can delete that template code you commented out if you'd like.

I cannot remember if there are any other {math} functions in product.tpl, as that was one speed issue I jumped at early on.

If anyone feels like posting their success with this or other templates and how you did it, don't hesitate to let everyone know. I'd love to be able to post steps for the rest of the templates I know but current time constraints won't allow for it. :wink:

balinor 01-20-2005 04:18 AM

Oooooo....more speed! Thanks as always for these useful tips NuAlpha! Before long you will have X-Cart loading as quickly as an html site! ;)

NuAlpha 01-20-2005 08:33 AM

If anyone has already applied this code, please note that I forgot to paste a critical part and I had to update it to encase the min_quantity assignment in if ($product) {. :oops:

If you don't do this then $product will always evaluate to true in subsequent IF conditionals regardless of whether a product was actually found.

NuAlpha 01-21-2005 09:53 AM

Here is another minor improvement, though I don't know if many will get much use out of this one.

Change the contents of skin1/customer/main/alter_currency_value.tpl to the following to eliminate the {math} function:
Code:

{if $alter_currency_value eq ''}{assign var='alter_currency_value' value=0}{/if}{if $config.General.alter_currency_symbol ne ''} ({$config.General.alter_currency_symbol} {assign var='alter_price' value=$alter_currency_value*$config.General.alter_currency_rate}{$alter_price|formatprice:',':'.'|string_format:'%.2f'}){/if}

A more useful modification might be in skin1/customer/main/cart.tpl where you can also replace:
Code:

{math equation="price*amount" price=$price amount=$products[product].amount format="%.2f" assign=unformatted}{include file='currency.tpl' value=$unformatted}

With:
Code:

{assign var='unformatted' value=$price*$products[product].amount}{include file='currency.tpl' value=$unformatted|string_format:'%.2f'}

Note that these only works if you have Smarty 2.6.5 or later installed.

Dybbuk 03-02-2005 05:45 AM

Re: Speed up Smarty templates by taking out {math} functions
 
Quote:

Originally Posted by NuAlpha
Find the function func_select_product in func.php and add this to the $product = func_query_first( SQL statement to replace the 'savings' and 'discount' math calculations:
Code:

100-($sql_tbl[pricing].price/$sql_tbl[products].list_price)*100 AS discount, $sql_tbl[products].list_price-$sql_tbl[pricing].price AS savings,
Below the select statement, add the following to replace the 'mq' math calculation:
Code:

if ($product) {
        if ($config['General']['unlimited_products'] == 'Y')
                $product['min_quantity'] = $config['Appearance']['max_select_quantity']+1;
        else {
                $tmp_compare = $config['Appearance']['max_select_quantity']/$product['min_amount'];
                if ($tmp_compare < 2)
                        $minamount = $product['min_amount'];
                else
                        $minamount = 0;
                $product['min_quantity'] = min($config['Appearance']['max_select_quantity']+$minamount, $product['avail'])+1;
        }
}



Does this work for 4.0.11? Bit confused. In my func.php I have the following:

Code:

$product = func_query_first("SELECT $sql_tbl[products].*, $sql_tbl[products].avail-$in_cart AS avail, min($sql_tbl[pricing].price) AS price $add_fields FROM $sql_tbl[products], $sql_tbl[pricing] $join WHERE $sql_tbl[products].productid='$id' ".$login_condition." AND $sql_tbl[pricing].productid=$sql_tbl[products].productid AND $sql_tbl[pricing].quantity=1 AND $sql_tbl[pricing].variantid = 0 AND ($sql_tbl[pricing].membership = '$membership' OR $sql_tbl[pricing].membership = '') GROUP BY $sql_tbl[products].productid");

I'm not exactly sure where to add NuAlpha's code (ie., after the SELECT statement, before, in the middle, as a replacement...)

Could somebody post an example of what the finished snippet of code in func.php should look like? Not being cheeky, I'm just uncertain how to proceed with this correctly.

Cheers,

DB

NuAlpha 03-02-2005 09:44 AM

This was designed with 3.5.14 in mind. We are working on the upgrade to 4.0.x now that it has been declared "stable" and hope to have all of these changes in place in time for 4.0.13 or 4.0.14 at the latest.

I will post that code modified code when we are done with the transition if someone else doesn't post it before me. If you don't see something here by mid April, make a post here and I will drop by to see what I can do.

Dybbuk 03-02-2005 04:04 PM

Thanks, Nu.

I will definitely be interested in the branch 4.x adaption of this mod - looks very nice, kudos.

Regards,

DB

andyng 03-02-2005 07:05 PM

Speed up Smarty templates by taking out {math} functions
 
Hi NuAlpha,

Did you check your mod can work in X-cart 4.7.0 or above? If yes, would mind to give more details how to make it work since I cannot follow you from your previous posts as the func.php is a very long list.

Thanks and regards,

Andy
X-Cart 4.7.0

NuAlpha 03-03-2005 09:58 AM

Re: Speed up Smarty templates by taking out {math} functions
 
Quote:

Originally Posted by andyng
Hi NuAlpha,

Did you check your mod can work in X-cart 4.7.0 or above? If yes, would mind to give more details how to make it work since I cannot follow you from your previous posts as the func.php is a very long list.

Thanks and regards,

Andy
X-Cart 4.7.0


Don't you mean 4.0.7 instead of 4.7.0? :D

I have only tested this on 3.5.14 so far.

andyng 03-03-2005 06:06 PM

Speed up Smarty templates by taking out {math} functions
 
Hi NuAlpha,

I am so sorry I have the typo error of my X-cart version.

It should be V.4.0.7 instead of v.4.7.0

Regards,

Andy


All times are GMT -8. The time now is 03:18 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.