Search code examples
phpcachingsmartyprestashop

Check if product has specific tag in Prestashop 1.6.1.4


I'd like to check if a product has a certain tag. If it does I'd like to display some text. Below is the sample of what I've done. It works. The only problem is I get errors.

 {if in_array('rent', $product->tags.1)}
      <img id="turnKeyimg" alt="TurnKey Rental Option" src="{$tpl_uri}img/key.png"/>
      <h3>TurnKey Rental Option</h3>
      <p>Also available for immediate rental.<br />Request a quote today</p>
 {/if}

The error log have entries like this:

Warning: in_array() expects parameter 2 to be array, null given in /cache/smarty/compile/94/4d/52/944d5284e871d0de7a0c6b84ebb2089ad579ed8b.file.product.tpl.php on line 330

Line 330 in the cache looks like this:

<?php if (in_array('rent',$_smarty_tpl->tpl_vars['product']->value->tags[1])) {?> 
     <img id="turnKeyimg" alt="TurnKey Rental Option" 
     src="<?php echo $_smarty_tpl->tpl_vars['tpl_uri']->value;
?>

What did I do wrong to cause these errors?


Solution

  • To fix the error messages I added a check if an array existed. No more errors.

     {if (isset($product->tags) && $product->tags)}
        {if in_array('rent', $product->tags.1)}
          <img id="turnKeyimg" alt="TurnKey Rental Option" src="{$tpl_uri}img/key.png"/>
          <h3>TurnKey Rental Option</h3>
          <p>Also available for immediate rental.<br />Request a quote today</p>
        {/if}
    {/if}