【问题标题】:How do you call a helper function in the category.tpl file OpenCart 2如何在 category.tpl 文件 OpenCart 2 中调用辅助函数
【发布时间】:2025-11-27 23:50:01
【问题描述】:

这是我之前的问题:How to convert a decimal $attribute['text']; into a fraction in opencart

我有一个在startup.php 中声明并在helper/dec2frac.php 中定义的辅助函数

我正在尝试使用以下代码从 category.tpl 文件调用辅助函数:

  <?php if ($product['attribute_groups']) { ?>
                   <?php foreach ($product['attribute_groups'] as $attribute_group) { ?>
                       <?php foreach ($attribute_group['attribute'] as $attribute) { ?>
                        <?php /*var_dump($attribute);*/

                       if($attribute['name'] == "Adjuster Position")
                       {
                          //echo("<h1>HELLLO</h1>");
                          dec2frac($attribute['text']);
                       }


                        ?>

...但我收到此错误消息:

致命错误:在 startup.php 中调用未定义函数 dec2frac() 助手/dec2frac.php

如何在category.tpl 文件中调用我的辅助函数? 我需要在我的category.php 文件中引用辅助函数吗?

【问题讨论】:

    标签: php opencart opencart2.x opencart2.3


    【解决方案1】:

    我正在尝试以不同的方式实现相同的目标。以下链接会有所帮助,它适用于 opencart v2.3x

    Not able to find my custom object in Registry - OpenCart-v2.3.0.2

    试试上面的链接(这是我发布的问题)。创建一个对象并将其保存在注册表中,然后从注册表中获取它并调用所需的函数。

    在您的 category.php 文件中,从注册表中获取您的对象。例如:

    $kt = $registry->get('ktLibrary'); //Object
    $value = $kt->getSomeValue(); //function
    

    object $kt 将在 category.tpl 文件中可用。

    【讨论】: