Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Incrementally number in a Smarty template

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 03-13-2015, 10:28 AM
 
MAPerformance Admin MAPerformance Admin is offline
 

Advanced Member
  
Join Date: Jun 2013
Posts: 44
 

Default Incrementally number in a Smarty template

We are trying to put a conversion script in our order_message.tpl file, but we need to increment a number in the script for each item. I am familiar with {foreach} but do not know how to do this specific task.

*************************
Base Script Example
*************************
<script type="text/javascript">
var productDetailsObj = {ldelim}
{foreach from=$orders.0.products item=prod}
'1' : {ldelim}
'id' : {$prod.productid},
'price': {$prod.price},
'qty': {$prod.amount}
{rdelim},
{/foreach}
{rdelim};
</script>
*************************
End Base Script Example
*************************

See that '1' in the script, directly under the {foreach} statement? That's the number I need to increment. Here's an example of a working script rendered output:

<script type="text/javascript">
var productDetailsObj = {
'1' : {
'id' : 12345,
'price': 49.99,
'qty': 1
},
'2' : {
'id' : 23456,
'price': 19.99,
'qty': 4
},
'3' : {
'id' : 34567,
'price': 109.99,
'qty': 2
},
};
</script>
__________________
X-cart 4.4.3 Gold
Modern Automotive Performance
Reply With Quote
  #2  
Old 03-13-2015, 11:05 AM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default Re: Incrementally number in a Smarty template

I think you can make use of the iteration property. To do this I believe your foreach loop needs a name:
Code:
{foreach from=$orders.0.products item=prod name=products} {$smarty.foreach.products.iteration} {/foreach}

Another idea is to assign a variable and increment it yourself each time:
Code:
{assign var="increment" value=1} {foreach from=$orders.0.products item=prod name=products} Increment: {$increment} {assign var="increment" value=$increment+1} {/foreach}
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote

The following user thanks totaltec for this useful post:
MAPerformance Admin (03-13-2015)
  #3  
Old 03-13-2015, 12:15 PM
 
MAPerformance Admin MAPerformance Admin is offline
 

Advanced Member
  
Join Date: Jun 2013
Posts: 44
 

Default Re: Incrementally number in a Smarty template

Thank you! I tried the second option and it appears to be working well.
__________________
X-cart 4.4.3 Gold
Modern Automotive Performance
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


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

   

 
X-Cart forums © 2001-2020