artivelab Posted July 1, 2017 Share Posted July 1, 2017 Hello everyone, I am using prestashop 1.7 and developing a template on it. It's my first time that I am trying to work on a template from scratch and play with prestashop's core. What I want to do is: When viewing a product on the product page I want to have 2 related products. I added a hook there and installed a free module I found here "relatedFree module". I played with the setting so I don't have to add manualythe to each product the category id, I pass it through smarty on the hook. So far so good. Where I am stack is, when trying to get the listing of 2 randoms products. I used $category->getProducts(....); it returns an array of products but not like the one on the category page. I tryied Product::getProducts(..); yet again has different array from category page. In caregory page (products.tpl), we get a default variable named "listing" that then we loop through to print all the products. {foreach from=$listing.products item="product"} That's the default functionality. How can I get the same $listing variable in my code ? the $listing variable has "url" for the link of the product and the cover image but the $category-getProducts doesn't. it has a "link" for the link of the product and no images. only the id. I want to use the same tpl that prints the products and because the arrays are different with different variables it breaks. Link to comment Share on other sites More sharing options...
mantas393 Posted January 11, 2019 Share Posted January 11, 2019 Hi @artivelab , did you find an answer to this question you asked? Link to comment Share on other sites More sharing options...
artivelab Posted March 2, 2019 Author Share Posted March 2, 2019 Hello and sorry for the late reply, I will post the solutions I found below. @mantas393 What I did after alot of searching, I created a function inside the main php file of the module that I wanted to get the products the same way as prestashop returns them. public function prepareBlocksProducts($products) { if ($products != false) { $products_for_template = []; $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()); $products_for_template = []; foreach ($products as $rawProduct) { $products_for_template[] = $presenter->present($presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language); } return $products_for_template; } else { return false; } } So I pass an array of products that I got from the category as followed: $products = $category->getProducts($lang, 0, 100); and used the function we created to update the way the array is returned and their link / urls etc like so: $products = $this->prepareBlocksProducts($products); Let me know if you need more information, Giannis 1 Link to comment Share on other sites More sharing options...
mantas393 Posted March 2, 2019 Share Posted March 2, 2019 Thanks artivelab, I'll try to implement this. Link to comment Share on other sites More sharing options...
regatonseb Posted July 3, 2019 Share Posted July 3, 2019 Hello, And what is the tpl code to display the product list ? Thank you so much !! Seb Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now