0

我需要在产品详细信息页面(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 文件中调用

有人帮忙吗?

4

1 回答 1

0

最好的方法是在 ProductController 中进行。首先,您需要覆盖它。为此创建(或使用existant)/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}

于 2013-06-03T14:52:26.460 回答