Back to Top

How to create a Product Grid listing same as Prestashop Theme | in Prestashop V-1.7 Module

Updated 28 June 2019

Lets first understand the problem, if you want a grid view of products on your Prestashop controller in a module like this in the screen shot

Screenshot-3

how to get this view in Prestashop, will you start this by writing a template and css for the same, I don’t think will be a good idea.

I am telling you a nice and simple way to get this view,

and the amazing fact is that what if there are a new theme installed in the prestashop, yes the solution will be compatible to any theme. see this in Prestashop Responsive Inventive Theme

Screenshot1-3

so lets straight come to the coding part, here the first step is to use some needed packages at the top of your module class,

Searching for an experienced
Prestashop Company ?
Find out More
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter;
use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever;

now create a front controller or hook and return a smarty file from this.

and write the following code on this smarty template file,

{foreach from=$disountProducts item="product"}
   <li class="wk_product_list col-md-3 col-sm-4 col-xs-12">
     {include file="catalog/_partials/miniatures/product.tpl" product=$product}
   </li>
{/foreach}

as you can see this here I have directly include a smarty template file which is including from theme.

so obviously will be compatible with the current theme 🙂

I include the templete file in a foreach loop of products ($disountProducts)

now the question is what is in this variable $disountProducts.

of course the product details, but what is the format and how to create this.

so lets create this array.

/*products to show in grid*/
$discountedProducts = Product::getPricesDrop((int)$this->context->language->id);

here in $discountedProducts key id_products is required, other needed data will be assemble later.

so now the next step is to create the needed class objects,

$assembler = new ProductAssembler($this->context);
$presenterFactory = new ProductPresenterFactory($this->context);

$presentationSettings = $presenterFactory->getPresentationSettings();

$presenter = new ProductListingPresenter(
    new ImageRetriever(
        $this->context->link
    ),
    $this->context->link,
    new PriceFormatter(),
    new ProductColorsRetriever(),
    $this->context->getTranslator()
);

now come to the last step to get the required array.

$products_for_template = [];
foreach ($discountedProducts as $productDetail) {
    $products_for_template[] = $presenter->present(
        $presentationSettings,
        $assembler->assembleProduct($productDetail),
        $this->context->language
    );
}

here I use $assembler->assembleProduct($productDetail) to assemble the needed product details.

thats it.

$this->context->smarty->assign(
    'disountProducts',
    $products_for_template
);

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home