Jump to content

Smarty {assign} within foreach loop not works


Dev Franco
 Share

Recommended Posts

Hi,

 

I am trying to group the products in cart summary page by suppliers. Within the {foreach $products as $product}{} loop, I am trying to assign a product value to a smarty variable. But when I am printing the value inside the loop, it is showing the default value which I have set before the loop. Please see the example below:

 

{assign var='sellerid' value=55}
{foreach $products as $product}
   {$sellerid|@print_r}
   {assign var='sellerid' value= $product.sellerid}
{/foreach }
 
It prints 55 1 55 1 55 1 55 1 55 1
 
I don't know from where this 1 comes. But the 55 is not changing. What is wrong here. Please help me. :(

Share this post


Link to post
Share on other sites

I would try something like this:

 

{$sellerid = 55}
{foreach $products as $product}
    {$sellerid = $product['id_supplier']}
   {$sellerid}
{/foreach }
 
If you want to assign other value than id_supplier use {$product|@print_r} and look for needed value.
 
And I guess that you should do something like this in controllers and module php files.
In tpl files you're only displaying content.

Share this post


Link to post
Share on other sites

HI Szkrin,

 

Thanks for the response. I have tried as you suggested.

 

{$sellerid = 55}

{foreach $products as $product}
    {$sellerid = $product['id_supplier']}
   {$sellerid}
{/foreach }

But still the result is same.

Unfortuantely, I have to do it in the tpl file. I think it should be possible to do something like this in template files also. Do you have nay suggestion? :(

Share this post


Link to post
Share on other sites

Hi Szkrin,

 

I have checked with different supplier products. No change.

What happening is,

{$var = 0}

{foreach $products as $product}

       {$var}  // prints 0 always

       {$var = 1}  // prints 1

{/foreach}

 

On each iteration, the value of $var is assigned as 0(default value set outside the loop). This is really weird. :(. Do you have any idea?

Edited by Dev Franco (see edit history)

Share this post


Link to post
Share on other sites

I added 3 products to cart, pasted your code into shopping-cart.tpl and it prints 0 1 1  so your code is working :)

Sadly I have no other idea why you're reciving 0 on each iteration. You can try to force compile and clear cache but I don't think that it will help.

Maybe somebody else will find solution for you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More