Jump to content

Get id_product in a hook from a module (Prestashop 1.7)


GaShin

Recommended Posts

I would like to recover the product id to be able to make my ajax call which needs the product id of the current product page. So I use the hook displayProductAdditionnalInfo which correctly returns the id_product to me but when I try to call it in another method, it returns me false. 

Capture.PNG.3f34b36e9044c23c640c27918f4d5a73.PNG

Can you help me? Is there a solution or an alternative ?

  

Link to comment
Share on other sites

  • 2 years later...
public function hookDisplayProductAdditionalInfo($params)
    {
// either:       
// return $params['id_product'];
//or 
return Tools::getValue('id_product');
        
  }

returns 0

Why theres is no id_product in $params?

 

Link to comment
Share on other sites

the thing is that the $params[product] look to be unset...

my $params looks like this:


(
    [smarty] => 
    [cookie] => Cookie Object
        (
            [_content:protected] => Array
                (
                    [date_add] => 2024-04-02 12:15:09
                    [id_lang] => 1
                    [id_currency] => 1
                    [checksum] => 975070e263dfbafb
                )

            [_name:protected] => PrestaShop-f7fb070309913ddc58cc
            [_expire:protected] => 1713844444404921
            [_domain:protected] => xxxxxxxx
            [_sameSite:protected] => Lax
            [_path:protected] => /
            [cipherTool:protected] => PhpEncryption Object
                (
                )

            [_modified:protected] => 
            [_allow_writing:protected] => 1
            [_salt:protected] => UTRXp2p
            [_standalone:protected] => 
            [_secure:protected] => 1
            [session:protected] => 
        )

    [cart] => Cart Object
        (
            [id] => 
            [id_shop_group] => 1
            [id_shop] => 1
            [id_address_delivery] => 0
            [id_address_invoice] => 0
            [id_currency] => 1
            [id_customer] => 
            [id_guest] => 0
            [id_lang] => 1
            [recyclable] => 
            [gift] => 
            [gift_message] => 
            [mobile_theme] => 
            [date_add] => 
            [secure_key] => 
            [id_carrier] => 0
            [date_upd] => 
            [checkedTos] => 
            [pictures] => 
            [textFields] => 
            [delivery_option] => 
            [allow_seperated_package] => 
            [_products:protected] => 
            ....
)

 

Link to comment
Share on other sites

Check if your module is registered on this hook inside positions. 

1 godzinę temu, Butch0071 napisał:

the thing is that the $params[product] look to be unset...

my $params looks like this:


(
    [smarty] => 
    [cookie] => Cookie Object
        (
            [_content:protected] => Array
                (
                    [date_add] => 2024-04-02 12:15:09
                    [id_lang] => 1
                    [id_currency] => 1
                    [checksum] => 975070e263dfbafb
                )

            [_name:protected] => PrestaShop-f7fb070309913ddc58cc
            [_expire:protected] => 1713844444404921
            [_domain:protected] => xxxxxxxx
            [_sameSite:protected] => Lax
            [_path:protected] => /
            [cipherTool:protected] => PhpEncryption Object
                (
                )

            [_modified:protected] => 
            [_allow_writing:protected] => 1
            [_salt:protected] => UTRXp2p
            [_standalone:protected] => 
            [_secure:protected] => 1
            [session:protected] => 
        )

    [cart] => Cart Object
        (
            [id] => 
            [id_shop_group] => 1
            [id_shop] => 1
            [id_address_delivery] => 0
            [id_address_invoice] => 0
            [id_currency] => 1
            [id_customer] => 
            [id_guest] => 0
            [id_lang] => 1
            [recyclable] => 
            [gift] => 
            [gift_message] => 
            [mobile_theme] => 
            [date_add] => 
            [secure_key] => 
            [id_carrier] => 0
            [date_upd] => 
            [checkedTos] => 
            [pictures] => 
            [textFields] => 
            [delivery_option] => 
            [allow_seperated_package] => 
            [_products:protected] => 
            ....
)

 

 

Link to comment
Share on other sites

I meant check it inside backoffice if its actually registered. Because params from this hook are passed through template, you should also check template unless you are using default one.

Link to comment
Share on other sites

2 minuty temu, Butch0071 napisał:

Now I think maybe I am using the wrong hook ? What hook should I use to display additional info on product list page - on product's miniature ?

 

Yes its a wrong hook, additional infos are productpage only. You could make your own hook or use some of these which are there by default:
displayProductListReviews
displayProductPriceBlock

But they are also used on Product Page.

If you wish to make your own hook and execute it you could do it like this:
Below is sample from my module displaying manufacturer image on product listing

    public function hookdisplayListingManufacturer($params){
        if($params['product']['id_manufacturer']>0){
            if(!isset($params['product']['manufacturer_image'])){
                $params['product']['manufacturer_image'] = $this->getManufacutrerImage($params['product']['id_manufacturer']);
            }
            $this->context->smarty->assign($params);
            return $this->fetch('module:'.$this->name.'/views/templates/front/listingmanufacturer.tpl');
        }
    }


Then you add hook inside your template/catalog/_partials/miniatures/product.tpl

{hook h='displayListingManufacturer' product=$product}

Calling hook inside template passes the product array so you are able to use it inside your custom hook. Ofcourse you need to register hook first.

  • Thanks 1
Link to comment
Share on other sites

OK I have:

1 - register hook in my module: $this->registerHook('displayListingProductColorsNumber')

2 - I have checked - the module is hooked to that position

3 - I call hook : {hook h='displayListingProductColorsNumber' product=$product}
4- in function :

public function hookDisplayListingProductColorsNumber($params){

$languages = Language::getLanguages($active);

$this->context->smarty->assign(array( 
	'product_ID' => $params['product']['id'], 
	'languages' => $languages, 
	'default_language' => $this->context->employee->id_lang, 
));

}

I print_r($params) in function (and it displays content of $params in good place)- but I still see no product data

Array ( [product] =>

What am I missing?

It looks like the $params doesn't have product data.

 

Link to comment
Share on other sites

30 minut temu, Butch0071 napisał:

OK I have:

1 - register hook in my module: $this->registerHook('displayListingProductColorsNumber')

2 - I have checked - the module is hooked to that position

3 - I call hook : {hook h='displayListingProductColorsNumber' product=$product}
4- in function :

public function hookDisplayListingProductColorsNumber($params){

$languages = Language::getLanguages($active);

$this->context->smarty->assign(array( 
	'product_ID' => $params['product']['id'], 
	'languages' => $languages, 
	'default_language' => $this->context->employee->id_lang, 
));

}

I print_r($params) in function (and it displays content of $params in good place)- but I still see no product data

Array ( [product] =>

What am I missing?

It looks like the $params doesn't have product data.

 

you need to return template file and in your template you will be able to access it. I checked and i can var_dump params['product'].
Also you have error with product id.

	'product_ID' => $params['product']['id_product'], 

Also please explain the meaning on the languages, because you can acess it using 
 

$this->context->language->id

 

Link to comment
Share on other sites

OK, thank you - I am returning :

return $this->display(__FILE__, 'views/templates/hook/productscolors.tpl');

And corrected to: (int)$params['id_product'];

I also try with new Product($params['id_product'])

 public function hookDisplayListingProductColorsNumber($params)
    {
	$productID = (int)$params['id_product'];
    
	$product = new Product($params['id_product']);

    $this->context->smarty->assign(array(
            'product_ID' => $productID,
            'prodID' => $product->id_product,
            'params' => $params,
        ));

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

and in productscolors.tpl I have:

Product ID: {$product_ID} ({$prodID})
<pre>
    {$params|@print_r}
</pre>

and on product list I see:

Product ID: 0()

Array ( [product] =>

...

Link to comment
Share on other sites

You need to use $params['product']['id_product'] because $params have other variables than just product.
 

var_dump($params['product']['id_product']);

Copy it and paste inside your hook, then check if it will be displayed.

Link to comment
Share on other sites

OK still I have empty product

from my tpl I get: NULL Product ID: ()

public function hookDisplayListingProductColorsNumber($params)
    {
	$productID = (int)$params['id_product'];
    
	$product = new Product($params['id_product']);

    var_dump($params['product']['id_product']);

    $this->context->smarty->assign(array(
            'product_ID' => $params['product']['id_product'],
            'prodID' => $product->id_product,
            'params' => $params,
        ));
        $this->context->smarty->assign($params);

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

and my tpl:

Product ID: {$product_ID}({$prodID})

Link to comment
Share on other sites

42 minuty temu, Butch0071 napisał:

I use: {hook h='displayListingProductColorsNumber' product=$product}

Well thats wreid. Which prestashop version are you using?

Link to comment
Share on other sites

I do not get it.

However when I hook on product page and :

$productID = $params['product']->name;

$this->context->smarty->assign(array(
    'product_ID' => $params['product']->id,
    'prodID' => $productID,
));

I got displayed Id and name - as supposed to be.

But when on product list - product is empty...

 

Link to comment
Share on other sites

In what .tpl have you placeed hook in 1.7.8.1?

For me it workds on product page but it doesn't on product list..

I need to check it on different version I quess or with different theme - maybe it messes something up.

 

 

Link to comment
Share on other sites

15 minut temu, Butch0071 napisał:

In what .tpl have you placeed hook in 1.7.8.1?

For me it workds on product page but it doesn't on product list..

I need to check it on different version I quess or with different theme - maybe it messes something up.

 

 

classic/catalog/_partials/miniatures/product.tpl

  • Thanks 1
Link to comment
Share on other sites

Thank you for your help.

It looks that Shortcode widget of CE is unable to pass variables. (I was using it) Confusing was becouse it placeed hook in right position and was passing other data.

I modyfied template .tpl file - and it works as it should.

@WisQQ thanks again for your help.

 

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