Avatar of sharingsunshine
sharingsunshine
Flag for United States of America asked on

Smarty Foreach Help Needed

I have a need to parse the contents of the cart for a particular productid.  I have found the exact foreach code I am using in a smarty .tpl that is being included above my code in the same .tpl file.  So I am confused why it won't work.

{foreach from=$products item=product name=products}
    {$product.productid}
     {if $product.productid eq 357}
     {include file="promotions/cleanstart_promo.tpl"}
     {/if}
     {/foreach}

Open in new window


When I use this code I get this error:

Invalid argument supplied for foreach()

The table has the key productid.  What am I doing wrong.

PHP

Avatar of undefined
Last Comment
sharingsunshine

8/22/2022 - Mon
babuno5

can you do a var_dump($products) in your php code and show the output?
Sandeep Kothari

it also depends on the content of promotions/cleanstart_promo.tpl .. does it contain any smarty foreach loop ???
sharingsunshine

ASKER
the vardump is

array(4) { [0]=> array(69) { ["cartid"]=> int(1) ["productid"]=> string(3) "226" ["amount"]=> float(5) ["options"]=> NULL ["free_price"]=> string(4) "0.00" ["distribution"]=> string(0) "" ["wishlistid"]=> NULL ["variantid"]=> NULL ["productcode"]=> string(5) "993-5" ["product"]=> string(30) "LBS II Vegi-Tabs (100 tablets)" ["provider"]=> string(8) "1294dahl" ["weight"]=> string(4) "0.20" ["shipping_freight"]=> string(4) "0.00" ["free_shipping"]=> string(1) "N" ["discount_avail"]=> string(1) "Y" ["product_type"]=> string(1) "N" ["price"]=> float(12.5) ["taxes"]=> array(0) { } ["product_options"]=> bool(false) ["discount"]=> int(0) ["coupon_discount"]=> int(0) ["discounted_price"]=> float(62.5) ["subtotal"]=> string(5) "62.50" ["display_price"]=> string(5) "12.50" ["display_discounted_price"]=> float(62.5) ["display_subtotal"]=> string(5) "62.50" ["qv"]=> string(5) "12.50" ["list_price"]=> string(5) "18.75" ["descr"]=> string(30) "(100 tablets), Stock No. 993-5" ["fulldescr"]=> string(5340) "

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
sharingsunshine

ASKER
the content of promotions/cleanstart_promo.tpl consists of javascript and html in it.
sharingsunshine

ASKER
Looking deeper into this.  It appears a better array to use is $cart.  

The debugging console shows the following

{$cart}      Array (26)
max_cartid => 1
products => Array (1)
  0 => Array (26)
    cartid => 1
    productid => "357"

Productd 357 is in the cart.

I tried a section since I wasn't sure if this was considered an associative array

{section name=products loop=$cart}   
  {$cart[products].productid}     // just trying to get it to print out the contents 
if $cart[products].productid eq 357}     
  {include file="rjw/promotions/cleanstart_promo.tpl"}     
  {/if}    
  {/section}

Open in new window


Nothing prints out so it looks like I am not referencing it correctly.  Please tell me how to  parse the cart and then do the if test.

here is the $cart array
array(26) { ["max_cartid"]=> int(1) ["products"]=> array(1) { [0]=> array(26) { ["cartid"]=> int(1) ["productid"]=> string(3) "357" ["amount"]=> float(101) ["options"]=> NULL ["free_price"]=> string(4) "0.00" ["distribution"]=> string(0) "" ["wishlistid"]=> NULL ["variantid"]=> NULL ["productcode"]=> string(6) "3993-8" ["product"]=> string(46) "CleanStart Cleanse / Detox Program - Wildberry" ["provider"]=> string(8) "1294dahl" ["weight"]=> string(5) "10.00" ["shipping_freight"]=> string(4) "0.00" ["free_shipping"]=> string(1) "N" ["discount_avail"]=> string(1) "Y" ["product_type"]=> string(1) "N" ["price"]=> float(45.95) ["taxes"]=> array(0) { } ["product_options"]=> bool(false) ["discount"]=> int(0) ["coupon_discount"]=> int(0) ["discounted_price"]=> float(4640.95) ["subtotal"]=> string(7) "4640.95" ["display_price"]=> string(5) "45.95" ["display_discounted_price"]=> float(4640.95) ["display_subtotal"]=> string(7) "4640.95" } } ["total_cost"]=> float(4660.95) ["shipping_cost"]=> string(5) "20.00" ["taxes"]=> array(0) { } ["tax_cost"]=> string(4) "0.00" ["discount"]=> string(4) "0.00" ["discount_info"]=> string(0) "" ["coupon"]=> string(0) "" ["coupon_discount"]=> string(4) "0.00" ["discount_coupon_info"]=> string(0) "" ["subtotal"]=> string(7) "4640.95" ["display_subtotal"]=> string(7) "4640.95" ["discounted_subtotal"]=> string(7) "4640.95" ["display_shipping_cost"]=> string(5) "20.00" ["display_discounted_subtotal"]=> string(7) "4640.95" ["orders"]=> array(1) { [0]=> array(16) { ["total_cost"]=> string(7) "4660.95" ["shipping_cost"]=> string(5) "20.00" ["taxes"]=> array(0) { } ["tax_cost"]=> string(4) "0.00" ["discount"]=> string(4) "0.00" ["discount_info"]=> string(0) "" ["coupon"]=> string(0) "" ["coupon_discount"]=> string(4) "0.00" ["discount_coupon_info"]=> string(0) "" ["subtotal"]=> string(7) "4640.95" ["display_subtotal"]=> string(7) "4640.95" ["discounted_subtotal"]=> string(7) "4640.95" ["display_shipping_cost"]=> string(5) "20.00" ["display_discounted_subtotal"]=> string(7) "4640.95" ["products"]=> array(1) { [0]=> array(26) { ["cartid"]=> int(1) ["productid"]=> string(3) "357" ["amount"]=> float(101) ["options"]=> NULL ["free_price"]=> string(4) "0.00" ["distribution"]=> string(0) "" ["wishlistid"]=> NULL ["variantid"]=> NULL ["productcode"]=> string(6) "3993-8" ["product"]=> string(46) "CleanStart Cleanse / Detox Program - Wildberry" ["provider"]=> string(8) "1294dahl" ["weight"]=> string(5) "10.00" ["shipping_freight"]=> string(4) "0.00" ["free_shipping"]=> string(1) "N" ["discount_avail"]=> string(1) "Y" ["product_type"]=> string(1) "N" ["price"]=> float(45.95) ["taxes"]=> array(0) { } ["product_options"]=> bool(false) ["discount"]=> int(0) ["coupon_discount"]=> int(0) ["discounted_price"]=> float(4640.95) ["subtotal"]=> string(7) "4640.95" ["display_price"]=> string(5) "45.95" ["display_discounted_price"]=> float(4640.95) ["display_subtotal"]=> string(7) "4640.95" } } ["provider"]=> string(8) "1294dahl" } } ["payment_surcharge"]=> int(0) ["display_cart_products_tax_rates"]=> string(1) "N" ["product_tax_name"]=> string(0) "" ["giftcert_discount"]=> int(0) ["applied_giftcerts"]=> array(0) { } ["shippingid"]=> string(1) "1" ["shipping_warning"]=> NULL ["delivery"]=> string(10) "UPS Ground" ["paymentid"]=> string(2) "20" }

Open in new window


Thanks,

Randal

Sandeep Kothari

have you smarty->assigned cart array .... ??? ...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sharingsunshine

ASKER
Sorry I didn't make myself clearer.  I was on a Smarty page when I was reading the debug console.  In what I have learned it seems, it has been assigned or it wouldn't be in the debug console.

Sandeep Kothari

oh ... in that case it seems you had some smarty error...........looking at your code again I can see a curly brace is missing for if statement... try this...

{section name=products loop=$cart}   
  {$cart[products].productid}     // just trying to get it to print out the contents 
  {if $cart[products].productid eq 357}     
  {include file="rjw/promotions/cleanstart_promo.tpl"}     
  {/if}    
  {/section}

Open in new window

sharingsunshine

ASKER
I don't have any response and I am on the .tpl page where this array detail is in the console.


{$cart}	Array (25)
max_cartid => 1
products => Array (1)
  0 => Array (26)
    cartid => 1
    productid => "357"
    amount => 1
    options => null
    free_price => "0.00"
    distribution => ""
    wishlistid => null
    variantid => null
    productcode => "3993-8"
    product => "CleanStart Cleanse / Detox Program - ..."
    provider => "1294dahl"
    weight => "10.00"
    shipping_freight => "0.00"
    free_shipping => "N"
    discount_avail => "Y"
    product_type => "N"
    price => 45.95
    taxes => Array (0)
    product_options => false
    discount => 0
    coupon_discount => 0
    discounted_price => 45.95
    subtotal => "45.95"
    display_price => "45.95"
    display_discounted_price => 45.95
    display_subtotal => "45.95"
total_cost => 53.45
shipping_cost => "7.50"
taxes => Array (0)
tax_cost => "0.00"
discount => "0.00"
discount_info => ""
coupon => ""
coupon_discount => "0.00"
discount_coupon_info => ""
subtotal => "45.95"
display_subtotal => "45.95"
discounted_subtotal => "45.95"
display_shipping_cost => "7.50"
display_discounted_subtotal => "45.95"
orders => Array (1)
  0 => Array (16)
    total_cost => "53.45"
    shipping_cost => "7.50"
    taxes => Array (0)
    tax_cost => "0.00"
    discount => "0.00"
    discount_info => ""
    coupon => ""
    coupon_discount => "0.00"
    discount_coupon_info => ""
    subtotal => "45.95"
    display_subtotal => "45.95"
    discounted_subtotal => "45.95"
    display_shipping_cost => "7.50"
    display_discounted_subtotal => "45.95"
    products => Array (1)
      0 => Array (26)
        cartid => 1
        productid => "357"
        amount => 1
        options => null
        free_price => "0.00"
        distribution => ""
        wishlistid => null
        variantid => null
        productcode => "3993-8"
        product => "CleanStart Cleanse / Detox Program - ..."
        provider => "1294dahl"
        weight => "10.00"
        shipping_freight => "0.00"
        free_shipping => "N"
        discount_avail => "Y"
        product_type => "N"
        price => 45.95
        taxes => Array (0)
        product_options => false
        discount => 0
        coupon_discount => 0
        discounted_price => 45.95
        subtotal => "45.95"
        display_price => "45.95"
        display_discounted_price => 45.95
        display_subtotal => "45.95"
    provider => "1294dahl"
payment_surcharge => 0
display_cart_products_tax_rates => "N"
product_tax_name => ""
giftcert_discount => 0
applied_giftcerts => Array (0)
shippingid => "1"
shipping_warning => null
delivery => "UPS Ground"

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
babuno5

here you go this should work

{foreach from=$cart.products item=curr_id}
    id: {$curr_id.productid}<br>
{/foreach}
sharingsunshine

ASKER
that got the productid to display but my original question was how to parse the cart for a specific productid say 357.

I have a need to parse the contents of the cart for a particular productid.
ASKER CERTIFIED SOLUTION
babuno5

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sharingsunshine

ASKER
works great, I thought I had to have quotes around the numbers but your code worked.  So I learned something about that too.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.