Jump to content

Getting product id in hookDisplayAdminProductsExtra


Recommended Posts

I'm trying to display a textarea in the admin extra tab in PS 1.7.

 

This is my tpl file:

<textarea id="pcpc">
    {if isset($pcpcText) && !empty($pcpcText)} {$pcpcText} {/if} 
</textarea>

And this is the hook in the controller:

public function hookDisplayAdminProductsExtra($id_module) {
    $id_product = Tools::getValue('id_product');
    file_put_contents(dirname(__FILE__).'/pid.txt', $id_product);
    $this->context->smarty->assign(array(
        'pcpcText' => "Id product: $id_product",
    ));


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

Every website found on google says to use Tools::getValue('id_product') to get the product id, but I get an empty string instead.

 

The product exists, it isn't a new product, I'm editing an already existing one, so I expect to get a valid ID.

 

The textarea shows "Product id: " and the pid.txt file is blank.

 

How do I properly get the product id in the displayAdminProductsExtra hook?

 

 

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

Here's how I did it in my Image/Video Gallery module:

    public function hookDisplayAdminProductsExtra($params)
    {
        if (isset($params['id_product']) && (int)$params['id_product']) {
            // Perform action using $params['id_product'] here
        }
    }
Link to comment
Share on other sites

 

Here's how I did it in my Image/Video Gallery module:

    public function hookDisplayAdminProductsExtra($params)
    {
        if (isset($params['id_product']) && (int)$params['id_product']) {
            // Perform action using $params['id_product'] here
        }
    }

Did you do this in prestashop 1.7? Unfortunately it doesn't work for me.

 

Edit

Nevermind, Typo. It works. Thanks!

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

Yes, it works in PrestaShop v1.7. Did you change the $id_module parameter to $params like in my code? I think I also had to hook into actionProductUpdate before it would work. 

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

            return $this->display(__FILE__, 'views/templates/admin/tab.tpl');
        }
    }
    
    public function hookActionProductUpdate($params)
    {
        $id_product_tab = (int)NCGalleryProductTab::getTabForProduct((int)$params['id_product']);
        $product_tab = null;
        
        if ($id_product_tab) {
            $product_tab = new NCGalleryProductTab($id_product_tab);
        } else {
            $product_tab = new NCGalleryProductTab();
        }

        $product_tab->id_nc_gallery_gallery = (int)Tools::getValue('id_nc_gallery_gallery');
        $product_tab->id_product = (int)$params['product']->id;
        $product_tab->active = (int)Tools::getValue('nc_gallery_active');
        $product_tab->name = array();

        foreach (Language::getLanguages(true) as $language) {
            $product_tab->name[(int)$language['id_lang']] = Tools::getValue('nc_gallery_name_'.$language['id_lang']);
        }

        if ($id_product_tab) {
            $product_tab->update();
        } else {
            $product_tab->add();
        }
    }

  • Like 1
Link to comment
Share on other sites

  • 3 years later...
On 3/1/2017 at 10:41 AM, rocky said:

Yes, it works in PrestaShop v1.7. Did you change the $id_module parameter to $params like in my code? I think I also had to hook into actionProductUpdate before it would work. 


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

            return $this->display(__FILE__, 'views/templates/admin/tab.tpl');
        }
    }
    
    public function hookActionProductUpdate($params)
    {
        $id_product_tab = (int)NCGalleryProductTab::getTabForProduct((int)$params['id_product']);
        $product_tab = null;
        
        if ($id_product_tab) {
            $product_tab = new NCGalleryProductTab($id_product_tab);
        } else {
            $product_tab = new NCGalleryProductTab();
        }

        $product_tab->id_nc_gallery_gallery = (int)Tools::getValue('id_nc_gallery_gallery');
        $product_tab->id_product = (int)$params['product']->id;
        $product_tab->active = (int)Tools::getValue('nc_gallery_active');
        $product_tab->name = array();

        foreach (Language::getLanguages(true) as $language) {
            $product_tab->name[(int)$language['id_lang']] = Tools::getValue('nc_gallery_name_'.$language['id_lang']);
        }

        if ($id_product_tab) {
            $product_tab->update();
        } else {
            $product_tab->add();
        }
    }

 

On 3/1/2017 at 10:28 AM, harlandraka94 said:

Did you do this in prestashop 1.7? Unfortunately it doesn't work for me.

 

Edit

Nevermind, Typo. It works. Thanks!

Does this hook still work in ps 1.7.6.4?

Our code:

public function hookDisplayAdminProductsExtra($params)
    {
        if (isset($params['id_product']) && (int)$params['id_product']) {
            $product = $params["product"];
            // Our custom product values to show
            $kantavuus = $product->kantavuus
            $ohiajomelu = $product->ohiajomelu;
            $nopeusluokka = $product->nopeusluokka;

            $this->context->smarty->assign(array(
                'ohiajomelu' => "Ohiajomelu: $ohiajomelu",
            ));
            return $this->display(__FILE__, 'views/templates/admin/product_custom_tab.tpl');
        }
    }

It does nothing, I also deliberately made a syntax error and still everything worked on the backend. During install we register the hook:

  public function install()
    {
        if (!parent::install() || !$this->installSql() || !$this->registerHook('displayProductExtraContent') || !$this->registerHook('DisplayAdminProductsExtra')) return false;
        

        return true;
    }

What seems to be the problem?

  • Like 1
Link to comment
Share on other sites

Yes, it works with PrestaShop v1.7.6.4 since my module works in that version.

Have you added the $kantavuus, $ohiajomelu and $nopeusluokka variables to classes/Product.php? They must be added as public variables of the Product class, have their format defined in $definition['fields'] and have been added to the ps_product or ps_product_lang.

In the code you've written, $params["product"] doesn't exist. You could fix this by writing $product = new Product($params['id_product'), $this->context->language->id, $this->context->shop->id);

If you find it too difficult, you can hire me to write the module for you. If it's simply adding some fields to the product and product editor, it will be easy for me to do.

Link to comment
Share on other sites

@rocky We got it working by using  hookDisplayAdminProductsMainStepLeftColumnMiddle($params)

 Now we need to figure out how to add the same info for combinations.

I found a hook: displayAdminProductsCombinationBottom, but I didn't find any instructions on how to use it.

 

Any info on this?

Link to comment
Share on other sites

Unfortunately, I've never used that hook in any of my modules and it appears that none of the modules that come with PrestaShop use it either.

I did a search over the entire PrestaShop v1.7.6 codebase and discovered the displayAdminProductsCombinationBottom hook was added in PrestaShop v1.7.2.0 with the description "Display new elements in back office product page, Combination tab". The only other useful information I could find was in the following code:

echo $this->env->getExtension('PrestaShopBundle\Twig\HookExtension')->renderHook(
    "displayAdminProductsCombinationBottom",
    [
        "id_product" => $this->getAttribute($this->getAttribute($this->getAttribute(($context["form"] ?? null), "vars", []), "value", []), "id_product", []), 
        "id_product_attribute" => $this->getAttribute($this->getAttribute($this->getAttribute(($context["form"] ?? null), "vars", []), "value", []), "id_product_attribute", [])
    ]
);

The hook has the parameters id_product and id_product_attribute and directly displays whatever is returned.

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