Jump to content

Product tabs in 1.7


Recommended Posts

Use the new productExtraContent hook like this:
    public function hookDisplayProductExtraContent($params)
    {
        $productExtraContent = new ProductExtraContent();
        $productExtraContent->setTitle($this->l('Gallery'));
        $productExtraContent->setContent($this->context->smarty->fetch(
            'module:nc_gallery/views/templates/front/tab_content.tpl'
        ));
            
        return array($productExtraContent);
    }

This code is similar to what I have in my Image/Video Gallery module to create a "Gallery" tab and load the content from tab_content.tpl.

Link to comment
Share on other sites

 

Use the new productExtraContent hook like this:
    public function hookDisplayProductExtraContent($params)
    {
        $productExtraContent = new ProductExtraContent();
        $productExtraContent->setTitle($this->l('Gallery'));
        $productExtraContent->setContent($this->context->smarty->fetch(
            'module:nc_gallery/views/templates/front/tab_content.tpl'
        ));
            
        return array($productExtraContent);
    }

This code is similar to what I have in my Image/Video Gallery module to create a "Gallery" tab and load the content from tab_content.tpl.

 

 

 

Thank you for your quick and accurate response!

 

With this, I ended up having "mod_fcgid: stderr: PHP Fatal error:  Class 'ProductExtraContent' not found ..." written to my error_log.

 

Obviously I was still missing something, but since you got me on the right track, here is what worked for me:

public function hookdisplayProductExtraContent($params)
{        
    $array = array();
    $array[] = (new PrestaShop\PrestaShop\Core\Product\ProductExtraContent())
            ->setTitle('tittle')
            ->setContent('content');
    return $array;
}

Hope this thread will be useful for future learners!

Link to comment
Share on other sites

  • 7 months later...

It should be in the main module file. For example, modules/nc_gallery/nc_gallery.php in my module. Add the use line to the top of the file and the hookDisplayProductExtraContent function anywhere after the class line and before the last }.

Also, don't forget to register the hook in the install() function using the code $this->registerHook('displayProductExtraContent')

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

  • 2 years later...
On 3/16/2017 at 12:09 PM, rocky said:
Use the new productExtraContent hook like this:

    public function hookDisplayProductExtraContent($params)
    {
        $productExtraContent = new ProductExtraContent();
        $productExtraContent->setTitle($this->l('Gallery'));
        $productExtraContent->setContent($this->context->smarty->fetch(
            'module:nc_gallery/views/templates/front/tab_content.tpl'
        ));
            
        return array($productExtraContent);
    }

This code is similar to what I have in my Image/Video Gallery module to create a "Gallery" tab and load the content from tab_content.tpl.

@rocky

Hey we are new to prestashop developing. We would like to show our custom product info on the front end, same as features and attributes. We have a module and we are implementing your code, but nothing appears to the front end.

We have disabled cache and cleared, but it didn't work. Is there something missing, do we have to invoke this hook elsewhere?

Link to comment
Share on other sites

6 hours ago, rocky said:

After adding the hookDisplayProductExtraContent($params) function to the module, did you transplant it into the displayProductExtraContent hook on the "Design > Positions > Transplant a module" tab?

@rocky Hey we got it working! Now we are struggling to show this info on the backend. There seems to be a similar hook DisplayAdminProductsExtra($params), but it doesn't seem to follow the same logic as front end. Do you have a solution for this?

Link to comment
Share on other sites

Here's the function I use in my Image/Video Gallery module (modules/nc_gallery/nc_gallery.php):

public function hookDisplayAdminProductsExtra($params)
{
    if (isset($params['id_product']) && (int)$params['id_product']) {
        $controller = new AdminNCGalleryGalleriesController();
        $this->context->smarty->assign('form', $controller->renderProductForm((int)$params['id_product']));

        return $this->display(__FILE__, 'views/templates/admin/tab.tpl');
    }
}

And a simplified version of the renderProductForm function that displays a multi-language text field on the configuration page (modules/nc_gallery/controllers/admin/AdminNCGalleryGalleriesController.php):

public function renderProductForm($id_product)  
{
    if ((int)$id_product <= 0) {
        return;
    }

    $fields_value = [];
    $product_tab = null;
    $id_product_tab = (int)NCGalleryProductTab::getTabForProduct((int)$id_product);

    if ($id_product_tab) {
        $product_tab = new NCGalleryProductTab((int)$id_product_tab);
        $fields_value['nc_gallery_name'] = [];

        foreach (Language::getLanguages(false) as $language) {
            if (is_array($product_tab->name) && isset($product_tab->name[(int)$language['id_lang']]) &&
                $product_tab->name[(int)$language['id_lang']] != '') {
                $fields_value['nc_gallery_name'][(int)$language['id_lang']] =
                    $product_tab->name[$language['id_lang']];
            } else {
                $fields_value['nc_gallery_name'][(int)$language['id_lang']] =
                    Configuration::get('NC_GALLERY_DEFAULT_TAB_TITLE', (int)$language['id_lang']);
            }
        }
    } else {
        foreach (Language::getLanguages(false) as $language) {
            $fields_value['nc_gallery_name'][(int)$language['id_lang']] =
                Configuration::get('NC_GALLERY_DEFAULT_TAB_TITLE', (int)$language['id_lang']);
        }
    }

    $this->fields_form = [
        'input' => [
            [
                'type' => 'text',
                'label' => $this->l('Title'),
                'name' => 'nc_gallery_name',
                'hint' => $this->l('The tab text displayed on the product page'),
                'required' => true,
                'lang' => true,
                'class' => 'form-control',
            ],
        ],
    ];

    $this->fields_form = [['form' => $this->fields_form]];

    $helper = new HelperForm($this);
    $this->setHelperDisplay($helper);
    $helper->languages = Language::getLanguages();
    $helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
    $helper->fields_value = $fields_value;
    $helper->submit_action = $this->submit_action;
    $helper->tpl_vars = $this->getTemplateFormVars();

    return $helper->generateForm($this->fields_form);
}

And in modules/nc_gallery/views/templates/admin/tab.tpl:

<input type="hidden" name="submitted_tabs[]" value="nc_gallery" />

{$form}

I hope this helps.

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

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