Jump to content

[Resolved] Re-use Prestashop Related Products Module


Recommended Posts

Hi,

I am developping a recommender system module and would like to use prestashop related products module for recommended products display. However, I am struggling with the documentation for data structure to pass this template. 
Actually, I am loading my products like that:

$products = array();
foreach($recommendations as $recommendation) {
	array_push($products, (array) new Product($recommendation['id_product'], false, '1'));
}

And pass that $products variable to the template: "module:ptsrelatedproducts/views/templates/hook/ptsrelatedproducts.tpl".

It displays some products but with a lot of missing information like images, links, js events, etc. 

image.thumb.png.f645939ae8404ee70edd6bd0c93f4acd.png

Does anyone know how to reuse that template ? Which datatype should I send in the $products array ? Is there any reusable function to format that data ? 

I am working on prestashop 1.7.1 

Thanks in advance

 

Simon

Edited by SimonBru
resolved (see edit history)
Link to comment
Share on other sites

I found a solution to my problem, by digging into the PtsRelatedProducts class, i took some code of the getCategoryProducts function. 

The part of code in the original code is not needed anymore, this function formats data to be passed to the module:ptsrelatedproducts/views/templates/hook/ptsrelatedproducts.tpl template.

/**
 * @param array $products an array with product id [ 'id_product' => 1 ....]
 */
private function getProducts($products) {

    $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()
    );

    foreach ($products as $rawProduct) {
        $productsForTemplate[] = $presenter->present(
            $presentationSettings,
            $assembler->assembleProduct($rawProduct),
            $this->context->language
        );
    }

    return $productsForTemplate;
}

 

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

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...