{"id":2381,"date":"2022-02-21T14:25:06","date_gmt":"2022-02-21T12:25:06","guid":{"rendered":"https:\/\/www.h-hennes.fr\/blog\/?p=2381"},"modified":"2022-04-04T09:49:10","modified_gmt":"2022-04-04T07:49:10","slug":"prestashop-afficher-les-commentaires-produits-dans-les-onglets-de-la-fiche-produit","status":"publish","type":"post","link":"https:\/\/www.h-hennes.fr\/blog\/2022\/02\/21\/prestashop-afficher-les-commentaires-produits-dans-les-onglets-de-la-fiche-produit\/","title":{"rendered":"Prestashop: Afficher les commentaires produits dans les onglets de la fiche produit"},"content":{"rendered":"\n<p>Petit tutoriel rapide aujourd&rsquo;hui sur une optimisation que j&rsquo;ai voulu faire cette semaine et qui s&rsquo;est av\u00e9r\u00e9e un peu plus emb\u00eatante que pr\u00e9vue.<br>Le besoin est relativement simple je veux d\u00e9placer le contenu des commentaires <strong>dans les onglets<\/strong> affich\u00e9s sur la fiche produit. ( Dans un th\u00e8me bas\u00e9 sur classic )<br>Les commentaires utilisent le module natif de Prestashop <em>productcomments<\/em><br>L&rsquo;affichage du contenu des onglets est g\u00e9r\u00e9 par le hook <em>displayProductExtraContent <\/em><br>Voici une capture du comportement avant :<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1016\" height=\"453\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image.png\" alt=\"\" class=\"wp-image-2383\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image.png 1016w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image-300x134.png 300w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image-768x342.png 768w\" sizes=\"auto, (max-width: 1016px) 100vw, 1016px\" \/><\/a><\/figure>\n\n\n\n<p>Et voici une capture du comportement que je souhaite obtenir apr\u00e8s.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"564\" height=\"336\" src=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image-1.png\" alt=\"\" class=\"wp-image-2384\" srcset=\"https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image-1.png 564w, https:\/\/www.h-hennes.fr\/blog\/wp-content\/uploads\/2022\/02\/image-1-300x179.png 300w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><\/a><\/figure>\n\n\n\n<p>Plusieurs approches techniques sont possibles pour faire cela, pour ma part j&rsquo;ai choisi de le faire un module compl\u00e9mentaire <strong>hhproductcomments<\/strong> qui d\u00e9pends du module natif <strong>productcomments<\/strong>.<br>Cela pr\u00e9sente l&rsquo;avantage de ne pas toucher devoir toucher ni au module natif, ni au th\u00e8me pour g\u00e9rer cet affichage.<br>L&rsquo;id\u00e9e est de greffer ce nouveau module sur le hook displayProductExtraContent pour g\u00e9rer l&rsquo;affichage en onglet et d&rsquo;appeller le hook natif <em>displayFooterProduct<\/em> du module productcomments<br><br>Le gros point d&rsquo;attention \u00e0 avoir , c&rsquo;est que la fonction hookDisplayFooterProduct attends une instance de&nbsp; <em>\\PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductLazyArray <\/em>et non pas un instance du produit ( Product ) telle qu&rsquo;on la re\u00e7oit dans le hook initial.<br>Il va donc falloir transformer le param\u00e8tre pour que \u00e7a fonctionne.<br><br>Voici l&rsquo;ensemble du code du module : <br><br><\/p>\n\n\n\n<pre lang=\"php\" escaped=\"true\">\n<?php\nuse PrestaShop\\PrestaShop\\Adapter\\Presenter\\Product\\ProductLazyArray;\nuse PrestaShop\\PrestaShop\\Core\\Product\\ProductExtraContent;\nuse PrestaShop\\PrestaShop\\Core\\Product\\ProductPresentationSettings;\n\nif (!defined('_PS_VERSION_')) {\n    exit;\n}\n\n\nclass Hhproductcomments extends Module\n{\n\n    \/** @var string Nom du module de gestion des commentaires *\/\n    const PRODUCT_COMMENTS_MODULE_NAME = 'productcomments';\n\n    public function __construct()\n    {\n        $this->name = 'hhproductcomments';\n        $this->tab = 'others';\n        $this->version = '0.1.0';\n        $this->author = 'hhennes';\n        $this->bootstrap = true;\n        $this->dependencies = [self::PRODUCT_COMMENTS_MODULE_NAME];\n        parent::__construct();\n\n        $this->displayName = $this->l('Hh product comments');\n        $this->description = $this->l('Display prestashop module comments in product extra content');\n    }\n\n    \/**\n     * Installation du module\n     * @return bool\n     *\/\n    public function install()\n    {\n        return parent::install()\n            && $this->registerHook('displayProductExtraContent')\n            && $this->unregisterProductCommentsFooter();\n    }\n\n    \/**\n     * Suppression du hook footer pour le module productComments\n     * @return bool\n     *\/\n    protected function unregisterProductCommentsFooter(): bool\n    {\n        if ($productCommentModule = $this->getProductCommentsModule()) {\n            return $productCommentModule->unregisterHook('displayFooterProduct');\n        }\n        return true;\n    }\n\n    \/**\n     * R\u00e9cup\u00e9ration du module de commentaire produits\n     * @return false|Module\n     *\/\n    protected function getProductCommentsModule()\n    {\n        return Module::getInstanceByName(self::PRODUCT_COMMENTS_MODULE_NAME);\n    }\n\n    \/**\n     * Affichage du contenu du module productComments dans le hookDisplay extra Content\n     * @param array $params\n     * @return array\n     *\/\n    public function hookDisplayProductExtraContent($params)\n    {\n        $return = [];\n        $productCommentModule = $this->getProductCommentsModule();\n        if ($productCommentModule && $productCommentModule->active == true) {\n            \/** @var \\ProductComments $productCommentModule *\/\n            try {\n                \/\/Probl\u00e8me \u00e0 ce niveau : dans notre hook on a une instance de Product et le hook du module veut avoir un ProductLazyArray\n                $params['product'] = $this->getPresentedProduct($params);\n                $content = $productCommentModule->hookDisplayFooterProduct($params);\n            } catch (Exception $e) {\n                $content = 'Error ' . $e->getMessage();\n            }\n            $return[] = (new ProductExtraContent())\n                ->setTitle($this->l('Reviews'))\n                ->setContent($content);\n        }\n        return $return;\n    }\n\n    \/**\n     * R\u00e9cup\u00e9ration d'une instance ProductLazyArray pour le frontOffice\n     * @param array $params\n     * @return ProductLazyArray\n     * @throws ReflectionException\n     *\/\n    protected function getPresentedProduct(array $params): ProductLazyArray\n    {\n        \/\/Gestion des param\u00e8tres de pr\u00e9sentation\n        $settings = new ProductPresentationSettings();\n        $settings->catalog_mode = Configuration::isCatalogMode();\n        $settings->catalog_mode_with_prices = (int)Configuration::get('PS_CATALOG_MODE_WITH_PRICES');\n        $settings->include_taxes = true;\n        $settings->allow_add_variant_to_cart_from_listing = (int)Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY');\n        $settings->stock_management_enabled = Configuration::get('PS_STOCK_MANAGEMENT');\n        $settings->showPrices = Configuration::showPrices();\n        $settings->lastRemainingItems = Configuration::get('PS_LAST_QTIES');\n        $settings->showLabelOOSListingPages = (bool)Configuration::get('PS_SHOW_LABEL_OOS_LISTING_PAGES');\n\n        \/\/R\u00e9cup\u00e9ration de l'instance du presenter\n        $productPresenter = new ProductPresenterFactory($this->context, null);\n        $presenter = $productPresenter->getPresenter();\n\n        \/\/R\u00e9cup\u00e9ration d'une instance de l'assembler\n        $assembler = new ProductAssembler($this->context);\n\n        \/\/Conversion de l'objet \"Product\" en tableau et ajout de son identifiant\n        $product = (array)$params['product'];\n        $product['id_product'] = $product['id'];\n\n        return $presenter->present(\n            $settings,\n            $assembler->assembleProduct($product),\n            $this->context->language\n        );\n    }\n\n\n}\n\n<\/pre>\n\n\n\n<p class=\"has-text-align-center has-ast-global-color-0-color has-luminous-vivid-amber-background-color has-text-color has-background\"><a href=\"https:\/\/shop.h-hennes.fr\/fr\/10-afficher-les-commentaires-produits-dans-les-onglets.html\" target=\"_blank\" rel=\"noreferrer noopener\">T\u00e9l\u00e9charger le module complet ( et gratuit ) sur la boutique<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Petit tutoriel rapide aujourd&rsquo;hui sur une optimisation que j&rsquo;ai voulu faire cette semaine et qui s&rsquo;est av\u00e9r\u00e9e un peu plus emb\u00eatante que pr\u00e9vue.Le besoin est relativement simple je veux d\u00e9placer le contenu des commentaires dans les onglets affich\u00e9s sur la fiche produit. ( Dans un th\u00e8me bas\u00e9 sur classic )Les commentaires utilisent le module natif [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[245],"tags":[104,483],"class_list":["post-2381","post","type-post","status-publish","format-standard","hentry","category-prestashop-2","tag-prestashop","tag-prestashop-1-7","prestashop-1-7","prestashop-1-7-8"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/comments?post=2381"}],"version-history":[{"count":4,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2381\/revisions"}],"predecessor-version":[{"id":2427,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/posts\/2381\/revisions\/2427"}],"wp:attachment":[{"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/media?parent=2381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/categories?post=2381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h-hennes.fr\/blog\/wp-json\/wp\/v2\/tags?post=2381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}