Jump to content

How to get product id in backOffice products editing page


Recommended Posts

On 07/04/2018 at 3:22 PM, NemoPS said:

If you mean Tools::getValue, yes, it is, same way: Tools::getValue('id_product')

I tried using this in the backOffice but it gives me 0.
(int)Tools::getValue('id_product')

and if I remove the cast to int I get boolean false like here:

Tools::getValue('id_product')

What I want is the product id in BackOffice on the products editing page of the product that is being edited.

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

37 minutes ago, xpindia said:

Bring the mouse over the product image n u will see the product id in the status bar down in the progress bar....

http://localhost/prestashop/admin878hqqohr/index.php/product/form/5?_token=_RMf_bkF0#tab-step1

This the URL I get when I go to editing the controller. I don't see any product id but here 5 is the product id. The problem is how can I get that in using code in some variable? Everything else works like Tools::getValue('controller') gives the controller name i.e. AdminProducts, only it is not giving the product id.

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

13 hours ago, NemoPS said:

You should have specified you were using 1.7.

print out ($_GET) and see where it is, I forgot the variable name

Hi NemoPS I did specify in the tags that I am using prestashop 1.7. I did var_dump on $_GET and all I got was the information about the controller token and controllerUri but didn't get anything about product id.

Link to comment
Share on other sites

  • 8 months later...

use $params to get product ID. see below example

public function hookDisplayAdminProductsExtra($params)
    {
        $product = new Product((int)$params['id_product']);
        $hook = new ProductsPackDisplayAdminProductsExtra();
        $hook->displayTab($product);
        
        return $this->display(__FILE__, 'views/admin/image.tpl');
    }

Link to comment
Share on other sites

  • 5 months later...
  • 1 year later...

Thank you for the answer Daresh.

I'm honestly a little confused why it is "as difficult" to get the id_product value when it is in the URL.
Had to implement some customization on the product edit page and I hadn't been able to simply get the product id, until your answer.

Edited by Estian
Language update (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

Hi.

If a thread encounters multiple different requests, it needs to ask for additional information.
You want an example of how to get the product id from a hook displayAdminProductsExtra for Prestashop 8.x ?

https://devdocs.prestashop-project.org/8/modules/concepts/hooks/list-of-hooks/displayadminproductsextra/

public function hookDisplayAdminProductsExtra($params)
{
    $idProduct = (int)$params['id_product'];    
    $product = new Product($idProduct);
}

 

  • Thanks 1
Link to comment
Share on other sites

4 hours ago, ps8moduly.cz said:

Hi.

If a thread encounters multiple different requests, it needs to ask for additional information.
You want an example of how to get the product id from a hook displayAdminProductsExtra for Prestashop 8.x ?

https://devdocs.prestashop-project.org/8/modules/concepts/hooks/list-of-hooks/displayadminproductsextra/

public function hookDisplayAdminProductsExtra($params)
{
    $idProduct = (int)$params['id_product'];    
    $product = new Product($idProduct);
}

 

Ups - I forgot to put $params in function :

public function hookDisplayAdminProductsCombinationBottom($params)

Thanks for a tip I got it know.
However now I am looking how to get the product combination ID that is currently beeing edited.

 

Link to comment
Share on other sites

:)

Do you happen to know is Presta holding the admin BO folder name in some variable?
Can not find it and I am trying to redirect to product edit page with product id after doing some stuff on product combination page by module.

 

I have something like:
 

Tools::redirect('/adminBO/sell/catalog/'.$product_ID);

but not sure is this the best way

Link to comment
Share on other sites

You can also add which Tab should be displayed, depending on the condition if the product has a combination.
For example:

$openTab = 1; // Tab Basic settings

$product = new Product((int)$product_ID);

if ($product->hasAttributes() > 0) {
    // yes, combinations exists Tab Combinations
    $openTab = 3; 
}

$pController  = $this->context->link->getAdminLink('AdminProducts', true, ['id_product' => $product_ID]);
$pController .= '#tab-step'.$openTab;

Tools::redirectAdmin($pController);

 

  • Like 1
Link to comment
Share on other sites

Great answer - thank you very much. There is so much I do know yet.

Is it possible that when using in in module front controller there is different way to access to context?

$pController = $this->context->link->getAdminLink('AdminProducts', true, ['id_product' => $product_ID]);

