如何调用函数并在prestashop中显示来自tpl文件的结果

如何调用函数并在prestashop中显示来自tpl文件的结果,prestashop,Prestashop,我需要在产品详细信息页面(product.tpl页面)中显示产品收藏夹计数和一些其他功能 但是我对prestashop是新手,所以我找不到我声明函数的位置,也不知道如何从tpl文件调用函数 我在这里写产品最喜欢的计数代码 public function favcount($id_product) { $sql = 'SELECT count(*) FROM `ps_favorite_product` WHERE `id_product`='.(int)$id_pro

我需要在产品详细信息页面(product.tpl页面)中显示产品收藏夹计数和一些其他功能

但是我对prestashop是新手,所以我找不到我声明函数的位置,也不知道如何从tpl文件调用函数

我在这里写产品最喜欢的计数代码

public function favcount($id_product)
    {

         $sql = 'SELECT count(*) FROM `ps_favorite_product` WHERE  `id_product`='.(int)$id_product.;


        $result = Db::getInstance()->getRow($sql);
        return  $result['count'];
    }
在哪个位置可以插入上述代码以及如何从product.tpl文件调用


有任何帮助吗?

最好的方法是在ProductController中进行。首先,您需要覆盖它。为此,创建(或使用现有的)
/override/controllers/front/ProductController.php
。您可以在
initContent()
方法中使用函数:

/**
 * @see ProductController::initContent()
 */
public function initContent() {
    parent::initContent();

    // you can place your favcount method inside the class and use it
    $favCount = $this->favcount($this->product->id);
    // then assign the variable to the template
    $this->context->smarty->assign('favcount', $favCount);
}
然后在模板中,可以使用以下命令调用变量:
{$favcount}