Jump to content

var outside {foreach} loop, incremented inside {foreach} if id_category is 437


Lucagiaicheca

Recommended Posts

Hi, 

 

I'm new to smarty so here's my question.

 

In the cart I need to check if each product category equals a certain product category, and if it does then increment +1 a variable.

 

here's my shopping-cart.tpl. My var is called "pippo"

{assign var='pippo' value=0}

{foreach $products as $product}
{assign var='productId' value=$product.id_product}
{assign var='productAttributeId' value=$product.id_product_attribute}
{assign var='quantityDisplayed' value=0}
{assign var='odd' value=$product@iteration%2}
{assign var='ignoreProductLast' value=isset($customizedDatas.$productId.$productAttributeId) || count($gift_products)}
{* Display the product line *}
{include file="./shopping-cart-product-line.tpl" productLast=$product@last productFirst=$product@first}
{* Then the customized datas ones*}
{if isset($customizedDatas.$productId.$productAttributeId)}
{foreach $customizedDatas.$productId.$productAttributeId[$product.id_address_delivery] as $id_customization=>$customization}

As you can see the shopping-cart.tpl includes shopping-cart-product-line.tpl, where each product get read and displayed.

 

Inside shopping-cart-product-line.tpl I put my increment (I get product category and if it equals to the one I'm interested to, I want to add 1 to $pippo (thanks to Mr. Milosz for his tutorial on how to add product category in cart)

{foreach from=Product::getProductCategoriesFull($product.id_product) item=cat} {/foreach}
{if $cat.id_category==437}
{math equation='x+1' x=$pippo assign="pippo"} 
{/if}

The loop works a charme, and it actually increments by 1 $pippo if   $cat.id_category==437. But the next line $pippo=0 once again. $pippo seems to reset on each loop. Why?

 

My var is assigned=0 outside the {foreach} cycle, so $pippo should increment by one, and then keep the new value, not the old one...

 

HELP PLEASE :-)

 

Link to comment
Share on other sites

Dear Tomerg3,

 

thank you so much for help. I actually checked my smarty version is 3, so you are right. 

 

The variable keeps on getting zero. In the image of my cart, close to the product image I put {$pippo} to let me see if it increments. As you can see, on the products there's 1 instead of 0, this means that {if} works and recognizes the category and adds 1 to $pippo. But the next line $pippo is 1 again, even if I have 2 products with the right product category. 

 

Above all, when I go back to shopping-cart.tpl and I make it print {$pippo}, it always equals 0 (you can see at the bottom of the page).

 

Sob. Any idea?

 

Thank you so much. :-)pippo.jpg

Link to comment
Share on other sites

I tried, but with no success  :wacko: 

 

I really don't know whom I could ask for help... Tomerg do you know somebody who knows prestashop and smarty (paying) that could help me pass through this issue?

 

It's really weird, isn't it?  :huh:

 

Thank you tomerg3 for your time and help...  :)

Link to comment
Share on other sites

  • 10 months later...

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
×
×
  • Create New...