user319940
user319940

Reputation: 3317

Smarty nested for-each with array

I'm having a little trouble with Smart for-each loops - here's my code:

{* this example will print out all the values of the $custid array *}
{foreach from=$order_info item=amount}
amount: {$amount}<br>
{/foreach}

So basically I'm just trying to loop through and print the "amount" from $order_info - the problem is, it just prints the whole array and not just the "amount".

Here's a reduced number of items from the array that I got from the debug console:

{$order_info}   Array (86)
order_id => "13"
is_parent_order => "N"
parent_order_id => "0"
company_id => "0"
user_id => "1"
total => "1077.87"
subtotal => "1049.87"
discount => "0.00"
subtotal_discount => "0.00"
payment_surcharge => "0.00"
shipping_ids => "1"
shipping_cost => "28.00"
timestamp => "1352380055"
status => "O"
notes => ""
details => ""
promotions => Array (0)
promotion_ids => ""
title => ""
firstname => "John"
lastname => "Doe"
company => ""
b_title => ""
b_firstname => "John"
b_lastname => "Doe"
b_address => "44 Main street"
b_address_2 => "test"
b_city => "Boston"
b_county => ""
b_state => "MA"
b_country => "US"
b_zipcode => "02134"
b_phone => ""
s_title => ""
s_firstname => "John"
s_lastname => "Doe"
s_address => "44 Main street"
s_address_2 => "test"
s_city => "Boston"
s_county => ""
s_state => "MA"
s_country => "US"
s_zipcode => "02134"
s_phone => ""
phone => ""
fax => ""
url => ""
email => "[email protected]"
payment_id => "3"
tax_exempt => "N"
lang_code => "EN"
ip_address => "62.49.144.33"
repaid => "0"
validation_code => ""
localization_id => "0"
payment_method => Array (15)
  payment_id => "3"
  usergroup_ids => "0"
  position => "30"
  status => "A"
  template => "check.tpl"
  processor_id => "0"
  params => ""
  a_surcharge => "0.000"
  p_surcharge => "0.000"
  localization => ""
  payment => "Check"
  description => "Check payment"
  instructions => "Type instructions to pay by visiting ..."
  lang_code => "EN"
  processor => ""
fields => Array (0)
items => Array (2)
  2902390881 => Array (19)
    item_id => "2902390881"
    order_id => "13"
    product_id => "1110"
    product_code => "B0002I9OJS"
    price => "549.99"
    amount => "1"
    extra => Array (9)
      step => "1"
      product_options => Array (0)
      unlimited_download => "N"
      product => "eMachines T2958 Desktop PC (2.66 GHz ..."
      company_id => "0"
      is_edp => "N"
      edp_shipping => "N"
      base_price => "549.99"
      stored_price => "N"
    product => "eMachines T2958 Desktop PC (2.66 GHz ..."
    deleted_product => ""
    discount => "0"
    company_id => "0"
    base_price => "549.99"
    original_price => "549.99"
    cart_id => "2902390881"
    tax_value => "0"
    subtotal => "549.99"
    display_subtotal => "549.99"
    shipped_amount => "0"
    shipment_amount => "1"
  2278796194 => Array (19)
    item_id => "2278796194"
    order_id => "13"
    product_id => "1120"
    product_code => "B00086HZJA"
    price => "499.88"
    amount => "1"
    extra => Array (9)
      step => "1"
      product_options => Array (0)
      unlimited_download => "N"
      product => "HP Pavilion a1000n Desktop PC (Intel ..."
      company_id => "0"
      is_edp => "N"
      edp_shipping => "N"
      base_price => "499.88"
      stored_price => "N"
    product => "HP Pavilion a1000n Desktop PC (Intel ..."
    deleted_product => ""
    discount => "0"
    company_id => "0"
    base_price => "499.88"
    original_price => "499.88"
    cart_id => "2278796194"
    tax_value => "0"
    subtotal => "499.88"
    display_subtotal => "499.88"
    shipped_amount => "0"
    shipment_amount => "1"

So what I'm really trying to do is get the amount from items within $order_info - I tried $order_info.items but this caused the site to crash.

Any ideas are much appreciated.

Upvotes: 4

Views: 1012

Answers (2)

user319940
user319940

Reputation: 3317

This solved it:

{foreach from=$order_info.items item=foo} 
     {$foo.product_id} 
{/foreach}

Upvotes: 3

pp19dd
pp19dd

Reputation: 3633

Wouldn't it be something like

{foreach from=$order_info.items item=items}
    {foreach from=$items item=item}
        amount: {$item.amount}<br>
    {/foreach}
{/foreach}

Assuming I understood your structure as $order_info -> items array -> (item item item item), and each item having an .amount property.

Upvotes: 1

Related Questions