iomedia Posted June 14, 2017 Share Posted June 14, 2017 Hi everybody, I need your help. I created a wishlist module for my shop and a new page listing all products that a user has add to his wishlist. What I want to do is reuse the partial template catalog/_partials/miniatures/product.tpl and for that the product list must be same as $listings.products. What I'm doing now is something like this: $products = array(); foreach ($products_id as $product_id){ $products[] = (array)new Product((int)$product_id['id_product'], true, $this->context->language->id); } to make the products array. how can I get products in good format? Thanks for your help Florian Link to comment Share on other sites More sharing options...
iomedia Posted July 18, 2017 Author Share Posted July 18, 2017 Hi, I found a solution looking in ps_featuredproducts. Here how I do that: protected function getProducts() { $customer = $this->context->customer->id; $sql = 'SELECT p.* FROM `'._DB_PREFIX_.'wishlist` wl LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = wl.id_product WHERE id_customer = '.$customer; $products = Db::getInstance()->executeS($sql); $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; } Hope it helps someone. 2 5 Link to comment Share on other sites More sharing options...
fanie Posted December 20, 2018 Share Posted December 20, 2018 It work just fine, Don't forget to write: use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter; use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever; use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter; use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever; 1 Link to comment Share on other sites More sharing options...
regatonseb Posted May 10, 2019 Share Posted May 10, 2019 Hello can you explain more what did you do ? I am trying too but in vain... I have seen the function but how can I call it and display products ? Thank you so much. Link to comment Share on other sites More sharing options...
fanie Posted May 13, 2019 Share Posted May 13, 2019 As the function is part of a module, and you are writing you own module, I would said you have to rewrite it on your own. Don't forget to put the function result in smarty. So for now, you have the product list creation done. For the display template part, I would do (in your module front controller ?): $this->module->display([path to your module file], [path to your template]); And in your template this content: {extends file='catalog/_partials/miniatures/product.tpl'} Link to comment Share on other sites More sharing options...
regatonseb Posted May 13, 2019 Share Posted May 13, 2019 Thank you, I have forgotten this line in my code : $products = $this->getProducts(); It's just stupid but I was focused on the function... Bye ! Link to comment Share on other sites More sharing options...
Darius1990 Posted June 18, 2019 Share Posted June 18, 2019 On 7/18/2017 at 5:11 PM, iomedia said: Hi, I found a solution looking in ps_featuredproducts. Here how I do that: protected function getProducts() { $customer = $this->context->customer->id; $sql = 'SELECT p.* FROM `'._DB_PREFIX_.'wishlist` wl LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = wl.id_product WHERE id_customer = '.$customer; $products = Db::getInstance()->executeS($sql); $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; } Hope it helps someone. Thank you so much! It`s working! 1 Link to comment Share on other sites More sharing options...
TonyKapa Posted June 20, 2022 Share Posted June 20, 2022 (edited) Hello! I use version 1.7.8.6. Here is my code public function getProducts() { $customer = $this->context->customer->id; //Get Last Products $db = Db::getInstance(); $request = 'SELECT DISTINCT `product_id` FROM `' . _DB_PREFIX_ . 'order_detail` ORDER BY `id_order` DESC LIMIT 10 '; $result = $db->executeS($request); foreach($result as $rs){ $products[] = new Product($rs, false, $this->context->language->id); } // echo '<pre>';print_r($products);die; $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 = array(); foreach ($products as $rawProduct) { $rawProduct = (array) $rawProduct; $products_for_template[] = $presenter->present( $presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language ); } return $products_for_template; } I get 2 errors. First One : I turn the $rawProduct to array : $rawProduct = (array) $rawProduct; Then I get this error: Anyone has any idea? Thanks in advance!! Edited June 20, 2022 by TonyKapa (see edit history) Link to comment Share on other sites More sharing options...
przemokon Posted September 22, 2022 Share Posted September 22, 2022 On 6/20/2022 at 5:08 PM, TonyKapa said: Hello! I use version 1.7.8.6. Here is my code public function getProducts() { $customer = $this->context->customer->id; //Get Last Products $db = Db::getInstance(); $request = 'SELECT DISTINCT `product_id` FROM `' . _DB_PREFIX_ . 'order_detail` ORDER BY `id_order` DESC LIMIT 10 '; $result = $db->executeS($request); foreach($result as $rs){ $products[] = new Product($rs, false, $this->context->language->id); } // echo '<pre>';print_r($products);die; $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 = array(); foreach ($products as $rawProduct) { $rawProduct = (array) $rawProduct; $products_for_template[] = $presenter->present( $presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language ); } return $products_for_template; } I get 2 errors. First One : I turn the $rawProduct to array : $rawProduct = (array) $rawProduct; Then I get this error: Anyone has any idea? Thanks in advance!! @TonyKapa Product is using id instead of id_product. On my PrestaShop v1.7.7.4 works fine. Try to use: $assembler->assembleProduct(['id_product' => $rawProduct['id']]) instead of: $assembler->assembleProduct(['id_product' => $rawProduct['id_product']]) 1 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