Undefined array key "children" in recursive call of ps_categorytree.tpl on PHP 8

Open Beginner friendly
#86 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
php
Domain
frontend

Research direction

Start in views/templates/hook/ps_categorytree.tpl, comparing the recursive call around line 34 with the guarded outer call around line 46. Render a page with both leaf and nested categories; done means leaf categories render without PHP 8 undefined-array-key warnings.

Written by the indexing model from the issue text.

Description

Bug description

The recursive categories function in views/templates/hook/ps_categorytree.tpl passes $node.children into its recursive call without a guard. When a node has no children (a leaf category), PHP 8 emits Undefined array key "children" on every render. The outer call site in the same template already uses a guard ({if !empty($categories.children)}), so this looks like a consistency oversight: the fix was applied once but not propagated to the recursive call.

Affected code

views/templates/hook/ps_categorytree.tpl, version 3.0.2 (current master).

{function name="categories" nodes=[] depth=0}
  {strip}
    {if $nodes|count}
      <ul>
        {foreach from=$nodes item=node}
          <li>
            <a href="{$node.link}">{$node.name}</a>
            <div>
              {categories nodes=$node.children depth=$depth+1}   {* line 34, no guard on $node.children *}
            </div>
          </li>
        {/foreach}
      </ul>
    {/if}
  {/strip}
{/function}

<div class="category-tree">
  <ul>
    <li><a href="{$categories.link nofilter}">{$categories.name}</a></li>
    {if !empty($categories.children)}                            {* line 46, guarded here *}
      <li>{categories nodes=$categories.children}</li>
    {/if}
  </ul>
</div>

The outer {if !empty(...)} at line 46 protects the top-level call. The recursive call at line 34 does not.

Steps to reproduce
  1. PS 9.x with PHP 8.1+ and ps_categorytree enabled in any hook position.

  2. Have at least one category tree where some branches have sub-categories and others do not (any non-trivial catalog).

  3. Load any page that renders the widget. Each leaf category iteration emits:

    PHP Warning: Undefined array key "children" in var/cache/prod/smarty/compile/<hash>_ps_categorytree.tpl.php on line 39
    

    The line in the compiled PHP corresponds to the Smarty template line 34.

Expected behavior

Leaf categories should render without warnings, same as the outer guarded branch.

Actual behavior

Every leaf iteration produces a PHP warning in the compiled Smarty template. On our production store with a standard catalog this generates ~50 warnings per day from this widget alone.

Proposed fix

Apply the same guard style already used at the outer call site (line 46) to the recursive call at line 34:

{foreach from=$nodes item=node}
  <li>
    <a href="{$node.link}">{$node.name}</a>
    <div>
      {if !empty($node.children)}
        {categories nodes=$node.children depth=$depth+1}
      {/if}
    </div>
  </li>
{/foreach}

Alternatively, using a Smarty default filter:

{categories nodes=$node.children|default:[] depth=$depth+1}

The first option matches the existing style in the same template.

Environment
  • PrestaShop: 9.1.0
  • PHP: 8.1
  • Module ps_categorytree: 3.0.2
Dominant language
PHP
Stars
6
Forks
29
Avg merge
2d 19h
Merged PRs (30d)
1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.