Jump to content

Get current product in a Hook


Recommended Posts

Hi!

 

I want to make a module that displays some product information in his current front page, for example in displayLeftColumnProduct hook.

 

My problem is I don't know how to get current product and his id_product_attribute in displayLeftColumnProduct function in my module, and I didn't find anything googling.

 

It is possible to get this information without getting it from URL?

Thanks

Link to comment
Share on other sites

when you set up the hook in your module do like this

public function hookDisplayLeftColumnProduct($params)

in the params variable you will find all that is needed!

Thanks for the info!

 

I'm trying to output that $params into a var_dump sentence but I can't see field $product or some id_product in the array.

This is the most similar thing I've found.

protected '_products' => 
        array (size=1)
          0 => 
            array (size=63)
              ...
Link to comment
Share on other sites

The first time I've tried with a 

die(var_dump($params));

directly.

 

EDIT

 

Inside '_products' array with size = 63 are all things that we were talking about.

 

Thanks BalzoT! You helped me a lot!

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

what is the output you get?

This:

object(Cart)[20]
 // ... fields of cart
  protected '_products' => 
    array (size=1)
      0 => 
        array (size=63)
          'id_product_attribute' => string '1' (length=1)
          'id_product' => string '1' (length=1)
          'cart_quantity' => string '12' (length=2)
          'id_shop' => string '1' (length=1)
          'name' => string 'RIS Daniel Defense 9,5"' (length=23)
          'is_virtual' => string '0' (length=1)
          'description_short' => string '<p>Prueba short description</p>' (length=31)
          'available_now' => string '' (length=0)
          'available_later' => string '' (length=0)
          'id_category_default' => string '2' (length=1)
          'id_supplier' => string '0' (length=1)
          'id_manufacturer' => string '1' (length=1)
          'on_sale' => string '0' (length=1)
          'ecotax' => string '0.000000' (length=8)
          'additional_shipping_cost' => string '0.00' (length=4)
          'available_for_order' => string '1' (length=1)
          'price' => float 46.68
          'active' => string '1' (length=1)
          'unity' => string '' (length=0)
          'unit_price_ratio' => string '1.015111' (length=8)
          'quantity_available' => string '249213' (length=6)
          'width' => string '0.000000' (length=8)
          'height' => string '0.000000' (length=8)
          'depth' => string '0.000000' (length=8)
          'out_of_stock' => string '1' (length=1)
          'weight' => float 0
          'date_add' => string '2017-03-10 10:26:12' (length=19)
          'date_upd' => string '2017-05-04 10:21:04' (length=19)
          'quantity' => int 12
          'link_rewrite' => string 'el-fary-vive' (length=12)
          'category' => string 'inicio' (length=6)
          'unique_id' => string '000000000100000000014' (length=21)
          'id_address_delivery' => string '4' (length=1)
          'advanced_stock_management' => string '1' (length=1)
          'supplier_reference' => null
          'customization_quantity' => null
          'id_customization' => null
          'price_attribute' => string '1.000000' (length=8)
          'ecotax_attr' => string '0.000000' (length=8)
          'reference' => string '' (length=0)
          'weight_attribute' => string '0.000000' (length=8)
          'ean13' => string '123456789101' (length=12)
          'upc' => string '' (length=0)
          'minimal_quantity' => string '1' (length=1)
          'wholesale_price' => string '50.340000' (length=9)
          'id_image' => string '1-4' (length=3)
          'legend' => string 'PRUEBAXML' (length=9)
          'reduction_type' => int 0
          'stock_quantity' => int 249213
          'price_without_reduction' => float 56.4828
          'price_with_reduction' => float 56.4828
          'price_with_reduction_without_tax' => float 46.68
          'total' => float 560.16
          'total_wt' => float 677.79
          'price_wt' => float 56.4828
          'reduction_applies' => boolean false
          'quantity_discount_applies' => boolean false
          'allow_oosp' => int 1
          'features' => 
            array (size=0)
              ...
          'attributes' => string 'Size : M, Color : Beige' (length=23)
          'attributes_small' => string 'M, Beige' (length=8)
          'rate' => float 21
          'tax_name' => string 'IVA ES 21%' (length=10)
  // ... Other cart fields

Oh... I was looking the same product I had in my actual cart so this isn't what I want  :D .

What a fail!

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

Glad to help! 

Sorry BalzoT, it was very helpful for see that prestashop saves when we're looking for the page, but I still can not find the current product in $params when I am in the product page.

What I've said later it was a total fail  :D  :D  :D .

Link to comment
Share on other sites

I guess the most straight forward solution is to change your code to 

public function hookDisplayLeftColumnProduct($params)
{
    $product = new Product(Tools::getValue('id_product'));
}

In the $product variable you will have all that is needed

Link to comment
Share on other sites

It's fine to get product ID but I also need get product attribute. I've tried it with same method and returns me a null value.

	public function hookDisplayLeftColumnProduct($params) {
		var_dump($this->context);
		var_dump(Tools::getValue('id_product'));
		var_dump(Tools::getValue('id_product_attribute')); //Returns null
		$product = new Product(Tools::getValue('id_product'));
		var_dump($product);
	}
Link to comment
Share on other sites

For the default attribute you need

public function hookDisplayLeftColumnProduct($params) {
    $product = new Product(Tools::getValue('id_product'));
    var_dump($product->id);// the product's id
    var_dump($product->cache_default_attribute);// the attribute
    die;
}
Link to comment
Share on other sites

I'm with the same trouble. If I have one product with two combinations I am always getting the default attribute ID.

I need the selected attribute ID for check, for example, his stock.

 

Has smarty some field saved when we navigate inside a product page?

 

Thanks a lot for your help, I'm feeling like I'm wasting your time.

Link to comment
Share on other sites

I searched JS and tpl files from get attribute but is very difficult because I don't know if its possible to assign a smarty.

I'm going to search another way, like say some info for each product attribute. I think I could do that without complicate hook stuff.

 

Thanks for your help BalzoT, I'll coment my solution if works.

Link to comment
Share on other sites

  • 11 months later...

Hello,

I found a simple way to retrieve the current id_product_value. After few days of google search and file inspection

 

In the ProductController I found that they retrieve the value with this simple request : 

$_GET['id_product_attribute']

Or 

Tools::getValue('id_product_attribute')

 

And it look like it work for me but you need to reload the page. Didn't already test it with ajax.

 

Hope it will help.

 

Bruno-

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

  • 5 years later...
public function hookDisplayLeftColumnProduct($params) {
    $product = new Product(Tools::getValue('id_product'));
    var_dump($product->id);// the product's id
    var_dump($product->cache_default_attribute);// the attribute
    die;
}

And I got empty $product object

and (Tools::getValue('id_product')) - is empty

My $params doesn't have id_product - it has only cart object.

I am using this hook on products list - on product miniature. Do I need to use different hook there? If so then wchich one?

I need to have product ID on priduct miniature on product lists

thank you

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