Jump to content

Recommended Posts

Posted (edited)
//This is my smarty template file i.e list.tpl

{* {include file='layouts/layout-full-width.tpl'} *}
{extends file='page.tpl'}

{block name='page_header_container'}
	<h1>
		{l s='Comments on product' mod='mymodcomments'}
		"{$product->name}"
	</h1>
{/block}
{block name='page_content'}
<div class="rte">
	{foreach from=$comments item=comment}
		<div class="mymodcomments-comment">
			<img src="http://www.gravatar.com/avatar/{$comment.email|trim|strtolower|md5}?s=45"
				class="pull-left img-thumbnail mymodcomments-avatar" />
			<div>{$comment.firstname} {$comment.lastname|substr:0:1}. <small>{$comment.date_add|substr:0:10}</small></div>
			<div class="star-rating"><i class="glyphicon glyphicon-star"></i>
				<strong>{l s='Grade:' mod='mymodcomments'}</strong>
			</div> <input value="{$comment.grade}" type="number" class="rating" min="0" max="5" step="1" data-size="xs" />
			<div><i class="glyphicon glyphicon-comment"></i> <strong>{l s='Comment' mod='mymodcomments'}
					#{$comment.id_mymod_comment}:</strong> {$comment.comment}</div>
		</div>
		<hr />
	{/foreach}
</div>

<ul class="pagination">
	{for $count=1 to $nb_pages}
		{assign var=params value=[
		        'module_action' => 'list',
		        'product_rewrite' => $product->link_rewrite,
		        'id_product' => $smarty.get.id_product,
		        'page' => $count
		    ]}
		{if $page eq $count}
			<li class="active current"><span><span>{$count}</span></span> </li>
		{else}
			<li><a href="{$link->getModuleLink('mymodcomments', 'comments', $params)}"><span>{$count}</span> </a></li>

		{/if}
	{/for}
</ul>
{/block}

This is my front controller  file:

//this is my controller code:
<?php
class MyModCommentsCommentsModuleFrontController extends ModuleFrontController
{
    public $product;
    public function setMedia()
        {
        parent::setMedia();
        $this->path = __PS_BASE_URI__.'modules/mymodcomments/';
        $this->context->controller->addCSS($this->path.'views/css/star-rating.css', 'all');
        $this->context->controller->addJS($this->path.'views/js/star-rating.js');
        $this->context->controller->addCSS($this->path.'views/css/mymodcomments.css', 'all');
        $this->context->controller->addJS($this->path.'views/js/mymodcomments.js');
    }
    public function initContent()
    {
        parent::initContent();
        $id_product = (int) Tools::getValue('id_product');
        $module_action = Tools::getValue('module_action');
        $actions_list = array('list' => 'initList');
        $this->product = new Product((int)$id_product, false, $this->context->cookie->id_lang);
        if ($id_product > 0 && isset($actions_list[$module_action])) {
            $method = $actions_list[$module_action];
            $this->$method();
        }

    }
    protected function initList()
    {
        $nb_comments = MyModComment::getProductNbComments((int)$this->product->id);
		$nb_per_page = 3;
		$nb_pages = ceil($nb_comments / $nb_per_page);
		$page = 1;
		if (Tools::getValue('page') != '')
		$page = (int)$_GET['page'];
		$limit_start = ($page - 1) * $nb_per_page;
		$limit_end = $nb_per_page;
        $comments = MyModComment::getProductComments((int)$this->product->id, $limit_start, $limit_end);
        //$comments = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'mymod_comment` WHERE `id_product` = '.(int)$this->product->id.' ORDER BY `date_add` DESC LIMIT '.(int)$limit_start.','.(int)$limit_end);
        $this->context->smarty->assign('comments', $comments);
        $this->context->smarty->assign('product', $this->product);
        $this->context->smarty->assign('page', $page);
		$this->context->smarty->assign('nb_pages', $nb_pages);
        $this->setTemplate('module:mymodcomments/views/templates/front/list.tpl');
    }
}
?>

Screenshotfrom2024-06-0914-07-42.thumb.png.801699f2a879aa76801c2cd78d88494e.png

Here are the codes from my front controller file and Smarty template file from my module.

These worked in PrestaShop 1.6 but are not functioning in PrestaShop 1.7.7.5. I have already made several modifications to adapt to PrestaShop 1.7, but I am still encountering errors. Can anyone help, please?

The error screenshot is attached.

Edited by Najam Sajid (see edit history)
Link to comment
Share on other sites

  • Najam Sajid changed the title to FatalThrowableError while working with front controller
1 hour ago, Prestashop Addict said:

Hi, you error message is clear you need to pass variables in an array

$this->context->smarty->assign([
                'comments' => $comments,
                'product' => $this->product,
                'page' => $page,
                'nb_pages' => $nb_pages
            ]
);

 

Hi sir, thank you for your response.

I have already tried this, but the error persists. I'm not sure what is wrong with the code. I have debugged and checked the values in the template, and they are already assigned as an array.

Link to comment
Share on other sites

Posted (edited)
12 hours ago, Prestashop Addict said:

Hi, you error message is clear you need to pass variables in an array

$this->context->smarty->assign([
                'comments' => $comments,
                'product' => $this->product,
                'page' => $page,
                'nb_pages' => $nb_pages
            ]
);

 

Finally I got this that I should pass "'page' => $page," as array so I did this like: 'page' => array ($page), because After commenting this code the template was loading properly.
But now I am facing this error:

(1/1) ContextErrorException

Notice: Undefined index: meta

Now I don't know how to resolve it. Please help. Thanks in Advance.

Edited by Najam Sajid (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...