returns empty. I tried also:

$symfonyUrl = $link->getAdminLink('AdminProducts', true, array('route' => 'admin_product_catalog'));

and

$link = $this->context->link; $url = $link->getAdminLink('AdminProducts', true, array( 
'route' => 'admin_product_catalog',
'id' => $product_ID,
));

I am missing something...

Link to comment
Share on other sites

<?php
include_once(dirname(__FILE__) . '../../../mymodule.php');

class myModuleDefaultModuleFrontController extends ModuleFrontController
{
    public function __construct()
    {
        parent::__construct();

        $this->context = Context::getContext();
    }

    public function initContent()
    {
        parent::initContent();

        if(isset($_POST['take_data']))
        {
//HANDLE THE POST  HERE
 			$product_ID = $_POST['product_ID'];
            $ID_product_attribute = $_POST['ID_product_attribute'];
/* action */

/* when job done then redirect */

            $openTab = 3;

          // $pController  = $this->context->link->getAdminLink('AdminProducts', true, ['id_product' => $product_ID]);
          // $pController .= '#tab-step'.$openTab;

			$link = $this->context->link;
            $url = $link->getAdminLink('AdminProducts', true, array(
                'route' => 'admin_product_catalog'

            ));
            $url.= '#tab-step'.$openTab;
       
     /* i have tried also:
$url =  $link->getAdminLink('AdminOrders', true, [], ['orderId' => 44, 'vieworder' => 1]);
*/

		/*that works */
          //  Tools::redirect('index.php');

            }

    }
}

Sure :) here is myModule front controller. Actions are fine - I have problem with getAdminLink() - probably some stupid mistake.

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

OK - my code is waiting for aproval to be posted. In mean time I see that my problem is to use

$link->getAdminLink()

in controller file of my module - do I need to include something to able to use it?

Code for getting link is OK and when use it in my Hook it gives back the link.

 

Link to comment
Share on other sites

thanks - ok so how to get it done?

I need redirect after handling POST request - that is being done in controller. Redirect itself works - but must think of how to redirect to product edition page and tab 3.
This redirect uses $vars that were $_POSTed

$product_id and $combination_id

to controller

Link to comment
Share on other sites

OK.

class myModuleDefaultModuleFrontController extends ModuleFrontController

it is controller - frontController in mymodule/controllers/front/default.php

it handles POST request from Back Office where my tpl with form is hooked. (product page edition )

my form action (in .tpl)  is:

<form action="{Context::getContext()->link->getModuleLink('mymodule', 'default')}" method="post">

I need to redirect to product edit page on tab-step3 after submitting the form on product edition page.

$openTab = 3; 
$pController = $this->context->link->getAdminLink('AdminProducts', true, ['id_product' => $product_ID]); 
$pController .= '#tab-step'.$openTab;

I can use in my controller (default.php): tools::redirect and give url like "index.php" - and it works fine but I am trying to construct the url link with getAdminLink - and can not do that..

I am missing something..

Hope that everything is quite clear.

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

1. FrontController = can be seen from the position of the customer, eshop
2. AdminController = is only the eshop administration

so FrontController is not intended for administration and AdminController is not intended for eshop.

You keep putting jackfruit and pears in one package and expecting it to turn out peaches.

https://devdocs.prestashop-project.org/8/modules/concepts/controllers/front-controllers/

https://devdocs.prestashop-project.org/8/modules/concepts/controllers/admin-controllers/

 

Link to comment
Share on other sites

Thank you - OK.

So the form (tpl) that I want to be seen on edit product page in BO - needs to be placed in

:/mymodule/views/templates/admin

or

/mymodule/views/templates/front

in  my form I got a link to action controller - that is:

<form action="{Context::getContext()->link->getModuleLink('mymodule', 'default')}" method="post">

so is it wrong? Should it link to controller file in

/mymodules/controllers/admin/

?

I read: https://devdocs.prestashop-project.org/8/modules/creation/module-file-structure/

It says:
 

Quote

 

The controllers folder contains the legacy-style Controller files.

Depending on where the controller belongs to, it is located in a different subfolder:

/controllers/admin: module’s back office controllers.

/controllers/front: module’s front office controllers.

 

I thought that module admin controller is for module configuration page, and front for module actions

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