Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

displayProductExtraContent page #1749

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions modules/concepts/hooks/list-of-hooks/displayProductExtraContent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
Title: displayProductExtraContent
hidden: true
hookTitle: 'Add content to the product page'
files:
-
theme: classic
url: 'https://github.com/PrestaShop/classic-theme/blob/develop/templates/catalog/product.tpl#L216'
file: 'templates/catalog/product.tml'
-
url: 'https://github.com/PrestaShop/PrestaShop/blob/8.0.x/src/Core/Product/ProductExtraContent.php'
file: 'src/Core/Product/ProductExtraContent.php'
-
url: 'https://github.com/PrestaShop/PrestaShop/blob/8.0.x/src/Core/Product/ProductExtraContentFinder.php'
file: 'src/Core/Product/ProductExtraContentFinder.php'
locations:
- 'front office'
type: display
hookAliases:
origin: core
array_return: false
check_exceptions: false
chain: false
description: 'Adds new field / content to the FO product page'
has_example: true
---

{{% hookDescriptor %}}

## Call of the Hook in the origin file

```php
protected $hookName = 'displayProductExtraContent';
protected $expectedInstanceClasses = ['PrestaShop\PrestaShop\Core\Product\ProductExtraContent'];
```

## Example implementation

This hook has been implemented as an example in our [modules examples repository - demoproductextracontent](https://github.com/PrestaShop/example-modules/tree/master/demoproductextracontent).
thomasnares marked this conversation as resolved.
Show resolved Hide resolved

## Hook explained

This hook is a little more complicated than other ones.

It starts by a render of extra contents, from the theme:
thomasnares marked this conversation as resolved.
Show resolved Hide resolved

```php
{foreach from=$product.extraContent item=extra key=extraKey}
<div class="tab-pane fade in {$extra.attr.class}" id="extra-{$extraKey}" role="tabpanel" {foreach $extra.attr as $key => $val} {$key}="{$val}"{/foreach}>
{$extra.content nofilter}
</div>
{/foreach}
```

Then, the `product` entity uses a `ProductExtraContentFinder` to fetch all extra content.
thomasnares marked this conversation as resolved.
Show resolved Hide resolved

```php
class ProductExtraContentFinder extends HookFinder
{
protected $hookName = 'displayProductExtraContent';
protected $expectedInstanceClasses = ['PrestaShop\PrestaShop\Core\Product\ProductExtraContent'];
```

The `ProductExtraContentFinder` will look for modules implementing `displayProductExtraContent` methods, and will expect `ProductExtraContent` entities to be returned.
thomasnares marked this conversation as resolved.
Show resolved Hide resolved

```php
return (new PrestaShop\PrestaShop\Core\Product\ProductExtraContent())
->setTitle('example field')
->setContent('example content')
```

This content will be shown in a dedicated tab on the product page:

![displayProductExtraContent](../screenshots/displayProductExtraContent.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.